1、网站页面用SilverLight4开发,
2、与数据库通信用WCF
3、客户端还包含一部分WPF开发的WinForm程序,与数据库的通信仍然用WCF。
4、WCF宿主为网站的IIS
5、要求服务器与SilverLight4与WPF皆可双工通信
问题:
网页的SilverLight4与WCF通信没有任何问题。
但WPF的WinForm窗口与WCF通信存在故障,
约定协议接口IService声明如下:
[ServiceContract(CallbackContract = typeof(IServiceCallBack))]
public interface IService
{
[OperationContract]
void Func(string msg);
}
客户端回调协议接口如下:
public interface IServiceCallBack
{
[OperationContract(IsOneWay = true)]
void ServerSendMessage(string arg);
}
当然,网站中具体的服务实现过程WCFService.svc这里就不列举了,无非就是函数代码而已,
这里仅列出结构:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class WCFService : QBox.IService
{
//此处省略不写代码了
}
由此,网站的Web.config配置如下:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="QBox.WCFServiceBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
<serviceTimeouts transactionTimeout="10:11:00"/>
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="QBox.WCFServiceBehavior" name="WCFService">
<endpoint address="" binding="pollingDuplexHttpBinding" bindingConfiguration="multipleMessagesPerPollPollingDuplexHttpBinding" contract="QBox.IService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<extensions>
<bindingExtensions>
<add name="pollingDuplexHttpBinding" type="System.ServiceModel.Configuration.PollingDuplexHttpBindingCollectionElement,System.ServiceModel.PollingDuplex, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</bindingExtensions>
</extensions>
<bindings>
<pollingDuplexHttpBinding>
<binding name="multipleMessagesPerPollPollingDuplexHttpBinding" duplexMode="MultipleMessagesPerPoll" maxOutputDelay="00:00:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647" />
</binding>
</pollingDuplexHttpBinding>
</bindings>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<defaultDocument>
<files>
<remove value="default.aspx"/>
<remove value="iisstart.htm"/>
<remove value="index.htm"/>
<remove value="Default.asp"/>
<remove value="Default.htm"/>
</files>
</defaultDocument>
<staticContent>
<clientCache cacheControlMode="DisableCache"/>
</staticContent>
<caching enabled="false" enableKernelCache="false"/>
</system.webServer>
<system.web>
<pages buffer="true"/>
<compilation>
<assemblies>
<add assembly="System.Data.OracleClient, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies></compilation></system.web>
<appSettings>
</appSettings></configuration>
可以看出是引用了SilverLight4 SDK中的pollingDuplexHttpBinding进行双工通信。
SilverLight4网页客户端连接此WCF的过程如下:
新建SilverLight4应用程序后,添加服务引用http://localhost/WCFService.svc,vs2010会自动生成
ServiceReferences.ClientConfig,文件内容只有下面这一句:
<configuration />
然后就是SilverLight4调用WCF服务:
EndpointAddress mo_address;
PollingDuplexHttpBinding mo_binding;
ServiceReference1.ServiceClient mo_wcfRunner = null;
mo_address = new EndpointAddress("http://localhost/WCFService.svc");