wcf通过xml文件配置终结点什么的感觉有点小麻烦,个人还是觉得用代码形式配置比较好,当然在发布的时候可能会比较麻烦,需要重新编译。。。
下面将wcf service寄宿在控制台应用程序中并配置终结点:
|
1
2
3
4
5
6
7
8
9
10
11 |
using
(var
host = new
ServiceHost(typeof(Service1), { host.AddServiceEndpoint(typeof(IService1), new
BasicHttpBinding(), ""); host.Opened += delegate { Console.WriteLine("服务已经启动"); }; host.Open(); Console.Read(); } |
客服端代理服务:
|
1
2
3
4
5
6
7
8
9
10
11 |
using
(var
factory = new
ChannelFactory<IService1>(new
BasicHttpBinding(), { IService1 channelproxy = factory.CreateChannel(); using
(channelproxy as
IDisposable) { Console.WriteLine(channelproxy.GetData(55555)); } Console.Read(); } |
注意宿主配置的终结点必须和客服端配置的终结点地址要保持一致,否则无法找到终结点而报异常。
原文地址:http://www.cnblogs.com/objectboy/p/3767312.html