在尝试使用WCF服务时获得FaultException

本文关键字:FaultException 服务 WCF | 更新日期: 2023-09-27 18:17:07

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

System.ServiceModel.dll ! System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood (System.ServiceModel.Channels。消息回复,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操作,ref 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 result) + 0x1e bytes
PhoneClient.dll ! PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(系统。第420行c#PhoneClient.dll ! PhoneClient.ServiceReference1.Service1Client.PhoneClient.ServiceReference1.IService1.EndGetFirstAidGuides(系统。IAsyncResult result) Line 284 + 0x7字节c#PhoneClient.dll ! PhoneClient.ServiceReference1.Service1Client.OnEndGetFirstAidGuides(系统。IAsyncResult result) Line 292 + 0x2字节c#System.ServiceModel.dll ! System.ServiceModel.ClientBase.OnAsyncCallCompleted(系统。IAsyncResult result) + 0x20 bytes
System.ServiceModel.dll ! System.ServiceModel.AsyncResult。Complete(bool completedsynchronous) + 0x66 bytes
System.ServiceModel.dll ! System.ServiceModel.AsyncResult。Complete(bool completedsynchronsynchronized, System。异常异常)+ 0xe字节
System.ServiceModel.dll ! System.ServiceModel.Channels.ServiceChannel.SendAsyncResult。CallComplete(bool completedsynchronsynchronized, System。异常异常)+ 0x8字节
System.ServiceModel.dll ! System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishSend(系统。IAsyncResult result, bool completedsynchronsynchronous) + 0x99 bytes
System.ServiceModel.dll ! System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.SendCallback(系统。IAsyncResult result) + 0x1a bytes
System.ServiceModel.dll ! System.ServiceModel.AsyncResult。Complete(bool completedsynchronous) + 0x66 bytes
System.ServiceModel.dll ! System.ServiceModel.AsyncResult。Complete(bool completedsynchronsynchronized, System。异常异常)+ 0xe字节
System.ServiceModel.dll ! System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(系统。IAsyncResult result) + 0x52 bytes
System.Windows.dll ! System.Net.Browser.ClientHttpWebRequest.InvokeGetResponseCallback。AnonymousMethod__8(object state2) + 0x1b字节mscorlib.dll ! System.Threading.ThreadPool.WorkItem。WaitCallback_Context(object state) + 0x18 bytes
mscorlib.dll ! System.Threading.ExecutionContext.Run (System.Threading。ExecutionContext ExecutionContext, System.Threading.ContextCallback回调,对象状态)+ 0x63字节
mscorlib.dll ! System.Threading.ThreadPool.WorkItem。doWork(object o) + 0x47字节mscorlib.dll!System.Threading.Timer.ring() + 0x70 bytes

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

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

这是我的类调用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服务的响应的方向。

任何帮助都会很感激。

在尝试使用WCF服务时获得FaultException

您可以尝试在WCF服务上启用跟踪并检查跟踪以找出错误是什么吗?还可以设置下面的属性以获得错误

的完整堆栈跟踪
<serviceDebug includeExceptionDetailInFaults="true" />

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

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());
    }
}

问题出在这一行:

foreach (var guide in firstAidGuides.ToList())

显然调用。tolist()会导致整个程序崩溃。只需删除。tolist()就可以修复一切。