wcf使用入门学习笔记
1.服务端创建wcf服务端程序,
2.由生成工具生成代理类
3.将代理类提供给客户端使用
服务端程序由三部分组成
1.接口
2.实现接口
3.宿主程序
客户端调用方法
IService ws = new ServiceClient(new NetTcpBinding(), new EndpointAddress("net.tcp://127.0.0.1:12345"));
宿主程序代码:
using (ServiceHost host = new ServiceHost(typeof(Service), new Uri("net.tcp://127.0.0.1:12345")))
{
NetTcpBinding bind = new NetTcpBinding();
host.AddServiceEndpoint(typeof(IService), bind, "");
//下面我们添加一个用于元数据交换的终结点
BindingElement metaBind = new TcpTransportBindingElement();
CustomBinding tcpBind = new CustomBinding(metaBind);
ServiceMetadataBehavior metaBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (metaBehavior == null)
{
metaBehavior = new ServiceMetadataBehavior();
host.Description.Behaviors.Add(metaBehavior);
}
host.AddServiceEndpoint(typeof(IMetadataExchange), tcpBind, "MEX");
host.Open();
Console.WriteLine("服务已经启动!");
Console.Read();
}
生成代理类方法如下: