WCF对我来说既陌生又熟悉,陌生是因为没怎么接触过,熟悉是听得太多,今天抽出点时间看了一下WCF,并且自己也写了一WCF的小程序以及调用WCF。步骤为:
1.创建一个解决方案WCF,和一个控制台项目WCFTestService(这就就是WCF服务)
2.配置WCF服务的App.Config
3.写WCF服务代码
4.新建项目WCFTestClient(客户端项目,用来测试访问服务)
5.写客户端代码
6.执行访问服务
下面详细说明每步的操作以及代码编写:
1.创建一个解决方案WCF,和一个控制台项目WCFTestService(这就就是WCF服务)
这一步是基础,肯定都会创建,就不在累赘。此处省略一万字··········
2.配置WCF服务的App.Config
首先我先贴上真个App.Config的内容
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> <standardEndpoints> <webHttpEndpoint> <standardEndpoint name="" maxReceivedMessageSize="300000000" defaultOutgoingResponseFormat="Json" helpEnabled="true" automaticFormatSelectionEnabled="true"> <readerQuotas maxArrayLength="30000000" /> </standardEndpoint> </webHttpEndpoint> </standardEndpoints> <bindings> <webHttpBinding> <binding name="webHttpBindingConfig" sendTimeout="00:10:00" bypassProxyOnLocal="false" maxReceivedMessageSize="2147483646"> <readerQuotas maxStringContentLength="2147483646" maxArrayLength="2147483646" /> </binding> </webHttpBinding> </bindings> <services> <!-- 注意: 服务名称必须与服务实现的名称相匹配。 --> <service name="WCFTestService.DbApiInfo" behaviorConfiguration="RestServiceBehavior"> <!-- 注意: 服务必须有一个 http 基址以便添加此终结点。 --> <host> <baseAddresses> <add baseAddress="http://localhost:8888/Service/DbApiInfo" /> </baseAddresses> </host> <!-- 添加下列终结点。 --> <endpoint address="" behaviorConfiguration="RestEndpointBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingConfig" contract="WCFTestService.IApiInfo"> <identity> <dns value="Localhost" /> </identity> </endpoint> </service> <!-- 注意: 服务名称必须与服务实现的名称相匹配。 --> <service name="WCFTestService.DbUpLoad" behaviorConfiguration="RestServiceBehavior"> <!--注意: 服务必须有一个 http 基址以便添加此终结点。--> <host> <baseAddresses> <add baseAddress="http://localhost:8888/Service/DbUpLoad" /> </baseAddresses> </host> <!--添加下列终结点。--> <endpoint address="" behaviorConfiguration="RestEndpointBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingConfig" contract="WCFTestService.IUpLoad"> <identity> <dns value="Localhost" /> </identity> </endpoint> </service> </services> <behaviors> <endpointBehaviors> <behavior name="RestEndpointBehavior"> <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="RestServiceBehavior"> <!-- 将下列元素添加到服务行为配置中。 --> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>