简体   繁体   English

WCF中的mex绑定错误

[英]mex binding error in WCF

I am using VSTS 2008 + C# + .NET 3.0. 我正在使用VSTS 2008 + C#+ .NET 3.0。 I am using a self-hosted WCF service. 我正在使用自托管的WCF服务。 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? 我已经发布了我的整个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. 无法通过绑定MetadataExchangeHttpBinding找到与端点的方案http匹配的基址。 Registered base address schemes are [https]. 注册的基地址方案是[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>

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 mexHttpsBinding as 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

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" />

I've asked a question in a comment for Marc_s answer 我在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 different bindings 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. 可以使用ServiceHostFactory类在Internet信息服务中创建从ServiceHost派生的自定义(IIS自定义ServiceHost,它添加ServiceMetadataBehavior(启用元数据发布),即使此行为未在服务的配置文件中显式添加也是如此。

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. 这是通过创建一个派生自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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM