WCF中的mex端点返回错误400

时间:2018-01-04 10:27:33

标签: .net wcf

我在Microsoft文档的帮助下创建了一个简单的WCF服务 我暴露了两个端点:1。服务端点(/ CalculationService)和
2。服务元数据终点(/ mex)
服务在控制台应用程序中正确托管,我可以从浏览器浏览服务baseAddress和baseAddress?wsdl。
这里的问题是 - 当我尝试浏览endpoint / mex和/ CalculationService时,我收到Bad request 400错误。
这里到底出了什么问题。找到以下托管应用程序的代码。

 static void Main(string[] args)
    {
        // Step 1 of the address configuration procedure: Create a URI to serve as the base address.  
        Uri baseAddress = new Uri("http://localhost:8000/ServiceModelSamples/Service/");
        // Step 2 of the hosting procedure: Create ServiceHost  
        ServiceHost selfHost = new ServiceHost(typeof(Service1), baseAddress);
        try
        {
            // Step 3 of the hosting procedure: Add a service endpoint.  
            // Step 4 of the hosting procedure: Enable metadata exchange. 
            ServiceMetadataBehavior smb = selfHost.Description.Behaviors.Find<ServiceMetadataBehavior>();

            if (smb == null)
                smb = new ServiceMetadataBehavior();
            //ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;

            selfHost.Description.Behaviors.Add(smb);
            selfHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, new BasicHttpBinding(), "mex");


            // Step 5 of the hosting procedure: Start (and then stop) the service.  
            selfHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "CalculatorService");
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.  
            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }
    }


我没有在web.config文件中明确添加任何内容 这是我的WCF服务的web.config文件

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1"/>
  </system.web>
  <system.serviceModel> 
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <!--<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false"/>-->

          <!-- 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>
    <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

1 个答案:

答案 0 :(得分:1)

因为您可以在服务中拥有多个端点,这就是指定它们的重要性。

他们确实在工作。 Metada交换终点用于交换metada。如果客户端应用程序无法访问此端点,您将无法引用该服务。

另一个端点是暴露方法。如果你能够使用它并使用它的方法,那么它也可以正常工作。