现在的Windows SDK测试版里对"Host a WCF Service in a Managed Application"这块说的不是太清楚, 希望下面的例子对学习WCF的同学有些参考作用.
使用托管应用程序运行和使用WCF服务首先要定义服务契约(Contract), 这也是所有WCF服务都要做的事情, 这里举个最简单的:
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
}
这个接口文件可以放到一个独立的项目里, 和.Net Remoting一样, Server和Client项目都需要引用它public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
}
在Server项目里,添加一个类, 实现服务接口:
public class Calculator:ICalculator
{
#region ICalculator 成员
public double Add(double n1, double n2)
{
return n1 + n2;
}
#endregion
}
在Server的App.config里加入服务配置:{
#region ICalculator 成员
public double Add(double n1, double n2)
{
return n1 + n2;
}
#endregion
}
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Server.Calculator"> <!--指定服务的实现类-->
<endpoint address="net.tcp://localhost:10102/WcfService"
binding="netTcpBinding"
contract="Interface.ICalculator" > <!--指定服务的契约接口-->
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
服务器端的程序代码:
<configuration>
<system.serviceModel>
<services>
<service name="Server.Calculator"> <!--指定服务的实现类-->
<endpoint address="net.tcp://localhost:10102/WcfService"
binding="netTcpBinding"
contract="Interface.ICalculator" > <!--指定服务的契约接口-->
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
class Program
{
static void Main(string[] args)
{
using (ServiceHost serviceHost = new ServiceHost(typeof(Calculator)))
{
serviceHost.Open();
Console.WriteLine("The service is ready.");
Console.ReadLine();
serviceHost.Close();
}
}
}
{
static void Main(string[] args)
{
using (ServiceHost serviceHost = new ServiceHost(typeof(Calculator)))
{
serviceHost.Open();
Console.WriteLine("The service is ready.");
Console.ReadLine();
serviceHost.Close();
}
}
}
客户端的App.config文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorClient" address="net.tcp://localhost:10102/WcfService"
binding="netTcpBinding"
contract="Interface.ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>
和Server端的差不多, 注意endpoint的name, 在客户端程序中需要用到<configuration>
<system.serviceModel>
<client>
<endpoint name="CalculatorClient" address="net.tcp://localhost:10102/WcfService"
binding="netTcpBinding"
contract="Interface.ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>
客户端的程序代码:
class Program
{
static void Main(string[] args)
{
Console.ReadLine();
using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("CalculatorClient"))
{
ICalculator remoteCalculator = factory.CreateChannel();
Console.WriteLine(remoteCalculator.Add(1, 2));
Console.ReadLine();
}
}
}
OK啦..运行服务器服务, 然后打开客户端, 回车, 一个美丽的得数就会惊现于Console了. 这些东西底层还是.Net Remoting, 做起来用起来都没什么区别, 但代码看上去要比Remoting的华丽得多...而且统一的配置文件很是方便~{
static void Main(string[] args)
{
Console.ReadLine();
using (ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("CalculatorClient"))
{
ICalculator remoteCalculator = factory.CreateChannel();
Console.WriteLine(remoteCalculator.Add(1, 2));
Console.ReadLine();
}
}
}