16

我正在使用 VSTS 2008 + C# + .NET 3.0。我正在使用自托管的 WCF 服务。执行以下语句时,出现以下“未找到绑定”错误。我已经发布了我的整个 app.config 文件,有什么想法有什么问题吗?

ServiceHost host = new ServiceHost(typeof(MyWCFService));

错误信息:

找不到与绑定 MetadataExchangeHttpBinding 的终结点的方案 http 匹配的基地址。注册的基地址方案是 [https]。

完整的 app.config:

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="MyBinding"
            closeTimeout="00:00:10"
            openTimeout="00:00:20"
            receiveTimeout="00:00:30"
            sendTimeout="00:00:40"
            bypassProxyOnLocal="false"
            transactionFlow="false"
            hostNameComparisonMode="WeakWildcard"
            maxReceivedMessageSize="100000000"
            messageEncoding="Mtom"
            proxyAddress="http://foo/bar"
            textEncoding="utf-16"
            useDefaultWebProxy="false">
          <reliableSession ordered="false"
               inactivityTimeout="00:02:00"
               enabled="true" />
          <security mode="Transport">
            <transport clientCredentialType="Digest"
               proxyCredentialType="None"
               realm="someRealm" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service name="MyWCFService"
                behaviorConfiguration="mexServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:9090/MyService"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="wsHttpBinding" bindingConfiguration="MyBinding" contract="IMyService"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexServiceBehavior">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
<startup><supportedRuntime version="v2.0.50727"/></startup></configuration>
4

3 回答 3

48

您的服务的基地址定义为“HTTPS://” - 但您的 mex 地址是“HTTP”。

如果您希望您的服务使用 https://,您还需要使用mexHttpsBinding

<services>
    <service name="MyWCFService" behaviorConfiguration="mexServiceBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="https://localhost:9090/MyService"/>
          </baseAddresses>
        </host>
        <endpoint address="" 
                binding="wsHttpBinding" 
                bindingConfiguration="MyBinding" 
                contract="IMyService" 
        />
        <endpoint address="mex" 
                binding="mexHttpsBinding" 
                contract="IMetadataExchange" 
        />
    </service>
</services>

马克

于 2009-06-22T08:57:46.250 回答
14

我可以去双倍分数吗?:)

当您使用 WS-Http 时,您将绑定到 HTTPS 协议,因此您需要使用正确的 MEX 绑定;

<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
于 2009-06-22T08:59:43.830 回答
4

我在Marc_s 回答的评论中问了一个问题

是否可以将 http 和 https 的 IMetadataExchange 作为单独的端点?

 marc_s 已回答 

您应该能够为 http:// 定义第二个基地址,并将其用于 http mex 端点。

所以解决方案是声明多个端点具有相同的地址=“mex”和不同的绑定,如下所示

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

最近我发现拥有一个配置开关更容易,它可用于在测试时启用 MEX 并在 Live 上禁用。

来自 http://msdn.microsoft.com/en-us/library/aa395224.aspx

可以使用 ServiceHostFactory 类创建从 Internet 信息服务中的 ServiceHost 派生的自定义(添加 ServiceMetadataBehavior 的 IIS 自定义 ServiceHost,(它启用元数据发布),即使此行为未明确添加到服务的配置文件中。

 编写一次启用元数据发布的命令式代码,然后在多个不同的服务中重用该代码。这是通过创建一个派生自 ServiceHost 并覆盖 ApplyConfiguration() 方法以强制添加元数据发布行为的新类来实现的。

来自自定义服务主机 MSDN 文章的示例代码

//Add a metadata endpoint at each base address
//using the "/mex" addressing convention
foreach (Uri baseAddress in this.BaseAddresses)
{
    if (baseAddress.Scheme == Uri.UriSchemeHttp)
    {
        mexBehavior.HttpGetEnabled = true;
        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                MetadataExchangeBindings.CreateMexHttpBinding(),
                                "mex");
    }
    else if (baseAddress.Scheme == Uri.UriSchemeHttps)
    {
        mexBehavior.HttpsGetEnabled = true;
        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                MetadataExchangeBindings.CreateMexHttpsBinding(),
                                "mex");
    }
    else if (baseAddress.Scheme == Uri.UriSchemeNetPipe)
    {
        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                MetadataExchangeBindings.CreateMexNamedPipeBinding(),
                                "mex");
    }
    else if (baseAddress.Scheme == Uri.UriSchemeNetTcp)
    {
        this.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName,
                                MetadataExchangeBindings.CreateMexTcpBinding(),
                                "mex");
    }
}
于 2012-12-17T19:50:16.833 回答