【问题标题】:Not able to access WSDL even though HTTPGetEnabled is set to true即使 HTTPGetEnabled 设置为 true,也无法访问 WSDL
【发布时间】:2020-07-09 15:28:49
【问题描述】:

我创建了一个 Windows 服务并使用 installutil.exe 安装它。

但即使 HTTPGetEnabled 设置为 true,我也无法访问“MEX”端点。 总是收到错误“此服务的元数据发布当前已禁用。”

你能帮我解决这个问题吗?

下面是我的 App.Config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
      <behaviors>
        <serviceBehaviors>
          <behavior name ="MyBehaviour">
            <serviceMetadata httpGetEnabled="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
        <services>
            <service name="GenerateResult.GenerateResult" behaviorConfiguration="MyBehaviour">
                <endpoint address="http://localhost:5050/GetResultService" binding="basicHttpBinding"
                    bindingConfiguration="" contract="GenerateResult.IGenerateResult" />
                <endpoint address="http://localhost:5050/GetResultService/mex" 
                    binding="mexHttpBinding"
                    bindingConfiguration="" contract="IMetadataExchange" />
        </service>    
    </services>
</system.serviceModel>

【问题讨论】:

    标签: c# wcf wsdl windows-services


    【解决方案1】:

    尽管我们已经设置了serviceMetadata 属性,但我们需要设置一个HTTP 基地址来启用httpGetEnabled 属性。
    请参考以下代码段。

      <system.serviceModel>
        <services>
          <service  name="ConsoleApp3.TestService" behaviorConfiguration="mybeh"> 
            <endpoint address="myservice" binding="basicHttpBinding" contract="ConsoleApp3.ITestService" bindingConfiguration=""></endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" bindingConfiguration=""></endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:21011/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <bindings>
       <basicHttpBinding>
       </basicHttpBinding>
        </bindings>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mybeh">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    

    如果问题仍然存在,请随时告诉我。
    已更新。

    static void Main(string[] args)
            {
                Uri uri = new Uri("http://localhost:21011");
                BasicHttpBinding binding = new BasicHttpBinding();
                using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
                {
                    sh.AddServiceEndpoint(typeof(IService), binding, "");
                    ServiceMetadataBehavior smb;
                    smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                    if (smb == null)
                    {
                        smb = new ServiceMetadataBehavior()
                        {
                            HttpGetEnabled = true
                        };
                        sh.Description.Behaviors.Add(smb);
                    }
                    Binding mexbinding = MetadataExchangeBindings.CreateMexHttpBinding();
                    sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
                    sh.Opened += delegate
                    {
                        Console.WriteLine("Service is ready");
                    };
                    sh.Closed += delegate
                    {
                        Console.WriteLine("Service is clsoed");
                    };
                    sh.Open();
                    Console.ReadLine();
                    //pause
                    sh.Close();
                    Console.ReadLine();
                }
    

    【讨论】:

    • 嗨亚伯拉罕...感谢您的回复但是,我仍然面临同样的问题。下面是 Windows 服务的 OnStart() 方法。 protected override void OnStart(string[] args) { sh = new ServiceHost(typeof(GetResultService), new Uri("localhost:5050/GetResultService")); sh.Open(); } 这在禁用元数据方面有什么作用吗?跨度>
    • 可以,如果你使用Console Application来托管服务,请参考上面的代码。
    • 谢谢,Abraham... 上面的代码工作正常,我可以访问 wsdl,但我有一个查询。当我尝试通过命令式方式(通过代码)添加配置时,我能够访问 WSDL,但在将配置添加到 app.config 文件时遇到问题
    • 我没有明白你的意思,请继续分享你的情况。当我们没有以编程方式设置服务配置时,在 OnStart 方法中创建的服务主机将自动采用appconfig 的设置。如果有帮助,请将回复标记为答案。
    猜你喜欢
    • 2021-08-20
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    相关资源
    最近更新 更多