using System; using System.ServiceModel; using System.Collections.Generic; using System.Runtime.Serialization; namespace Service { [ServiceContract] public interface IUserInfo { [OperationContract] User GetInfo(); } [MessageContract] public class User { [MessageHeader] public string OprationType { get; set; } [MessageHeader] public DateTime OperationTime { get; set; } [MessageBodyMember] public int ID { get; set; } [MessageBodyMember] public string Name { get; set; } [MessageBodyMember] public int Age { get; set; } [MessageBodyMember] public string Nationality { get; set; } } }
UserInfo.cs代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Service { public class UserInfo:IUserInfo { public User GetInfo() { User user = new User(); user.OprationType = "GET"; user.OperationTime = System.DateTime.Now; user.Name = "JACK"; user.Age = 20; user.ID = 1; user.Nationality = "CHINA"; return user; } } }
2. Host:控制台应用程序,服务承载程序。添加对程序集Service的引用,完成以下代码,寄宿服务。Program.cs代码如下:
logs_code_hide('b8e8a6c9-d064-4fc4-a9d5-afb8c13fa1a8',event)" src="/Upload/Images/2015042410/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(UserInfo))) { host.Opened += delegate { Console.WriteLine("服务已经启动,按任意键终止!"); }; host.Open(); Console.Read(); } } } }View Code
App.config代码如下:
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service name="Service.UserInfo" behaviorConfiguration="mexBehavior"> <host> <baseAddresses> <add baseAddress="http://localhost:1234/UserInfo/"/> </baseAddresses> </host> <endpoint address="" binding="wsHttpBinding" contract="Service.IUserInfo" /> <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
我们通过svcutil.exe工具生成客户端代理类和客户端的配置文件,注意,此处生成的客户端代理类型为消息协定类型,所以需要在svcutil.exe的参数后追加/mc才能生成消息协定的客户端代理类。
svcutil.exe是一个命令行工具,位于路径C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin下,我们可以通过命令行运行该工具生成客户端代理类
3. Client:控制台应用程序,客户端调用程序。将生成的UserInfoClient.cs和App.config复制到Client的工程目录下,完成客户端调用代码。Program.cs的代码如下:
using System; namespace Client { class Program { static void Main(string[] args) { UserInfoClient proxy = new UserInfoClient(); User user = proxy.GetInfo(new GetInfoRequest()); Console.WriteLine("{0,-10}{1,-20}{2,-10}{3,-10}{4,-10}{5,-10}", "Type","Time","ID", "Name", "Age", "Nationality"); Console.WriteLine("{0,-10}{1,-20}{2,-10}{3,-10}{4,-10}{5,-10}", user.OprationType.ToString(), user.OperationTime.ToString(), user.ID.ToString(), user.Name.ToString(), user.Age.ToString(), user.Nationality.ToString()); Console.Read(); } } }
程序运行结果如下:
在生成消息客户端代理类时,我们追加了一个/mc的参数,当我们查看客户端代理类UserInfoClient.cs的代码时会发现,客户端生成的消息协定按照服务端消息协定的定义被MessageContractAttribute和MessageHeaderAttribute以及MessageBodyMemberAttribute修饰了。但是操作协定的GetInfo却多出了一个类型为GetInfoRequest参数。所以我们在客户端调用的时候,还要new 一个 GetInfoRequest参数类型,才能调用GetInfo方法。