[英]WCF PollingDuplexHttpBinding with Silverlight Client timeouts and errors
我正在構建一個具有自托管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不能用於通信,因為它已被中止
我怎樣才能在這里找出錯誤的信息?
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.