0

2 つのサービス コントラクトを実装し、異なるポートで 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

2 つの完全に異なるアドレスが必要で、同じ実装クラスのみが必要な場合は、2 つの異なる ServiceHostインスタンスを使用することをお勧めします。

于 2014-12-30T18:44:05.820 に答える