我有一個對第三方API的服務引用。我使用自定義的ChannelFactory類來構建這個API的通道([System.ServiceModel.ClientBase類型的[WCF]))。爲什麼Silverlight不會爲我的自定義ServiceModel.ClientBase <TChannel>通道的行爲調用AfterReceiveReply?
我有一個自定義行爲類(定義如下),我附加到此通道端點,以處理從API返回的任何異常。在我的正常.NET代碼中,這工作正常。但是,在Silverlight中,僅當沒有錯誤時才調用AfterReceiveReply方法。
在這種情況下,當您嘗試引用eventArgs的結果時調用方法遇到錯誤:'eventArgs.Result' threw an exception of type 'System.Reflection.TargetInvocationException'
。
內部異常有:InnerException = {System.ServiceModel.CommunicationException: The remote server returned an error: NotFound.
而不管真正的錯誤我看到上面的錯誤。在菲德勒,我可以看到真正的錯誤回來。有一些關於隱藏它的通道處理。下面是具有真正錯誤的SOAP響應的示例。 (有些值已被刪除。)
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<soapenv:Fault>
<faultcode><!-- REMOVED REAL CODE --></faultcode>
<faultstring><!-- REMOVED REAL FAULT --></faultstring>
<detail>
<!-- REMOVED DETAILS -->
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
我確定我還沒有提供足夠的信息,但我不知道什麼是相關的。在評論中詢問更多信息。
我該如何去調試呢?相同的代碼在.NET中工作,但Silverlight不能很好地處理它。
一些相關的代碼如下。
行爲:
public class ExceptionMapperBehavior : IClientMessageInspector, IEndpointBehavior
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{
//this is only called if there is no fault--not helpful!
if (reply == null || !reply.IsFault)
return;
//todo: make the exception pretty
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
//this is always called
return null;
}
}
頻道創建:
//...
var constructor = typeof(T).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
var ret = (T)constructor.Invoke(new object[] { binding, endpointAddress });
ret.Endpoint.Behaviors.Add(new CredentialInserterEndpointBehavior(_authToken));
ret.Endpoint.Behaviors.Add(new ExceptionMapperBehavior());
return ret;
這工作。萬分感謝。 – EndangeredMassa 2011-02-14 04:30:41