标签:
using System.ServiceModel; namespace Service{ [ServiceContract(SessionMode=SessionMode.Required)] public interface ISampleMethod{ [OperationContract] string MethodOne(string msg); [OperationContract] string MethodTwo(string msg); [OperationContract] int GetInstanceId(); //获取服务实例ID [OperationContract] int GetOperationCount(); //获取调用操作方法的计数器 }}
SampleMethod.cs的代码如下:
using System.ServiceModel; namespace Service{ [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] public class SampleMethod:ISampleMethod { static int instanceCount; int instanceId; int operationCount; public SampleMethod() { instanceCount++; instanceId = instanceCount; } public string MethodOne(string msg) { operationCount++; return "You called MethodOne return message is: " + msg; } public string MethodTwo(string msg) { operationCount++; return "You called MethodTwo return message is: " + msg; } public int GetInstanceId() { return instanceId; } public int GetOperationCount() { return operationCount; } } }
寄宿服务后,使用svcutil.exe生成客户端代理类和配置文件,客户端参考代码如下:
class Program{ static void Main(string[] args){ SampleMethodClient client1 = new SampleMethodClient(); CallMethod(client1); SampleMethodClient client2 = new SampleMethodClient(); CallMethod(client2); Console.Read(); } static void CallMethod(SampleMethodClient client){ Console.WriteLine(client.MethodOne("MethodOne")); Console.WriteLine("InstanceId:{0},OperationCount:{1}", client.GetInstanceId(), client.GetOperationCount()); Console.WriteLine(client.MethodTwo("MethodTwo")); Console.WriteLine("InstanceId:{0},OperationCount:{1}", client.GetInstanceId(), client.GetOperationCount()); } }
运行结果如下:
标签:
原文地址:http://www.cnblogs.com/wangweimutou/p/4517951.html