gpt4 book ai didi

.net - 为什么 Silverlight 不为我的自定义 ServiceModel.ClientBase channel 的行为调用 AfterReceiveReply?

转载 作者:行者123 更新时间:2023-12-02 15:09:10 24 4
gpt4 key购买 nike

我有一个第三方 API 的服务引用。我使用自定义 ChannelFactory 类来构建此 API 的 channel (类型为 System.ServiceModel.ClientBase 的 [WCF])。

我有一个自定义行为类(定义如下),我将其附加到此 channel 端点,以便处理从 API 返回的任何异常。在我正常的 .NET 代码中,这工作得很好。然而,在 Silverlight 中,仅当没有错误时才会调用 AfterReceiveReply 方法。

在这种情况下,当您尝试引用 eventArgs 的结果时,调用方法会遇到错误:'eventArgs.Result' 引发了类型为 'System.Reflection.TargetInitationException' 的异常

内部异常有:InnerException = {System.ServiceModel.CommunicationException:远程服务器返回错误:NotFound。

无论真实错误如何,我都会看到上述错误。在 Fiddler 中,我可以看到真正的错误返回。只是 channel 处理过程中隐藏了一些东西。下面是具有实际错误的 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;
}
}

channel 创建:

//...
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;

最佳答案

不知道这是否有帮助/有用。

默认情况下,silverlight 通过浏览器执行 HTTP 请求(包括 SOAP/REST)。通过这些调用,所有 HTTP/HTTPS 请求都将由 silverlight 客户端网络堆栈处理。

bool httpResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
bool httpsResult = WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

关于.net - 为什么 Silverlight 不为我的自定义 ServiceModel.ClientBase<TChannel> channel 的行为调用 AfterReceiveReply?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com