首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WCF PollingDuplexHttpBinding与Silverlight客户端超时和错误

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

Stack Overflow用户
提问于 2010-10-18 02:22:55
回答 1查看 3.2K关注 0票数 3

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

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

代码语言:javascript
复制
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代码中创建对代理的引用,如下所示:

代码语言:javascript
复制
        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服务器应用程序向客户端发送一条消息,我会得到如下所示的超时错误:

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

它全部在本地主机上运行,不应该有延迟,更不用说1分钟的延迟了。我不知道为什么,但是这个频道好像关闭了或者消失了什么的.

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

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

我怎么才能找出这里出了什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-10-19 08:13:44

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

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

web.config

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<%@ ServiceHost Language="C#" Debug="true" Service="SlApp.Web.DuplexService" Factory="SlApp.Web.DuplexServiceHostFactory" %>

DuplexServiceHostFactory.cs:

代码语言:javascript
复制
    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客户端代码:

代码语言:javascript
复制
address = new EndpointAddress("http://localhost:43000/DuplexService.svc");
binding = new CustomBinding(
            new PollingDuplexBindingElement(),
            new BinaryMessageEncodingBindingElement(),
            new HttpTransportBindingElement()
        );
proxy = new DuplexServiceClient(binding, address);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3956248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档