从WCF中的MEX/WSDL隐藏REST端点

Way*_*neC 6 rest wcf soap wsdl

我有一个WCF服务,每个服务都有REST和SOAP端点.这与本文类似地实现:WCF服务的REST/SOAP端点,其配置类似于以下内容:

<services>
  <service name="TestService">
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="rest" binding="webHttpBinding" contract="ITestService"/>
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

问题是REST端点在结果WSDL中显示为附加端口和绑定.

有没有办法阻止REST端点包含在WSDL中?

Way*_*neC 1

找到了一种使用IWsdlExportExtension. 可能有一种更强大/可重用的方法来执行此操作,但此解决方案要求所有 REST 端点的约定都命名为“REST”。以下是附加到所有 REST 端点的端点行为的相关部分:

public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
    // Remove all REST references (binding & port) from SOAP WSDL
    foreach (ServiceDescription wsdl in exporter.GeneratedWsdlDocuments)
    {
        // Remove REST bindings
        foreach (Binding binding in wsdl.Bindings)
        {
            if (binding.Name == "REST")
            {
                wsdl.Bindings.Remove(binding);
                break;
            }
        }

        // Remove REST ports
        foreach (Service service in wsdl.Services)
        {
            foreach (Port port in service.Ports)
            {
                if (port.Name == "REST")
                {
                    service.Ports.Remove(port);
                    break;
                }
            }
        }
    }  
}
Run Code Online (Sandbox Code Playgroud)