WCF 新手在这里...我正在尝试使用 NetTcpBinding 自托管 WCF 服务。基于MSDN “how-to” 教程,我已经在代码中完成了所有绑定,然后我将其从 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 服务编写代码?