如何为 WCF 自托管服务定义配置多个端点?
我有两个 WCF Web API 合同。在此之前,我很高兴能够使用 TestClient。但在我实现第二个端点之后,我必须定义端点(并且无法使用默认端点),之后,我在浏览器中看不到任何内容,或者这条消息说“此 XML 文件似乎没有与以下内容关联的任何样式信息”:它。”当我尝试访问端点地址时。当我尝试配置文件时也是一样的(尽管我不知道如何设置“EnableTestClient = true”)。我真的很感谢任何帮助。
var baseurl = new Uri("http://localhost:7000/api/v1.0");
var config = new HttpConfiguration() { EnableTestClient = true };
config.CreateInstance = (type, context, request) => container.Resolve(type);
var host = new HttpServiceHost(typeof(ServiceAPI), config, baseurl);
host.Description.Behaviors.Add(
new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetUrl = baseurl });
// Add MEX endpoint
//host.AddServiceEndpoint(
// ServiceMetadataBehavior.MexContractName,
// MetadataExchangeBindings.CreateMexHttpBinding(),
// "mex"
//);
//host.AddServiceEndpoint(typeof(IStatAPI), new WebHttpBinding(), "/stat");
//host.AddServiceEndpoint(typeof(IAlarmAPI), new WebHttpBinding(), "/alarm");
host.Faulted += (s, e) => Debug.WriteLine(e);
host.Open();
I have two WCF Web API Contracts. Before this, I was happy that I could use TestClient. But after I implemented the second one I had to define endpoints (and could not use the default one) and after that, either I see nothing in browser or this message saying that "This XML file does not appear to have any style information associated with it." when I try to go to the endpoint address. It is the same when I try the config file (although I do not know how to set "EnableTestClient = true"). I really appreciate any help.
var baseurl = new Uri("http://localhost:7000/api/v1.0");
var config = new HttpConfiguration() { EnableTestClient = true };
config.CreateInstance = (type, context, request) => container.Resolve(type);
var host = new HttpServiceHost(typeof(ServiceAPI), config, baseurl);
host.Description.Behaviors.Add(
new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetUrl = baseurl });
// Add MEX endpoint
//host.AddServiceEndpoint(
// ServiceMetadataBehavior.MexContractName,
// MetadataExchangeBindings.CreateMexHttpBinding(),
// "mex"
//);
//host.AddServiceEndpoint(typeof(IStatAPI), new WebHttpBinding(), "/stat");
//host.AddServiceEndpoint(typeof(IAlarmAPI), new WebHttpBinding(), "/alarm");
host.Faulted += (s, e) => Debug.WriteLine(e);
host.Open();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为应该使用多个端点来公开不同的 API。它们用于公开具有不同绑定的相同合约。
您应该为每个 API 创建一个新主机。不过,您可以在它们之间共享配置。
I don't believe that multiple endpoints should be used to expose different APIs. They are for exposing the same contract with a different binding.
You should create a new host for each API. You can share the config between them though.