WCF 中的 mex 绑定错误

发布于 2024-07-25 14:03:22 字数 2412 浏览 2 评论 0原文

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

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

错误信息:

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

完整的应用程序配置:

<?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>

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?

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].

Full 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-08-01 14:03:22

您的服务的基址定义为“HTTPS://” - 但您的墨西哥地址是“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>

Marc

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

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

<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

阳光下的泡沫是彩色的 2024-08-01 14:03:22

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

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

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

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;

<endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange" />
凝望流年 2024-08-01 14:03:22

我在评论中提出了一个问题 Marc_s 答案

是否可以将 IMetadataExchange 用于 http 和 https
单独的端点?

 marc_s已回答 

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

因此,解决方案是使用相同的address="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 类来创建自定义
源自互联网信息服务中的ServiceHost
(IIS 自定义 ServiceHost 添加了 ServiceMetadataBehavior,(其中
启用元数据发布),即使此行为不是明确的
添加到服务的配置文件中。

 编写一次启用元数据发布的命令式代码
然后在多个不同的服务中重用该代码。 这是
通过创建一个派生自 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");
    }
}

I've asked a question in a comment  for Marc_s answer

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

 marc_s answered 

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

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

<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.

From 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.

Example code from Custom Service Host MSDN article

//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");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文