0

我想创建一个实现 2 个服务合同并在不同端口上公开 2 个端点的类(我将端口作为程序的输入)。

我的问题与这个问题非常相似,但我想要两个端点,每个端点都在不同的端口上。

编辑:

使用此代码:

var aConnectionString = "http://localhost:8200/A";
var bConnectionString = "http://localhost:8201/B";

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

var binding = new BasicHttpBinding();

host.AddServiceEndpoint(typeof(A), binding, aConnectionString);
host.AddServiceEndpoint(typeof(B), binding, bConnectionString);

{
    // Check to see if the service host already has a ServiceMetadataBehavior
    ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();

    // If not, add one
    if (smb == null)
        smb = new ServiceMetadataBehavior();

    smb.HttpGetEnabled = true;
    smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
    host.Description.Behaviors.Add(smb);


    host.AddServiceEndpoint(
        ServiceMetadataBehavior.MexContractName,
        MetadataExchangeBindings.CreateMexHttpBinding(),
        aConnectionString
        );

}
host.Open();

我在 host.Open() 操作上收到此错误消息:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

Additional information: A binding instance has already been associated to listen URI 'http://localhost:8200/A'. 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.

解决方案

            var aConnectionString = "http://localhost:" + portA+ "/A";
            var bConnectionString = "http://localhost:" + portB+ "/B";


            ServiceHost aHost = new ServiceHost(instance, new Uri(aConnectionString));
            ServiceHost bHost = new ServiceHost(instance, new Uri(bConnectionString));

            aHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;
            bHost.Description.Behaviors.Find<ServiceBehaviorAttribute>().InstanceContextMode = InstanceContextMode.Single;

            aHost.AddServiceEndpoint(typeof(A), new BasicHttpBinding(), aConnectionString);
            bHost.AddServiceEndpoint(typeof(B), new BasicHttpBinding(), bConnectionString);


            {
                // Check to see if the service host already has a ServiceMetadataBehavior
                var smb = aHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior();
                aHost.Description.Behaviors.Add(smb);

                aHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    aConnectionString + "/Mex"
                    );
            }
            {
                // Check to see if the service host already has a ServiceMetadataBehavior
                var smb = bHost.Description.Behaviors.Find<ServiceMetadataBehavior>() ?? new ServiceMetadataBehavior();
                bHost.Description.Behaviors.Add(smb);

                bHost.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    bConnectionString + "/Mex"
                    );
            }

            aHost.Open();
            bHost.Open();
4

1 回答 1

0

鉴于您想要两个完全不同的地址,只有相同的实现类,我建议使用两个不同 ServiceHost的实例。

于 2014-12-30T18:44:05.820 回答