C# WCF 中的 mex 绑定错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1026149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:50:16  来源:igfitidea点击:

mex binding error in WCF

c#.netwcfvisual-studio-2008wshttpbinding

提问by George2

I am using VSTS 2008 + C# + .NET 3.0. I am using a self-hosted WCF service. When executing the following statement, there is the following "binding not found" error. I have posted my whole app.config file, any ideas what is wrong?

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

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

Error message:

错误信息:

Could not find a base address that matches scheme http for the endpoint with binding MetadataExchangeHttpBinding. Registered base address schemes are [https].

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

Full app.config:

完整的 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>

采纳答案by marc_s

The base address for your service defines "HTTPS://" - but your mex address is "HTTP".

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

If you want your service to use https://, you'll need to use the mexHttpsBindingas well:

如果您希望您的服务使用 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>

Marc

马克

回答by blowdart

Can I go for the double score? :)

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

As you're using WS-Http you are binding to an HTTPS protocol, so you need to use the correct MEX binding;

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

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

回答by Michael Freidgeim

I've asked a question in a comment ?for Marc_sanswer

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

Is it possible to have IMetadataExchange for both http and https as separate endpoints?

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

?marc_s?answered?

?marc_s?回答了吗?

you should be able to define a second base address, for http:// and use that for the http mex endpoint.

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

So solution is to declare multiple?endpoints with the SAME?address="mex" and differentbindings like the following

所以解决方案是使用 SAME?address="mex" 和不同的绑定来声明多个端点,如下所示

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

Recently I found that it's easier to have one configuration switch that can be used to enable MEX on test and disable on Live.

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

From?http://msdn.microsoft.com/en-us/library/aa395224.aspx

从?http://msdn.microsoft.com/en-us/library/aa395224.aspx

It's possible to use the?ServiceHostFactory?class to create a custom derived from ServiceHost in the Internet Information Services (IIS?custom ServiceHost that adds the ServiceMetadataBehavior, (which enables metadata publishing), even if this behavior is not explicitly added in the service's configuration file.

?Write the imperative code that enables metadata publishing once and then reuse that code across several different services. This is accomplished by creating a new class that derives from?ServiceHost?and overrides the?ApplyConfiguration() method to imperatively add the metadata publishing behavior.

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

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

Example code from Custom Service Host MSDN article

定义服务主机 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");
    }
}