WCF服务入门
1.新建类库Contract,类库内创建类ICalu.此项目为服务契约,为客户端访问提供接口。
[ServiceContract] public interface ICalu { [OperationContract]
[FaultContract(typeof(WCFException))] string DoWork(int i); }
也可以定义一些数据契约
如:
[DataContract] public class WCFException { private string _operation; private string _errorMessage; /// <summary> /// 自定义WCF错误包的构造函数 /// </summary> /// <param name="operation"></param> /// <param name="errorMessage"></param> public WCFException(string operation, string errorMessage) { this.Operation = operation; this.ErrorMessage = errorMessage; } /// <summary> /// 产生错误的操作信息 /// </summary> [DataMember] public string Operation { get { return _operation; } set { _operation = value; } } /// <summary> /// 产生的异常信息 /// </summary> [DataMember] public string ErrorMessage { get { return _errorMessage; } set { _errorMessage = value; } } }
2.新建类库项目Services。实现上面的接口。
public class Calu : ICalu { public void DoWork() { } }
3.建立一个空的web项目Host。承载WCF服务。
<%@ ServiceHost Language="C#" Debug="true" Service="Services.Calu" %>
新建名为Calu的WCF服务,删除Calu.svc.cs文件和ICalu.cs。打开Calu.svc,删除后面的codebihind,修改 Service="Services.Calu" 为服务实现的类。
此项目引用上面的两个项目。
4.修改web.config
<system.serviceModel> <!--<behaviors><serviceBehaviors><behavior>--> <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <!--<serviceMetadata httpGetEnabled="true"/>--> <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> <!--<serviceDebug includeExceptionDetailInFaults="True"/></behavior></serviceBehaviors></behaviors>--> <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />--> <!--数据绑定协议--> <!--<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />--> <bindings> <wsHttpBinding> <binding name="wdhb" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" maxReceivedMessageSize="2147483647" maxBufferPoolSize="655360000"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> <security mode="None"/> </binding> </wsHttpBinding> </bindings> <!--数据传输协议--> <behaviors> <serviceBehaviors> <behavior name="bbhr"> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> <dataContractSerializer maxItemsInObjectGraph="2147483647" /> </behavior> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="bbhr" name="Services.UserService"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wdhb" contract="Contract.IUserService" /> </service> <service behaviorConfiguration="bbhr" name="Services.Calu"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wdhb" contract="Contract.ICalu" /> </service> </services> </system.serviceModel>
5.新建控制台客户端,引用服务即可。
源代码:https://files.cnblogs.com/adebayors/Contract.7z