前言:
操作协定的属性:
OperationContract修饰的操作协定具有以下参数:
WCF操作协定示例:
设置了操作契约Action(设置请求消息的 WS-Addressing 操作)和ReplyAction(设置用于该操作答复消息的 SOAP 操作的值)
ISampleMethod.cs的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace Service { [ServiceContract(Name = "SampleMethodContract", Namespace = "http://wangweimutou.SampleMethodContract", SessionMode=SessionMode.Required)] public interface ISampleMethod { /// <summary> /// IsInitiating默认值为true,IsTerminating默认值为false /// </summary> /// <param name="msg"></param> [OperationContract(Name="OCMethodOne", AsyncPattern=false, IsInitiating=true, IsTerminating=false, Action = "http://wangweimutou.SampleMethodContract/RequestMethodOne", ReplyAction = "http://wangweimutou.SampleMethodContract/ResponseMethodOne")] string MethodOne(string msg); [OperationContract(Name = "OCMethodTwo", AsyncPattern = false, IsInitiating = true, IsTerminating = false, Action = "http://wangweimutou.SampleMethodContract/RequestMethodTwo", ReplyAction = "http://wangweimutou.SampleMethodContract/ResponseMethodTwo")] string MethodTwo(string msg); [OperationContract(Name = "OCMethodThree", AsyncPattern = true, IsInitiating = true, IsTerminating = false, Action = "http://wangweimutou.SampleMethodContract/RequestMethodThree", ReplyAction = "http://wangweimutou.SampleMethodContract/ResponseMethodThree")] IAsyncResult BeginMethodThree(string msg, AsyncCallback callback, object asyncState); string EndMethodThree(IAsyncResult result); } }
SampleMethod.cs的代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.ServiceModel; namespace Service { public class SampleMethod :ISampleMethod { public string MethodOne(string msg) { return "You called MethodOne return message is: " + msg; } public string MethodTwo(string msg) { return "You called MethodTwo return message is: " + msg; } public IAsyncResult BeginMethodThree(string msg, AsyncCallback callback, object asyncState) { return new CompletedAsyncResult<string>(msg); } public string EndMethodThree(IAsyncResult r) { CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>; return "You called MethodThree return message is: " + result.Data; } } class CompletedAsyncResult<T> : IAsyncResult { T data; public CompletedAsyncResult(T data) { this.data = data; } public T Data { get { return data; } } #region IAsyncResult Members public object AsyncState { get { return (object)data; } } public WaitHandle AsyncWaitHandle { get { throw new Exception("The method or operation is not implemented."); } } public bool CompletedSynchronously { get { return true; } } public bool IsCompleted { get { return true; } } #endregion } }
2. Host:控制台应用程序,服务承载程序。添加对Service程序集的引用,实现以下代码。Program.cs的代码如下:
logs_code_hide('9984514f-c868-403a-8e97-986e78726e4d',event)" src="/Upload/Images/2015041510/2B1B950FA3DF188F.gif" alt="" />using System; using System.Collections.Generic; using System.Linq; using System.Text; using Service; using System.ServiceModel; namespace Host { class Program { static void Main(string[] args) { using (ServiceHost host = new ServiceHost(typeof(SampleMethod))) { host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); }; host.Open(); Console.Read(); } } } }View Code
App.config代码如下:
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service name="Service.SampleMethod" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1234/SampleMethod/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.ISampleMethod" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>View Code
3. Client:控制台应用程序。客户端应用程序,启动Host服务承载程序,添加对服务地址http://localhost:1234/SampleMethod/引用后,将命名空间设置为ServiceRef,
勾选生成异步操作复选框,生成异步客户端代理类(参照WCF初探-11:WCF客户端异步调用服务),我们就可以对程序进行同步和异步的调用了。Program.cs代码如下:
运行程序,结果显示如下:
从结果中,我们可以看到,程序停止了对MethodTwo和MethodThree的调用,这是因为MethodOne的IsTerminating设置为true,所以客户端代理在调用完MethodOne
后就关闭了对服务器会话的支持。接下来,我们将调用MethodOne的代码注释掉,编译Client后再次运行,会得到一下结果:
由于我们将MethodTwo的IsInitiating设置为了false,导致服务器会话没有启动,所以服务调用失败。接下来,我们将MethodOne、MethodTwo、MethodThree三个操作
契约的IsInitiating和IsTerminating分别设置为true和false,也就是设置为默认值,重新编译程序后,运行客户端我们可以看到如下结果:
接下来,我们再来查看一下操作契约设置的属性值,从上面的客户端程序代码可以看出,服务契约和操作契约的方法和名称都改成了设定值,如MethodOne变成了OCMethodOne。
打开客户端测试程序,添加对服务地址的引用后,我们就可以看到消息的请求和响应,如观察到MethodOne调用的结果如下:
总结:
WCF初探-3:WCF消息交换模式之单向模式
WCF初探-5:WCF消息交换模式之双工通讯(Duplex)
WCF初探-13:WCF客户端为双工服务创建回调对象
WCF初探-11:WCF客户端异步调用服务