C# 如何将方法绑定到自托管WCF Web服务的根?

C# 如何将方法绑定到自托管WCF Web服务的根?,c#,web-services,wcf,C#,Web Services,Wcf,当请求的URL是主机的根目录时,我想从web服务调用一个方法 [ServiceContract] public interface ICalculator { [OperationContract] [WebGet(UriTemplate = "/")] string RootMethod(); [OperationContract] [WebGet] double Add(double x, double y); } 执行浏览添加并按预期返回结果,但当我浏览添加

当请求的URL是主机的根目录时,我想从web服务调用一个方法

[ServiceContract]
public interface ICalculator
{
  [OperationContract]
  [WebGet(UriTemplate = "/")]
  string RootMethod(); 

  [OperationContract]
  [WebGet]
  double Add(double x, double y); 
}
执行浏览添加并按预期返回结果,但当我浏览添加时,不会执行RootMehtod,而是会收到一条消息,告诉我

此服务的元数据发布当前已禁用


如何将一个方法绑定到自托管WCF Web服务的根?

首先,您遇到的错误是因为您尚未将我们的服务配置为公开有关它的任何元数据。为了公开服务的WSDL,我们需要配置服务以提供元信息

现在,您需要如下更新您的运营合同:

[OperationContract]

[WebGet(UriTemplate = "")]
        string baseAddress = "http://" + Environment.MachineName;

        ServiceHost host = new ServiceHost(typeof(TestService), new Uri(baseAddress));

        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITestService), new WebHttpBinding(), "");

        endpoint.Behaviors.Add(new WebHttpBehavior());

        ServiceDebugBehavior debugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();

        debugBehavior.HttpHelpPageEnabled = false;

        debugBehavior.HttpsHelpPageEnabled = false;

        host.Open();
请注意UriTemplate中的差异

之后,您需要像下面这样公开端点:

[OperationContract]

[WebGet(UriTemplate = "")]
        string baseAddress = "http://" + Environment.MachineName;

        ServiceHost host = new ServiceHost(typeof(TestService), new Uri(baseAddress));

        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITestService), new WebHttpBinding(), "");

        endpoint.Behaviors.Add(new WebHttpBehavior());

        ServiceDebugBehavior debugBehavior = host.Description.Behaviors.Find<ServiceDebugBehavior>();

        debugBehavior.HttpHelpPageEnabled = false;

        debugBehavior.HttpsHelpPageEnabled = false;

        host.Open();

请参考此了解更多详细信息,希望对您有所帮助

谢谢@ankit我不需要wsdl元数据,我错过的是debugBehavior.HttpHelpPageEnabled=false;