WCF的创建及其服务配置
1 开发环境VS2010,我们可以通过,“WCF服务库”、“WCF服务应用程序”,这里说“WCF服务应用程序”的方式。
2 如下
①
②
先把项目中的"IService1.cs","ITestWcf.cs"删除掉,然后新建项
在项目中会自动生成 “ITestWcf.cs”,“TestWcf.svc”文件。
“ITestWcf.cs是接口文件,TestWcf实现ITestWcf。
请参照例子接口:
1 /// <summary> 2 /// 程序说明 手机端WCF服务·接口层 3 /// 建立者:杨斌 4 /// 建立日期:2013-09-22 5 /// 修改记录: 6 /// 修改者-修改日期时间: 7 /// 修改内容: 8 /// </summary> 9 using System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using System.Runtime.Serialization; 13 using System.ServiceModel; 14 using System.Text; 15 using PosErp.Model.MobileErpWcfModel; 16 using System.ServiceModel.Web; 17 18 namespace MobileErp.Wcf 19 { 20 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IPosAPI”。 21 [ServiceContract] 22 public interface IPosAPI 23 { 24 [OperationContract] 25 [WebInvoke( 26 UriTemplate = "getversion", 27 BodyStyle = WebMessageBodyStyle.WrappedRequest, 28 ResponseFormat = WebMessageFormat.Json, 29 RequestFormat = WebMessageFormat.Json, 30 Method = "POST")] 31 PosResponse GetVersion(); 32 } 33 }
实现的服务类:
1 /// <summary> 2 /// 程序说明 手机端WCF服务·接口实现层 3 /// 建立者:杨斌 4 /// 建立日期:2013-09-22 5 /// 修改记录: 6 /// 修改者-修改日期时间: 7 /// 修改内容: 8 /// </summary> 9 using System; 10 using System.Collections.Generic; 11 using System.Linq; 12 using System.Runtime.Serialization; 13 using System.ServiceModel; 14 using System.Text; 15 using Newtonsoft.Json; 16 using PosErp.Model.MobileErpWcfModel; 17 18 namespace MobileErp.Wcf 19 { 20 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“PosAPI”。 21 public class PosAPI : IPosAPI 22 { 23 public PosResponse GetVersion() 24 { 25 PosResponse rsp = new PosResponse(); 26 IDictionary<string, string> m1 = new Dictionary<string, string>(); 27 m1.Add("versioncode", "13"); 28 m1.Add("downloadurl", "http://erp.yuqingtea.com/mobileERP.apk"); 29 rsp.Body = JsonConvert.SerializeObject(m1); 30 return rsp; 31 } 32 } 33 }
整个项目完整代码下载
③配置文件的修改。
在项目中 选择 “Web.config”-->编辑WCF配置-->服务-->新建服务-->在bin目录选择生成的服务。
为终结点准备属性(BindingConfiguration配置终结点的通讯协议、BehaviourConfiguration终结点执行方式设置组)。
配置终结点的通讯协议:
终结点执行方式设置组:
配置终结点的属性。
最后的webconfig的代码为:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 4 <system.web> 5 <compilation debug="true" targetFramework="4.0" /> 6 <customErrors mode="Off"/> 7 <!--设置网站编码格式--> 8 <globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN"/> 9 </system.web> 10 <system.serviceModel> 11 <bindings> 12 <webHttpBinding> 13 <binding name="webBinding" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" 14 maxReceivedMessageSize="2147483647"> 15 <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 16 maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 17 </binding> 18 </webHttpBinding> 19 </bindings> 20 <services> 21 <service name="MobileErp.Wcf.PosAPI"> 22 <endpoint address="" behaviorConfiguration="httpBehavior" binding="webHttpBinding" 23 bindingConfiguration="webBinding" contract="MobileErp.Wcf.IPosAPI" /> 24 </service> 25 </services> 26 <behaviors> 27 <endpointBehaviors> 28 <behavior name="httpBehavior"> 29 <webHttp /> 30 </behavior> 31 </endpointBehaviors> 32 <serviceBehaviors> 33 <behavior name=""> 34 <serviceMetadata httpGetEnabled="true" /> 35 <serviceDebug includeExceptionDetailInFaults="false" /> 36 </behavior> 37 </serviceBehaviors> 38 </behaviors> 39 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 40 </system.serviceModel> 41 <system.webServer> 42 <modules runAllManagedModulesForAllRequests="true"/> 43 </system.webServer> 44 45 </configuration>