解决方法找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址

在使用WCF过程中经常碰到莫名其妙的各种方案匹配的基址和注册基址方案不符的异常信息。比如方案匹配基址net.tcp和注册注册基址方案http、方案匹配基址http和注册注册基址方案net.tcp和方案匹配基址http和注册注册基址方案https。注意 http和https细小的差别,可是完全不同的协议。

在这里总结几种碰到的情况和解决方法。

一、IIS 承载WCF。找不到具有绑定 NetTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注册的基址方案是 [http]。


IIS 承载WCF ,在高级设置->已启用的协议 中添加 net.tcp


二、console承载WCF。找不到具有绑定 MetadataExchangeTcpBinding 的终结点的与方案 net.tcp 匹配的基址。注册的基址方案是 []。


区别在于注册的基址方案是 []  和 注册的基址方案是 [http] 注册方案为空,是因为没有设置baseAddress。

下面的例子是console承载WCF,实现net.tcp 和提供元数据信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using WcfServiceLibrary1;
 
namespace WcfConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddr = new Uri("net.tcp://localhost:8880/Service1/");
            using (ServiceHost host=new ServiceHost(typeof(Service1),baseAddr)) //设置基址,没有就会引发异常,注册的基址方案是 []
            {
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = false;
                host.Description.Behaviors.Add(smb);
                host.AddServiceEndpoint(typeof(IService1), new NetTcpBinding(), "");
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(),"mex");

                Console.WriteLine("启动服务...");
                host.Open();
                Console.WriteLine("服务启动成功");
                Console.ReadLine();
            }
        }
    }
}

三、WCF服务主机承载。找不到具有绑定 MetadataExchangeHttpBinding 的终结点的与方案 http 匹配的基址。注册的基址方案是 [net.tcp]。



我的基址是"net.tcp://localhost:8733/Service1/" 而 元数据终结点的binding是mexHttpBinding,所以要改成mexTcpBinding。 <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- 部署服务库项目时,必须将配置文件的内容添加到 
  主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">
         <host>
        <baseAddresses>
            <add baseAddress="net.tcp://localhost:8733/Service1/"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- 除非完全限定,否则地址将与上面提供的基址相关 -->
      <endpoint address="" binding="netTcpBinding" contract="WcfServiceLibrary1.IService1">
         
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        
        <!-- Metadata Endpoints -->
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> 
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
       
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="False" httpsGetEnabled="False"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
            以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1"/></startup></configuration>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值