悬赏园豆:100
[已关闭问题]
关闭于 2011-01-01 14:46
<pre>环境:客户端:Silverlight4,服务端(.netfx 4.0,wcf)</pre>
<pre>这个系统是提供给企业内部使用的,我希望系统能自动根据运行环境来选择通过http或者tcp来访问wcf服务.</pre>
<pre>如果允许在浏览器中,使用basichttpbinding,如果在浏览器外运行,则使用tcpnetbinding,以获得更好的性能.</pre>
<pre>我在Silverlight中首先添加了http绑定的wcf服务,然后手工修改配置文件.</pre>
<pre>在配置文件中添加了Tcpnet绑定的内容,希望两个服务可以共用一个Contact.终结点的配置如下: </pre>
<pre><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff;"><</span><span style="color: #800000;">endpoint </span><span style="color: #ff0000;">address</span><span style="color: #0000ff;">="http://localhost/abcService.svc"</span><span style="color: #ff0000;"> binding</span><span style="color: #0000ff;">="basicHttpBinding"</span><span style="color: #ff0000;"> bindingConfiguration</span><span style="color: #0000ff;">="BasicHttpBinding"</span><span style="color: #ff0000;"> contract</span><span style="color: #0000ff;">="Server.IAbcService"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="BasicHttpBinding_IAbcService"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span></div><div><span style="color: #0000ff;"><</span><span style="color: #800000;">endpoint </span><span style="color: #ff0000;">address</span><span style="color: #0000ff;">="net.tcp://localhost:4512/abcService"</span><span style="color: #ff0000;"> binding</span><span style="color: #0000ff;">="customBinding"</span><span style="color: #ff0000;"> bindingConfiguration</span><span style="color: #0000ff;">="NetTcpBinding_AbcService"</span><span style="color: #ff0000;"> contract</span><span style="color: #0000ff;">="Server.IAbcService"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="NetTcpBinding_IAbcService"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/></span><span style="color: #000000;"><br /></span></div></pre>
</div>
<p> </p>
然后在代码中这样获取服务,这是个服务工厂,对于使用者是透明的: </pre>
<pre><div class="cnblogs_code"><pre><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000ff;">public</span><span style="color: #000000;"> </span><span style="color: #0000ff;">static</span><span style="color: #000000;"> AbcServiceClient GetAbcService()</span></div><div><span style="color: #000000;">{ </span></div><div><span style="color: #000000;"> </span><span style="color: #0000ff;"> if</span><span style="color: #000000;"> (Application.Current.IsRunningOutOfBrowser) </span></div><div><span style="color: #000000;"> { </span></div><div><span style="color: #000000;"> var addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> AppContext.TcpServerHost </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">:4512/AbcService</span><span style="color: #800000;">"</span><span style="color: #000000;">;</span></div><div><span style="color: #000000;"> Log.Debug(</span><span style="color: #800000;">"</span><span style="color: #800000;">Service Address is:</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> addr);</span></div><div><span style="color: #000000;"> var client </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AbceServiceClient(</span><span style="color: #800000;">"</span><span style="color: #800000;">NetTcpBinding_IAbcService</span><span style="color: #800000;">"</span><span style="color: #000000;">, addr);</span></div><div><span style="color: #000000;"> </span><span style="color: #0000ff;"> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> client;</span></div><div><span style="color: #000000;"> } </span></div><div><span style="color: #000000;"> </span><span style="color: #0000ff;"> else</span></div><div><span style="color: #000000;"> {</span></div><div><span style="color: #000000;"> var addr </span><span style="color: #000000;">=</span><span style="color: #000000;"> AppContext.ServerHost </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">/AbcService.svc</span><span style="color: #800000;">"</span><span style="color: #000000;">;</span></div><div><span style="color: #0000ff;"> </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> AbcClient(</span><span style="color: #800000;">"</span><span style="color: #800000;">BasicHttpBinding_IAbcService</span><span style="color: #800000;">"</span><span style="color: #000000;">, addr); </span></div><div><span style="color: #000000;"> }</span></div><div><span style="color: #000000;">} <br /></span></div></pre>
</div>
<p> </p>
结果发现,在浏览器中运行使用http绑定都是正常的,而在浏览器外使用tcp下,无法工作.请教,自动选择服务绑定的思路是否可行,我该如何做. </pre>
<pre>附:如果在添加服务的时候,添加的是tcp绑定的服务,然后在配置文件中添加http绑定,结果是反过来,tcp可用,而http不可用.</pre>
<pre>如果同时添加两种服务,那么两个服务的名称空间不一致,无法实现服务工厂.</pre>
一味
|
初学一级
|
园豆:
150
提问于:2010-12-31 12:12