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

时间:2010-10-18 02:22:55

标签: wpf silverlight wcf

我正在构建一个具有自托管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值。分配给此操作的时间可能是较长超时的一部分。

它全部运行在localhost上,确实不应该有延迟,更不用说延迟1分钟了。我不知道为什么,但频道似乎已关闭或丢失或某事......

我也尝试删除绑定上的超时,我得到像这样的错误

通讯对象System.ServiceModel.Channels.ServiceChannel不能用于通讯,因为它已被中止

我怎样才能找出这里的错误?

1 个答案:

答案 0 :(得分:2)

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

在使用PollingDuplexHttpBinding的许多问题后,我决定使用没有MultipleMessagesPerPoll的CustomBinding。

的web.config

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

DuplexService.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);