WCF 基础

时间:2022-06-27 22:03:18

WCF 基础

ServiceModel 配置元素

WCF 基础

Binding 配置元素:

WCF 基础

客户端Web.config:

<?xml version="1.0" encoding="utf-8"?>

<configuration>

<system.serviceModel>

<!-- 描述咋样通信,如:编码、传输协议、安全-->
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IUser" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>

<!--设定匹配端点-->
<client>
<endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser"
name="BasicHttpBinding_IUser" />
</client>
</system.serviceModel>
</configuration>

服务端Web.config代码

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
<serviceMetadata httpGetEnabled="true"/>
<!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>

<!--匹配所有相同端点的客户端请求:就是A,B,C必须一致-->

<services>
<service name="WCFService">
<endpoint address="http://localhost/User.svc" binding="basicHttpBinding"
contract="WCFService.IUser" />
</service>
</services>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

</configuration>

//客户端使用

using (ChannelFactory<Ixxx> channelFactory = new ChannelFactory<IEmployees>(endpointConfigName))
{
               
               Ixxx proxy = channelFactory.CreateChannel();
         //dosomething...
}