IUserInfo.cs代码如下:
using System.ServiceModel;
using System.Runtime.Serialization;
using System;
namespace Service
{
[ServiceContract]
public interface IUserInfo
{
[OperationContract]
Person GetInfo(int id,string name);
[OperationContract]
Person GetInfoEx(int id, string name);
}
[DataContract]
[KnownType(typeof(User))]
public class Person
{
[DataMember]
public int ID { get; set; }
[DataMember]
public string Name { get; set; }
public Person(int id, string name)
{
this.ID = id;
this.Name = name;
}
}
[DataContract]
public class User:Person
{
public User(int id, string name): base(id,name){}
[DataMember]
public string SayHello
{
get { return "Hello:" + Name; }
set { throw new NotImplementedException(); }
}
}
}
UserInfo.cs代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Service
{
public class UserInfo:IUserInfo
{
public Person GetInfo(int id, string name)
{
return new Person(id, name);
}
public Person GetInfoEx(int id, string name)
{
return new User(id, name);
}
}
}
2. Host:控制台应用程序,服务承载程序。添加对程序集Service的引用,完成以下代码,寄宿服务。Program.cs代码如下:
logs_code_hide('e1681226-d415-4ada-a0ff-4d477b947088',event)" src="/Upload/Images/2015042212/2B1B950FA3DF188F.gif" alt="" />using System; using System.ServiceModel; using Service; 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是一个命令行工具,位于路径C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin下,我们可以通过命令行运行该工具生成客户端代理类
3. Client:控制台应用程序,客户端调用程序。将生成的UserInfoClient.cs和App.config复制到Client的工程目录下,完成客户端调用代码。Program.cs的代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Service;
namespace Client
{
class Program
{
static void Main(string[] args)
{
try
{
UserInfoClient proxy = new UserInfoClient();
Person P1 = proxy.GetInfo(1,"JACK");
Person P2 = proxy.GetInfoEx(2, "TOM");
Console.WriteLine("{0,-10}{1,-20}", "ID", "Message");
Console.WriteLine("{0,-10}{1,-20}", P1.ID, P1.Name);
if (P2 is User)
{
Console.WriteLine("{0,-10}{1,-20}", ((User)P2).ID,((User)P2).SayHello);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
}
}
4. 程序运行效果如下: