简体   繁体   English

尝试使用WCF服务时获取FaultException

[英]Getting a FaultException when trying to work with a WCF service

EDIT: Here's my call stack. 编辑:这是我的调用堆栈。

System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(System.ServiceModel.Channels.Message reply, System.ServiceModel.Channels.MessageFault fault, string action, System.ServiceModel.Channels.MessageVersion version, System.ServiceModel.Channels.FaultConverter faultConverter) + 0x124 bytes 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 operation, ref System.ServiceModel.Dispatcher.ProxyRpc rpc) + 0x147 bytes 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.Channels.ServiceChannel.EndCall(字符串操作,对象[]输出,System.IAsyncResult结果)+ 0xb2字节
System.ServiceModel.dll!System.ServiceModel.ClientBase.ChannelBase.EndInvoke(string methodName, object[] args, System.IAsyncResult result) + 0x1e bytes System.ServiceModel.dll!System.ServiceModel.ClientBase.ChannelBase.EndInvoke(字符串methodName,object [] args,System.IAsyncResult结果)+ 0x1e字节
PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(System.IAsyncResult result) Line 420 C# PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.PhoneClient.ServiceReference1.IService1.EndGetFirstAidGuides(System.IAsyncResult result) Line 284 + 0x7 bytes C# PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.OnEndGetFirstAidGuides(System.IAsyncResult result) Line 292 + 0x2 bytes C# System.ServiceModel.dll!System.ServiceModel.ClientBase.OnAsyncCallCompleted(System.IAsyncResult result) + 0x20 bytes PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(System.IAsyncResult结果)420行C#PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.PhoneClient.ServiceReference1.IService1.EndGetFirstAidGuides(System.IAsyncResult结果)第284 + 0x7字节C#PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.OnEndGetFirstAidGuides(System.IAsyncResult result)行292 + 0x2字节C#System.ServiceModel.dll!System.ServiceModel.ClientBase.OnAsyncCallCompleted(System.IAsyncResult结果)+ 0x20字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 bytes System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool同步完成)+ 0x66字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception exception) + 0xe bytes System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool已同步完成,System.Exception异常)+ 0xe字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.CallComplete(bool completedSynchronously, System.Exception exception) + 0x8 bytes System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.CallComplete(bool同步完成,System.Exception异常)+ 0x8字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishSend(System.IAsyncResult result, bool completedSynchronously) + 0x99 bytes System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishSend(System.IAsyncResult结果,布尔同步完成)+ 0x99字节
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.SendCallback(System.IAsyncResult result) + 0x1a bytes System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.SendCallback(System.IAsyncResult结果)+ 0x1a字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 bytes System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool同步完成)+ 0x66字节
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception exception) + 0xe bytes System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool已同步完成,System.Exception异常)+ 0xe字节
System.ServiceModel.dll!System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(System.IAsyncResult result) + 0x52 bytes 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 bytes mscorlib.dll!System.Threading.ThreadPool.WorkItem.WaitCallback_Context(object state) + 0x18 bytes System.Windows.dll!System.Net.Browser.ClientHttpWebRequest.InvokeGetResponseCallback.AnonymousMethod__8(对象状态2)+ 0x1b字节mscorlib.dll!System.Threading.ThreadPool.WorkItem.WaitCallback_Context(对象状态)+ 0x18字节
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x63 bytes mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executeContext,System.Threading.ContextCallback回调,对象状态)+ 0x63字节
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(object o) + 0x47 bytes mscorlib.dll!System.Threading.Timer.ring() + 0x70 bytes mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(对象o)+ 0x47字节mscorlib.dll!System.Threading.Timer.ring()+ 0x70字节

And the error: The server was unable to process the request due to an internal error. 错误:服务器由于内部错误而无法处理请求。 For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs. 有关错误的更多信息,请打开服务器上的IncludeExceptionDetailInFaults(从ServiceBehaviorAttribute或从配置行为),以便将异常信息发送回客户端,或者根据Microsoft .NET Framework 3.0 SDK文档打开跟踪。并检查服务器跟踪日志。

I'm currenntly working on a Windows Phone 7 application in which I am communicating with a WCF service. 我目前正在与WCF服务进行通信的Windows Phone 7应用程序上工作。 I've made it work within one method already. 我已经使其在一种方法中工作了。 So I that it is possible. 所以我认为这是可能的。

Here is my class that calls the WCF service 这是我的类,它调用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;
    }
}

Right now, I'm just trying to get some text written in a textblock, from my result. 现在,我只是想从结果中获取一些文本写入文本块。

This is the interface of the WCF service. 这是WCF服务的接口。

[ServiceContract]
public interface IService1
{

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

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

The method of my service class, that pulls data from a database. 我的服务类的方法,该方法从数据库中提取数据。

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

And just for convenience. 只是为了方便。 The FirstAidGuide class. 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; }
    }
}

I simply cannot get it to do anything. 我根本无法做任何事情。 I'm getting a FaultException, which points me in the direction that it cannot handle the response from the WCF service. 我收到了FaultException,这向我指出了它无法处理WCF服务的响应的方向。

Any help would be appreciated. 任何帮助,将不胜感激。

Can you try enabling tracing on your WCF Service and also inspect the trace to find out what the error is. 您可以尝试在WCF服务上启用跟踪吗,还可以检查跟踪以找出错误是什么吗? Also set the below property to get a complete stack trace of the error 还要设置下面的属性以获得错误的完整堆栈跟踪

<serviceDebug includeExceptionDetailInFaults="true" />

What I tend to do over WCF is to wrap everything inside my [OperationContract] method in a Try...Catch block; 我倾向于通过WCF进行的操作是将所有内容包装在Try...Catch块中的[OperationContract]方法中; unravel the stack trace of any caught exception and all inner exceptions and stick that as a string into the message of a FaultException that I then re-throw over the soap boundary. 解开所有捕获的异常和所有内部异常的堆栈跟踪,并将其作为字符串粘贴到FaultException的消息中,然后我将其重新抛出肥皂边界。 Something like this: 像这样:

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

The problem was in this line: 问题出在这一行:

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

Apparently calling .ToList() made the whole thing crash. 显然,调用.ToList()使整个过程崩溃。 Simply removing .ToList() fixed everything. 只需删除.ToList()即可修复所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM