c# – 使用wsHttpBinding配置WCF服务

我在使用
wsHttpBinding和Https基地址设置WCF服务时遇到问题.

真正的问题是在客户端测试WCF中定义mex时:

错误:无法从中获取元数据
https://localhost:8722/Design_Time_Addresses/_20180420_WcfServiceLibraryTest/Service1/mex
如果这是您有权访问的Windows(R)Communication Foundation服务,请检查您是否已在指定地址启用元数据发布.
有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455中的MSDN文档.

App.config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="NewBehavior0"> <!-- Error: Cannot optain metadata -->
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>

            <behavior name="NewBehavior1"> <!-- Error: Cannot Find Cert -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceCredentials>
                <serviceCertificate  findValue="BadThumbprint" x509FindType="FindByThumbprint"  storeLocation="LocalMachine" storeName="My"/>
              </serviceCredentials>
            </behavior>

            <behavior name="NewBehavior2"> <!-- Error: Cannot optain metadata -->
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
              <serviceCredentials>
                <serviceCertificate  findValue="GoodThumbprint" x509FindType="FindByThumbprint"  storeLocation="LocalMachine" storeName="My"/>
              </serviceCredentials>
            </behavior>             
        </serviceBehaviors>
    </behaviors>

    <bindings>
        <wsHttpBinding>
            <binding name="wsHTTPBindingConf">
                <security mode="Transport">
                    <!--<message clientCredentialType="None" />-->
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>

    <services>
        <service behaviorConfiguration="NewBehavior0" name="_20180420_WcfServiceLibraryTest.Service1">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHTTPBindingConf"
          name="WCFEndpoint" contract="_20180420_WcfServiceLibraryTest.IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration=""
          name="mexEndPoint" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="https://localhost:8722/Design_Time_Addresses/_20180420_WcfServiceLibraryTest/Service1/" />
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>

阅读SO并尝试我发现的每件事我尝试了很多配置.但没有一个显示出值得一提的进步.看起来更像,我不知道.
使用wsHttpBinding使最基本的W工作(在SoapUi中编译可测试)的正确配置是什么?

最佳答案 我想这可能是因为mex bindingConfiguration =“”被设置为一个空字符串?

尝试删除它,或将其设置为绑定.

如果默认的mexHttpBinding不起作用,请尝试将其更改为wsHttpBinding.这是一个可能有用的示例Custom Secure Metadata Endpoint.它说:

In many of the other samples, the metadata endpoint uses the default mexHttpBinding, which is not secure. Here the metadata is secured using WSHttpBinding with Message security. In order for metadata clients to retrieve this metadata, they must be configured with a matching binding.

需要为不同的绑定配置WCF测试客户端,但我认为它会自动配置自己.

<services>   
    <service name="Microsoft.ServiceModel.Samples.CalculatorService"  
             behaviorConfiguration="CalculatorServiceBehavior">  
     <!-- use base address provided by host -->  
     <endpoint address=""  
       binding="wsHttpBinding"  
       bindingConfiguration="Binding2"  
       contract="Microsoft.ServiceModel.Samples.ICalculator" />  
     <endpoint address="mex"  
       binding="wsHttpBinding"  
       bindingConfiguration="Binding1"  
       contract="IMetadataExchange" />  
     </service>  
 </services>  
 <bindings>  
   <wsHttpBinding>  
     <binding name="Binding1">  
       <security mode="Message">  
         <message clientCredentialType="Certificate" />  
       </security>  
     </binding>  
     <binding name="Binding2">  
       <reliableSession inactivityTimeout="00:01:00" enabled="true" />  
       <security mode="Message">  
         <message clientCredentialType="Certificate" />  
       </security>  
     </binding>  
   </wsHttpBinding>  
 </bindings>
点赞