gpt4 book ai didi

wpf - WCF PollingDuplexHttpBinding 与 Silverlight 客户端超时和错误

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

我正在构建一个具有自托管 WCF 服务的 WPF 3.5 桌面应用。

该服务有一个 PollingDuplexHttpBinding 端点定义如下:

public static void StartService()
{
var selfHost = new ServiceHost(Singleton, new Uri("http://localhost:1155/"));

selfHost.AddServiceEndpoint(
typeof(IMyService),
new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll) {ReceiveTimeout = new TimeSpan(1,0,0,0)},
"MyService"
);

ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
selfHost.Description.Behaviors.Add(smb);

selfHost.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());

selfHost.Open();
}

注意:IPolicyRetriever 是一项使我能够定义策略文件的服务

这有效,我可以在我的客户端 Silverlight 应用程序中看到我的服务。然后我在 Silverlight 代码中创建对代理的引用,如下所示:

        EndpointAddress address = new EndpointAddress("http://localhost:1155/MyService");

PollingDuplexHttpBinding binding = new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);
binding.ReceiveTimeout = new TimeSpan(1, 0, 0, 0);
_proxy = new MyServiceClient(binding, address);
_proxy.ReceiveReceived += MessageFromServer;
_proxy.OrderAsync("Test", 4);

这也很好,沟通很有效!

但是如果我不理会它(即不从服务器发送消息)超过 1 分钟,然后尝试从 WPF 服务器应用程序向客户端发送消息,我会收到如下超时错误:

IOutputChannel 在 00:01:00 后尝试发送时超时。增加传递给 Send 调用的超时值或增加 Binding 上的 SendTimeout 值。分配给此操作的时间可能是较长超时的一部分。

它都在本地主机上运行,​​真的不应该有延迟,更不用说延迟 1 分钟了。不知道为什么, channel 好像关闭了或者丢失了什么的……

我也试过删除绑定(bind)的超时,但我得到了这样的错误

通信对象 System.ServiceModel.Channels.ServiceChannel 无法用于通信,因为它已被中止

我怎样才能找出问题所在?

最佳答案

WPF 使用 wsDualHttpBinding、Silverlight - 轮询双工。WPF 解决方案很简单; Silverlight 需要 ServiceHostFactory 和更多代码。此外,Silverlight 服务器从不发送消息,而是客户端轮询服务器并检索其消息。

在 PollingDuplexHttpBinding 出现许多问题后,我决定使用不带 MultipleMessagesPerPoll 的 CustomBinding。

网络配置

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="SlApp.Web.DuplexServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

<services>
<service behaviorConfiguration="SlApp.Web.DuplexServiceBehavior" name="SlApp.Web.DuplexService">
<endpoint address="WS" binding="wsDualHttpBinding" contract="SlApp.Web.DuplexService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>

双工服务.svc:

<%@ ServiceHost Language="C#" Debug="true" Service="SlApp.Web.DuplexService" Factory="SlApp.Web.DuplexServiceHostFactory" %>

DuplexServiceHostFactory.cs:

    public class DuplexServiceHostFactory : ServiceHostFactoryBase
{
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
return new DuplexServiceHost(baseAddresses);
}
}

class DuplexServiceHost : ServiceHost
{
public DuplexServiceHost(params Uri[] addresses)
{
base.InitializeDescription(typeof(DuplexService), new UriSchemeKeyedCollection(addresses));
}

protected override void InitializeRuntime()
{
PollingDuplexBindingElement pdbe = new PollingDuplexBindingElement()
{
ServerPollTimeout = TimeSpan.FromSeconds(3),
//Duration to wait before the channel is closed due to inactivity
InactivityTimeout = TimeSpan.FromHours(24)
};

this.AddServiceEndpoint(typeof(DuplexService),
new CustomBinding(
pdbe,
new BinaryMessageEncodingBindingElement(),
new HttpTransportBindingElement()), string.Empty);

base.InitializeRuntime();
}
}

Silverlight 客户端代码:

address = new EndpointAddress("http://localhost:43000/DuplexService.svc");
binding = new CustomBinding(
new PollingDuplexBindingElement(),
new BinaryMessageEncodingBindingElement(),
new HttpTransportBindingElement()
);
proxy = new DuplexServiceClient(binding, address);

关于wpf - WCF PollingDuplexHttpBinding 与 Silverlight 客户端超时和错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3956248/

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