2011-12-08 64 views
2

編輯:這是我的調用堆棧。嘗試使用WCF服務時收到FaultException

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.HandleReply(System.ServiceModel.Dispatcher.ProxyOperationRuntime operation, ref System.ServiceModel.Dispatcher.ProxyRpc rpc) + 0x147 bytes
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(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
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 bytes
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception exception) + 0xe bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.CallComplete(bool completedSynchronously, System.Exception exception) + 0x8 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.FinishSend(System.IAsyncResult result, bool completedSynchronously) + 0x99 bytes
System.ServiceModel.dll!System.ServiceModel.Channels.ServiceChannel.SendAsyncResult.SendCallback(System.IAsyncResult result) + 0x1a bytes
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously) + 0x66 bytes
System.ServiceModel.dll!System.ServiceModel.AsyncResult.Complete(bool completedSynchronously, System.Exception exception) + 0xe bytes
System.ServiceModel.dll!System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(System.IAsyncResult result) + 0x52 bytes
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
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) + 0x63 bytes
mscorlib.dll!System.Threading.ThreadPool.WorkItem.doWork(object o) + 0x47 bytes 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服務的響應。

任何幫助,將不勝感激。

+0

爲什麼你的支持字段而不是公共屬性上有DataMember屬性? –

+0

我真的不知道。這是一個絕望的舉措,經過嘗試幾乎所有的感覺:) –

+0

你會想要將這些移回你的屬性,而不是支持領域。你得到的確切FaultException是什麼? –

回答

-1

的問題是在這條線:

的foreach(VAR指南firstAidGuides.ToList())

顯然調用.ToList()做這件事崩潰。 只需刪除.ToList()修復了一切。

1

您可以嘗試在您的WCF服務上啓用tracing並檢查跟蹤以找出錯誤是什麼。還設置了以下屬性來獲取錯誤

<serviceDebug includeExceptionDetailInFaults="true" /> 
+0

> \t PhoneClient.dll!PhoneClient.ServiceReference1.Service1Client.Service1ClientChannel.EndGetFirstAidGuides(System.IAsyncResult結果)線420 \t C# 這是一個似乎會導致這個問題對我來說就行了。 –

1

我傾向於做了WCF是我的包裹在Try...Catch[OperationContract]方法內部一切的完整堆棧跟蹤;解開捕獲到的異常和所有內部異常的堆棧跟蹤,並將其作爲字符串粘貼到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()); 
    } 
} 
相關問題