【问题标题】:Getting a FaultException when trying to work with a WCF service尝试使用 WCF 服务时出现 FaultException
【发布时间】:2011-12-08 16:26:16
【问题描述】:

编辑:这是我的调用堆栈。

System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(System.ServiceModel.Channels.Message 回复, System.ServiceModel.Channels.MessageFault 故障, 字符串操作, System.ServiceModel.Channels.MessageVersion 版本, System .ServiceModel.Channels.FaultConverter faultConverter) + 0x124 字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.HandleReply(System.ServiceModel.Dispatcher.ProxyOperationRuntime 操作,参考 System.ServiceModel.Dispatcher.ProxyRpc rpc) + 0x147 字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.EndCall(string action, object[] outs, System.IAsyncResult result) + 0xb2 bytes
System.ServiceModel.dll!System.ServiceModel.ClientBase.ChannelBase.EndInvoke(string methodName, object[] args, System.IAsyncResult 结果) + 0x1e 字节
PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(System.IAsyncResult 结果) 第 420 行 C# PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.PhoneClient.ServiceReference1.IService1.EndGetFirstAidGuides(System.IAsyncResult 结果) Line 284 + 0x7 bytes C# PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.OnEndGetFirstAidGuides(System.IAsyncResult 结果) 第 292 行 + 0x2 字节 C# System.ServiceModel.dll!System.ServiceModel.ClientBase.OnAsyncCallCompleted(System.IAsyncResult 结果) + 0x20 字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception 异常) + 0xe 字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.CallComplete(bool completedSynchronously, System.Exception 异常) + 0x8 字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishSend(System.IAsyncResult 结果, bool completedSynchronously) + 0x99 字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.SendCallback(System.IAsyncResult 结果) + 0x1a 字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception 异常) + 0xe 字节
System.ServiceModel.dll!System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(System.IAsyncResult 结果) + 0x52 字节
System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.InvokeGetResponseCallback.AnonymousMethod__8(object state2) + 0x1b 字节 mscorlib.dll!System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象状态) + 0x18 字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback 回调, 对象状态) + 0x63 字节
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(object o) + 0x47 bytes mscorlib.dll!System.Threading.Timer.ring() + 0x70 字节

错误:由于内部错误,服务器无法处理请求。有关错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(来自 ServiceBehaviorAttribute 或来自配置行为)以便将异常信息发送回客户端,或根据 Microsoft .NET Framework 3.0 SDK 文档打开跟踪并检查服务器跟踪日志。

我目前正在开发一个与 WCF 服务通信的 Windows Phone 7 应用程序。我已经让它在一种方法中工作了。所以我认为这是可能的。

这是我调用 WCF 服务的类

public partial class FirstAidGuides : PhoneApplicationPage
{
    public FirstAidGuides()
    {
        InitializeComponent();
        ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
        sc.GetFirstAidGuidesCompleted += new EventHandler<ServiceReference1.GetFirstAidGuidesCompletedEventArgs>(sc_GetFirstAidGuidesCompleted);
        sc.GetFirstAidGuidesAsync();
    }

    void sc_GetFirstAidGuidesCompleted(object sender, ServiceReference1.GetFirstAidGuidesCompletedEventArgs e)
    {
        FirstAidGuideText.Text = e.Result[0].Text;
    }
}

现在,我只是想从我的结果中获取一些写在文本块中的文本。

这是WCF服务的接口。

[ServiceContract]
public interface IService1
{

    [OperationContract]
    long CreateCall(string phoneNumber, double longtitude, double langtitude);

    [OperationContract]
    List<Model.FirstAidGuide> GetFirstAidGuides();
}

我的服务类的方法,从数据库中提取数据。

public List<Model.FirstAidGuide> GetFirstAidGuides()
    {
        DataClasses1DataContext db = new DataClasses1DataContext();

        var firstAidGuides = (from f in db.FirstAidGuides select f);
        List<Model.FirstAidGuide> list = new List<Model.FirstAidGuide>();

        foreach (var guide in firstAidGuides.ToList())
        {
            Model.FirstAidGuide fa = new Model.FirstAidGuide();
            fa.FirstAidId = guide.FirstAidId;
            fa.Title = guide.FirstAidTitle;
            fa.Text = guide.FirstAidText;
            fa.LastUpdated = (DateTime)guide.LastUpdated;
            list.Add(fa);
        }
        return list;
    }

只是为了方便。 FirstAidGuide 类。

[DataContract]
public class FirstAidGuide
{
    [DataMember]
    private string _title;
    [DataMember]
    private string _text;
    [DataMember]
    private DateTime _lastUpdated;
    [DataMember]
    private long _firstAidId;

    public long FirstAidId
    {
        get { return _firstAidId; }
        set { _firstAidId = value; }
    }      

    public DateTime LastUpdated
    {
        get { return _lastUpdated; }
        set { _lastUpdated = value; }
    }     

    public string Text
    {
        get { return _text; }
        set { _text = value; }
    }      

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }
}

我根本无法让它做任何事情。我收到了一个 FaultException,它指出了它无法处理来自 WCF 服务的响应的方向。

任何帮助将不胜感激。

【问题讨论】:

  • 为什么你的 DataMember 属性在你的支持字段上而不是在公共属性上?
  • 我真的不知道。在尝试了几乎所有感觉之后,这是一个绝望的举动:)
  • 您需要将它们移回您的属性而不是支持字段。您得到的确切 FaultException 是什么?
  • 用堆栈跟踪编辑了我的帖子。抱歉格式不好。仍然没有想出如何让它看起来不那么糟糕。

标签: c# wcf list


【解决方案1】:

您能否尝试在您的 WCF 服务上启用 tracing 并检查跟踪以找出错误所在。还设置以下属性以获得错误的完整堆栈跟踪

<serviceDebug includeExceptionDetailInFaults="true" />

【讨论】:

  • > PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(System.IAsyncResult result) 第 420 行 C# 这行似乎对我造成了问题。
【解决方案2】:

我倾向于在 WCF 上做的是将我的 [OperationContract] 方法中的所有内容包装在 Try...Catch 块中;解开任何捕获的异常和所有内部异常的堆栈跟踪,并将其作为字符串粘贴到 FaultException 的消息中,然后我将其重新抛出肥皂边界。像这样的:

public static string GetDebugString(this Exception ex)
{
   var builder = new StringBuilder();
   GetDebugString(ex, builder);
   while ((ex = ex.InnerException) != null)
   {
      GetDebugString(ex, builder);
   }
   return builder.ToString();
}


private static void GetDebugString(Exception ex, StringBuilder builder)
{
    builder.AppendLine(ex.GetType().Name);
    builder.AppendLine();
    builder.AppendLine(ex.Message);
    builder.AppendLine();
    builder.AppendLine(ex.StackTrace);
    builder.AppendLine();
}

[OperationContract]
public void Foo()
{
    this.MakeSafeCall(() => this.UnsafeFoo());
}

public void Unsafe()
{
    // do stuff
}

private void MakeSafeCall(Action action)
{
    try
    {
       action();
    }
    catch (Exception ex)
    {
       throw new FaultException(ex.GetDebugString());
    }
}

【讨论】:

    【解决方案3】:

    问题出在这一行:

    foreach(firstAidGuides.ToList() 中的 var 指南)

    显然调用 .ToList() 会使整个事情崩溃。 只需删除 .ToList() 即可解决所有问题。

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多