WCF + SilverLight4 + WPF 的疑难了
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");
mo_binding = new PollingDuplexHttpBinding(PollingDuplexMode.MultipleMessagesPerPoll);
mo_wcfRunner = new QBox.ServiceReference1.ServiceClient(mo_binding, mo_address);
接下来就是调用方法了.其它调用的代码省略。。。
到此,网页SilverLight与IIS中的WCF互相通信都没有问题。
下面再说一说存在问题的 WPF窗体程序与这个IIS中的WCF通信的问题:
新建WPF应用程序后,引用服务http://localhost/WCFService.svc:
然后系统自动生成app.config配置文件,内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="PollingDuplexHttpBinding_IService">
<!-- WsdlImporter 在 ServiceDescription“http://tempuri.org/”中遇到了无法识别的策略断言: -->
<!-- <wsdl:binding name='PollingDuplexHttpBinding_IService'> -->
<!-- <netdx:Duplex xmlns:netdx="http://schemas.microsoft.com/2008/04/netduplex">..</netdx:Duplex> -->
<binaryMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
maxSessionSize="2048">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
</binaryMessageEncoding>
<httpTransport manualAddressing="false" maxBufferPoolSize="524288"
maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
useDefaultWebProxy="true" />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost/WCFService.svc" binding="customBinding"
bindingConfiguration="PollingDuplexHttpBinding_IService" contract="ServiceReference1.IService"
name="PollingDuplexHttpBinding_IService" />
</client>
</system.serviceModel>
</configuration>
自己编写客户端供服务器调用的回调类实现代码如下:
public class ClientCallBacks : ServiceReference1.IServiceCallback
{
public void ServerSendMessage(string msg)
{
MessageBox.Show(msg);
}
}
编写调用WCF调用的过程:
private void button1_Click(object sender, RoutedEventArgs e)
{
ClientCallBacks back = new ClientCallBacks();
InstanceContext lo_context = new InstanceContext(back);
ServiceReference1.ServiceClient lo_wcf = new ServiceReference1.ServiceClient(lo_context);
lo_wcf.RegAsWcfMsgReceiver("", "", ServiceReference1.WcfMsgType.None, null, null);
}
运行,提示异常:
协定需要双工,但是绑定“CustomBinding”不支持它或者因配置不正确而无法支持它。
希望高手指导:
如何才能解决这个异常,并实现WPF窗口程序与IIS中的WCF也能顺利双工通信呢?
目前网页的SilverLight4与IIS中的WCF双工通信没问题。
[解决办法]
我觉得全部是你WPF 的代码问题,按理说WPF 如果运行得起,SILVERLIGHT 才可以用得起的啊,不知道你的是不是NETTCP 连接方式,如果是的话明显 SILVERLIGHT 要求的要更复杂些啊。因为需要80端口下面有那个.XML文件啊。
[解决办法]
wpf代码跟silverlight代码类似啊,两者应该差不多,你在检查一下
[解决办法]
可能pollingDuplexHttpBinding 无法与 WPF 协作,
添加一个wsDualHttpBinding 与 WPF 通信看看