C# 一个类实现两个服务契约

C# 一个类实现两个服务契约,c#,wcf,wcf-endpoint,servicecontract,C#,Wcf,Wcf Endpoint,Servicecontract,我想创建一个类,实现2个服务契约,并在不同端口上公开2个端点(我将端口作为程序的输入) 我的问题与非常类似,但我想要两个端点,每个端点位于不同的端口 编辑: 使用此代码: var aConnectionString = "http://localhost:8200/A"; var bConnectionString = "http://localhost:8201/B"; ServiceHost host= new ServiceHost(typeof(abImp)); var bindin

我想创建一个类,实现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();
解决方案

            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();
var aConnectionString=”http://localhost:“+portA+”/A”;
var bConnectionString=”http://localhost:“+portB+”/B”;
ServiceHost aHost=newServiceHost(实例,新Uri(aConnectionString));
ServiceHost bHost=新ServiceHost(实例,新Uri(bConnectionString));
aHost.Description.Behaviors.Find().InstanceContextMode=InstanceContextMode.Single;
bHost.Description.Behaviors.Find().InstanceContextMode=InstanceContextMode.Single;
aHost.AddServiceEndpoint(typeof(A),new BasicHttpBinding(),aConnectionString);
bHost.AddServiceEndpoint(typeof(B),new BasicHttpBinding(),bConnectionString);
{
//检查服务主机是否已具有ServiceMetadataBehavior
var smb=aHost.Description.Behaviors.Find()??新ServiceMetadataBehavior();
aHost.Description.Behaviors.Add(smb);
aHost.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
A连接字符串+“/Mex”
);
}
{
//检查服务主机是否已具有ServiceMetadataBehavior
var smb=bHost.Description.Behaviors.Find()??新ServiceMetadataBehavior();
bHost.Description.Behaviors.Add(smb);
bHost.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
b连接字符串+“/Mex”
);
}
aHost.Open();
bHost.Open();

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

看看这个:查看我编辑的问题。您需要添加MetadataExchange(MEX)端点:查看我编辑的问题。我做错了什么?错误还是一样的?谢谢你的回复。但是,正如我在问题中所写的,我需要在不同端口上使用这两个服务。@user334396如果您明确希望在完全不同的URI上使用该服务,那么为什么不使用两个不同的
ServiceHost
。您需要相同的实现类,但情况仍然如此。是否有任何理由让您只使用一个
ServiceHost
实例执行此操作?因为我希望具体的类是singleton。在这种情况下,您可以查看以下内容是否适用于
ServiceHost
的两个实例,但适用于您服务类的singleton实例-
            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();