WCF配置心得
<P>根据蒋金楠老师的博文所说的, WCF的终结点有三个要素组成,分别是地址(Address)、绑定(Binding)和契约(Contract),简记可写成Endpoint = ABC。<BR> 地址:地址决定了服务的位置,解决了服务寻址的问题。<BR> 绑定:绑定实现了通信的所有细节,包括网络传输、消息编码,以及其他为实现某种功能对消息进行的相应处理。绑定的类型包括BasicHttpBinding、WsHttpBinding、NetTcpBinding等。<BR> 契约:契约是对服务操作的抽象,也是对消息交换模式以及消息结构的定义。<BR> 以上这些内容摘抄自蒋老师的博文。理解的这些对配置WCF很有帮助。<BR>那下面就一步步来配置一个WCF。<br><br>首先是服务端,<BR>一个WCF的核心是终结点,那么先把终结点写列出来,<BR><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code86451"><BR><services><BR> <service name="BLL.Logic" behaviorConfiguration="te"><BR> <host><BR> <baseAddresses><BR> <add baseAddress="http://localhost:9091/logicService"/><BR> </baseAddresses><BR> </host><BR> <endpoint address="" binding="ws2007HttpBinding" contract="BLL.ILogic" bindingConfiguration="transportWS2007HttpBinding" /><BR> </service><BR> </services><BR></div><BR> 从<endpoint>几个属性address(地址) binding(绑定),Contract(契约),这几个属性正是上面所说的"ABC" 注意一下 binding里填的是BasicHttpBinding、WsHttpBinding、NetTcpBinding这些值,而确切使用哪一个binding呢,就需要在bindingConfiguration中设置,值是使用的<binding>的name值。contract项目中contract的契约接口的完全限定名,这里关于binding的配置接下来会介绍。address没填值,这里在<host>中已经给定了一个地址了。<BR> 介绍完<endpoint>,再看看<endpoint>外面的。<endpoint>包含在<services>的<service>下,这里的<serivces>是一个集合,里面可以包含多个服务,每个服务都会有特定的命名(name),而name则是项目里头实现契约(Contract)的服务(Service)的类的完全限定名。这里对servicebehavior进行了一些设置,具体的内容在名为te的<servicebehavior>中。<BR>既然上面有配置有涉及到binding和behavior,下面则分别对两者进行配置。<BR><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code78165"><BR><bindings><BR> <ws2007HttpBinding><BR> <binding name="transportWS2007HttpBinding" maxReceivedMessageSize="2147483647" maxBufferPoolSize="2147483647"><BR> <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/><BR> <security mode="Message"><BR> <transport clientCredentialType="None"/><BR> </security><BR> </binding><BR> </ws2007HttpBinding></P><P> <basicHttpBinding><BR> <binding name="newBinding" maxBufferPoolSize="21474835647" maxReceivedMessageSize="2147483647" messageEncoding="Text"><BR> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/><BR> </binding><BR> </basicHttpBinding><BR></bindings><BR></div><BR> bindings这部分和services一样,也是一个集合,里面包含着各种类型的binding,例如在<ws2007HttpBinding>里面的<binding>才是确切的某一个binding, <endpoint>使用时,bindingConfiguration的名称要写对外,binding的类型也不能错。<binding>里面的子节点和属性就不再一一介绍了,若是要通过WCF传输比较大的数据时,要在binding的属性和<readerQuotas>设置一下。<BR><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code56139"><BR> <behaviors><BR> <serviceBehaviors><BR> <behavior name="te"><BR> <serviceMetadata httpGetEnabled="true"/><BR> </behavior><BR> </serviceBehaviors><BR> </behaviors><BR></div><BR>最后到behaviors了。同理,behaviors也是一个集合,里面有两种类型,一种是serviceBehaviors,用于配置service的;另一种是endpointBehaviors,用于配置endpoint的。这两种类型都是一个集合,子节点<behavior>是它们的子项,以name来区分各个behavior,至于里面有什么属性和子项也不多说了,使用时在相应的service或endpoint的behaviorConfiguration属性填上behavior的name值就行了。<BR>服务端的配置就唠叨到这里,下面到客户端的。<BR><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code95660"><BR><client><BR> <endpoint address="http://localhost:9091/logicService" binding="ws2007HttpBinding"<BR> bindingConfiguration="WS2007HttpBinding_ILogic" contract="Proxy.ILogic"<BR> name="WS2007HttpBinding_ILogic"><BR> </endpoint><BR> </client><BR></div><BR>首先也是是终结点,客户端的终结点放在client里,里面也是有"ABC",这里的address一定要与服务端配置的一样,否则找不到相应的服务的。binding的类型也要与服务端的一样,contract则是用svcutil或其他工具生成的代码里的那个类的完全限定名。<BR><div class="codetitle"><span><U>复制代码</U></span> 代码如下:</div><div class="codebody" id="code77097"><BR><ws2007HttpBinding><BR> <binding name="WS2007HttpBinding_ILogic" closeTimeout="00:01:00"<BR> openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"<BR> bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"<BR> maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"<BR> messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"<BR> allowCookies="false"><BR> <readerQuotas maxDepth="32" maxStringContentLength="1024" maxArrayLength="2147483647"<BR> maxBytesPerRead="4096" maxNameTableCharCount="16384" /><BR> <reliableSession ordered="true" inactivityTimeout="00:10:00"<BR> enabled="false" /><BR> </binding><BR> </ws2007HttpBinding><BR></div><BR>另一个还要提的是这个binding,客户端的binding比服务端的要配置多一点东西closeTimeout,openTimeout,receiveTimeout 大致与服务端一样。<BR>另外若要传输比较的大数据时,可以按我这样来配,其实这个配置已经适用于传输几M的图片。由于是个入门者,很多东西的理解还不够透彻,以上有说错的还请各位批评指出。谢谢!</P>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>C#计算器编写代码</li><li>C#实现Winform版计算器</li><li>C#日历样式的下拉式计算器实例讲解</li><li>c#入门之实现简易存款利息计算器示例</li><li>C#编写的windows计算器的实例代码</li><li>C# 一个WCF简单实例</li><li>WinForm窗体调用WCF服务窗体卡死问题</li><li>C# WCF简单入门图文教程(VS2010版)</li><li>总结C#动态调用WCF接口的两种方法</li><li>WCF实现的计算器功能实例</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]