【发布时间】:2011-05-30 21:17:26
【问题描述】:
WCF 新手在这里...我正在尝试使用 NetTcpBinding 自托管 WCF 服务。基于MSDN "how-to" tutorial我已经在代码中完成了所有的绑定,然后我把它从WsHttpBinding改成了NetTcpBinding,现在看起来是这样的:
var baseAddress = new Uri("net.tcp://localhost:8000/MyWebService");
var selfHost = new ServiceHost(typeof(ConcreteWebService), baseAddress);
try {
var binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
selfHost.AddServiceEndpoint(typeof(IWebService), binding, "TRWebService");
selfHost.Open();
Console.WriteLine("The service is ready at {0}", baseAddress.AbsoluteUri);
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
} catch (CommunicationException ce) {
Console.WriteLine("An exception occurred: {0}", ce.Message);
selfHost.Abort();
}
问题是,教程然后说您必须运行 svcutil.exe 来为客户端生成代理...但是由于我切换到 NetTcpBinding,svcutil 不再工作 - 无法检测到我的服务。我搜索了这个问题,发现 NetTcpBinding 的每个示例都在 app.config 文件中进行设置,而不是在代码中,并且它们都添加了一个名为“Mex”的端点,绑定类型为“mexTcpBinding”。代码中似乎没有任何等价物。
那么,我是否必须更改我的项目以使用 app.config,并放弃基于代码的方法?谁能向我解释 Mex 是什么,我为什么需要它,以及为什么(显然)不能在代码中调用它 - 或者如果可以,如何或为什么不鼓励它?一般来说,什么时候使用 app.config 更好,什么时候为 WCF 服务编写代码?
【问题讨论】:
-
我遵循完全相同的教程并想要回调合约,所以在切换到 tcp 后我遇到了完全相同的问题。
标签: c# wcf nettcpbinding