WCF web.config 一般配置
新建一个DeskTopService的Wcf服务
web.config配置如下:(测试可以用)
<?xml version="1.0" encoding="utf-8"?> <configuration> <connectionStrings> <add name="TestConnectionString" connectionString="Data Source=.;Database=test;Uid=test;Pwd=test;" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service behaviorConfiguration="DeskTopService.DeskTopServiceBehavior" name="DeskTopService.DeskTopService"> <endpoint address="" binding="basicHttpBinding" contract="DeskTopService.IDeskTopService" /> <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="DeskTopService.DeskTopServiceBehavior"> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="true"/> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <serviceDebug includeExceptionDetailInFaults="false"/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> </system.serviceModel> </configuration>
客户端 添加服务应用的Wcf地址 如:http://desktopservice.xxxxxx.com/DeskTopService.svc (最好是配置在IIS中,方便测试调用)
客户端web.config配置示例:(这里我只贴上 ServiceModel 节点,实际上也只需要配置这个节点)
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="basicBinding" maxReceivedMessageSize="6553600"> <readerQuotas maxStringContentLength="6553600" /> </binding> <binding name="BasicHttpBinding_IDeskTopService" 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> <netTcpBinding> <binding name="netTcpBinding" maxReceivedMessageSize="6553600"> <readerQuotas maxStringContentLength="6553600" /> </binding> </netTcpBinding> </bindings> <client> <endpoint address="http://desktopservice.xxxxxx.com/DeskTopService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDeskTopService" contract="DeskTopService.IDeskTopService" name="BasicHttpBinding_IDeskTopService" /> </client> </system.serviceModel>
然后就可以在客户端进行引用了
class Test { static void Main() { DeskTopServiceClient client = new DeskTopServiceClient(); // 使用 "client" 变量在服务上调用操作。 // 始终关闭客户端。 client.Close(); } }
完成!!