WCF 多端点 [英] WCF multiple endpoints

查看:34
本文介绍了WCF 多端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在多个端点上公开我的 WCF 服务.我不明白为什么会出现这个错误,因为每个端点都有唯一的地址.

I would to expose my WCF service on multiple endpoints. I don't understand why this error, because each endpoint has unique address.

我是否必须为每个单独的端点公开 mex 端点?

Do I have to expose mex endpoint for each separate endpoint?

错误是:

System.InvalidOperationException: 绑定实例已关联到侦听 URI 'http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/'.如果两个端点想要共享同一个 ListenUri,它们也必须共享同一个绑定对象实例.两个冲突的端点要么在 AddServiceEndpoint() 调用中指定,要么在配置文件中指定,要么在 AddServiceEndpoint() 和配置的组合中指定.在 System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)在 System.ServiceModel.ServiceHostBase.InitializeRuntime()在 System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)在 System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)在 Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

System.InvalidOperationException: A binding instance has already been associated to listen URI 'http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/'. If two endpoints want to share the same ListenUri, they must also share the same binding object instance. The two conflicting endpoints were either specified in AddServiceEndpoint() calls, in a config file, or a combination of AddServiceEndpoint() and config. at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) at System.ServiceModel.ServiceHostBase.InitializeRuntime() at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary1
{
  // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
  [ServiceContract]
  public interface IService1
  {
    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
  }

  // Use a data contract as illustrated in the sample below to add composite types to service operations.
  // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfServiceLibrary1.ContractType".
  [DataContract]
  public class CompositeType
  {
    bool boolValue = true;
    string stringValue = "Hello ";

    [DataMember]
    public bool BoolValue
    {
      get { return boolValue; }
      set { boolValue = value; }
    }

    [DataMember]
    public string StringValue
    {
      get { return stringValue; }
      set { stringValue = value; }
    }
  }
}




    namespace WcfServiceLibrary1
{
  // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
  public class Service1 : IService1
  {
    public string GetData(int value)
    {
      return string.Format("You entered: {0}", value);
    }

    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
      if (composite == null)
      {
        throw new ArgumentNullException("composite");
      }
      if (composite.BoolValue)
      {
        composite.StringValue += "Suffix";
      }
      return composite;
    }
  }
}

配置文件:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="WcfServiceLibrary1.Service1">        
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/"
                  binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1" name="first" />

        <endpoint address="http://localhost:8734/Design_Time_Addresses/WcfServiceLibrary1/Service2/"
                  binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1" name="second" />

        <endpoint address="http://localhost:8735/Design_Time_Addresses/WcfServiceLibrary1/Service3/"
                  binding="basicHttpBinding" contract="WcfServiceLibrary1.IService1" name="third" />



        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="http://localhost:8733/Design_Time_Addresses/WcfServiceLibrary1/Service1/" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="http://localhost:8734/Design_Time_Addresses/WcfServiceLibrary1/Service2/" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="http://localhost:8735/Design_Time_Addresses/WcfServiceLibrary1/Service3/" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

推荐答案

我认为这对你有用

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

这篇关于WCF 多端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆