作者:jiankunking 出处:http://blog.csdn.net/jiankunking
1、搭建IIS(具体步骤略)
2、服务契约如下:namespace JianKunKing.NewVersion.Service { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“NewVersionService”。 //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class NewVersionService : INewVersionService { NewVersionManager newVersionManager = new NewVersionManager(); public string GetHello() { return newVersionManager.GetHello(); } } }
3、WCF客户端引用服务:
4、客户端调用
重点来了:
客户端配置文件:
<?xml version="1.0"?> <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_INewVersionService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://192.168.XX.XX/JianKunKingServer/NewVersionService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_INewVersionService" contract="NewVersionClient.INewVersionService" name="BasicHttpBinding_INewVersionService" /> </client> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
WCF服务客户端配置文件的契约由以下两部分组成:
1、Client的服务引用
2、WCF服务接口(就是平时说的WCF契约)
服务端配置文件Web.config:
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="MyServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" > <readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> </binding> </basicHttpBinding> </bindings> <services> <service name="JianKunKing.NewVersion.Service.NewVersionService"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" contract="JianKunKing.NewVersion.Service.INewVersionService"> </endpoint> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
WCF服务实现多个契约的时候 :
public class RightManagementServer : IRightManagementServer,IUserRightService服务端config配置:
<service name="JianKunKing.Test.Service.RightManagementServer"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" contract="JianKunKing.Test.Service.IRightManagementServer"> </endpoint> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="MyServiceBinding" contract="JianKunKing.Test.Service.IUserRightService"> </endpoint> </service>
WCF配置演示源码:点击打开链接
WCF 配置服务介绍:点击打开链接
在一个服务器端A调用另一个服务端B的Client,比如:
服务器A是搜索引擎服务器,服务端B是数据库服务器(这两个服务器可以是物理上的一台机器也可以是物理上的两台机器),这两个服务器都有自己WCF的服务端及Client端,在搜索引擎服务器的一部分操作需要操作操作数据库,此时需要调用数据库服务器的Client来操作数据,搜索引擎服务器的具体配置如下:
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="JianKunKingSystemDBConnenction" connectionString="Data Source=127.0.0.1;Initial Catalog=JianKunKingDBtest;Persist Security Info=True;User ID=a;Password=a" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="JianKunKingServiceBinding" closeTimeout="10:01:00" openTimeout="10:01:00" receiveTimeout="10:10:00" sendTimeout="10:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> <security mode="None"> <transport clientCredentialType="None" proxyCredentialType="None" realm="" /> <message clientCredentialType="UserName" algorithmSuite="Default" /> </security> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://127.0.0.1:8088/JianKunKingService/SearchEngineDataBaseService.svc" binding="basicHttpBinding" bindingConfiguration="JianKunKingServiceBinding" contract="DataBaseService.ISearchEngineDataBaseService" /> </client> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="true" /> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <directoryBrowse enabled="true" /> </system.webServer> </configuration>