WCF-无法通过浏览器访问MEX

时间:2018-08-02 07:23:48

标签: c# wcf wsdl

我刚刚使用几乎默认的配置创建了新项目“ WCF服务库”:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
    <membership defaultProvider="ClientAuthenticationMembershipProvider">
      <providers>
        <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
    <roleManager defaultProvider="ClientRoleProvider" enabled="true">
      <providers>
        <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
      </providers>
    </roleManager>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
            <!--<add baseAddress="net.tcp://localhost:8734/Design_Time_Addresses/WcfServiceLibrary1/Service1/" /> -->
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" contract="WcfServiceLibrary1.IService1">
          <identity><dns value="localhost" /></identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

它在WcfSvcHost中完美运行,但是当我尝试通过浏览器(Edge或Firefox)访问http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/mex时,出现HTTP错误400。

WCF测试客户端可以很好地使用该URI。

我试图在行为标签中添加“名称”属性,然后在服务标签中使用该名称-并没有帮助。

令人惊讶的是,只有在注释以下字符串时,我才能修复它:

<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

当WCF测试客户端中的地址更改为:http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1 ?wsdl

,此URI在浏览器中可以正常工作。但是我认为这不是正确的方法。

我在Windows 10 x64计算机上使用Visual Studio 2015和.Net Framework 4.5.2。

非常感谢您的帮助。

UPD。只是要清楚一点:我想在浏览器中看到我的服务页面(以“您已创建服务”文本开头)。当我删除“ mex”端点时,我只能看到.wsdl文件-这不是我想要的。

1 个答案:

答案 0 :(得分:0)

刚刚找到答案-正确的URI是: http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/ (末尾没有“ mex”)。