C# 在单个ServiceHost中是否可能有多个服务类型和服务端点?

C# 在单个ServiceHost中是否可能有多个服务类型和服务端点?,c#,web-services,wsdl,C#,Web Services,Wsdl,使用.NET3.5但也欢迎使用4.5解决方案 我有几个wsdl,每个wsdl定义一个唯一的web服务,我需要将其作为服务器托管。以下代码说明了如何运行单个web服务: using System; using System.ServiceModel; using System.ServiceModel.Description; namespace Playground { class Program { static BasicHttpBinding GetBin

使用.NET3.5但也欢迎使用4.5解决方案

我有几个wsdl,每个wsdl定义一个唯一的web服务,我需要将其作为服务器托管。以下代码说明了如何运行单个web服务:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Playground
{
    class Program
    {
        static BasicHttpBinding GetBinding()
        {
            var binding = new BasicHttpBinding();
            binding.MaxBufferSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            binding.AllowCookies = true;
            binding.MaxReceivedMessageSize = int.MaxValue;

            return binding;
        }

        static void Main(string[] args)
        {
            using (ServiceHost host = new ServiceHost(
                typeof(ServiceAImplementation),
                new Uri("http://127.0.0.1:8081/")
                ))
            {
                host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
                host.Description.Behaviors.Add(
                    new ServiceDebugBehavior
                    {
                        IncludeExceptionDetailInFaults = true
                    });
                host.AddServiceEndpoint(typeof(ServiceAInterface), GetBinding(), "ServiceA.soap");

                try
                {
                    host.Open();
                }
                catch (TimeoutException timeoutExp)
                {
                    Console.WriteLine("Timeout");
                }
                catch (CommunicationException commExp)
                {
                    Console.WriteLine("Communication problem");
                }
                finally
                {
                    Console.WriteLine("Press any key to exit...");
                    Console.ReadLine();
                    host.Close();
                }
            }
        }
    }
}
我正在研究向同一主机添加多个服务类型和服务点

假设我有以下服务:

  • ServiceA
    带有
    serviceanterface
    serviceamplementation
    ,可在
    http://127.0.0.1:8080/ServiceA.soap
  • ServiceB
    ServiceBInterface
    ServiceBImplementation
    ,可在
    http://127.0.0.1:8081/ServiceB.soap
  • ServiceC
    ServiceCInterface
    ServiceCImplementation
    ,可在
    http://127.0.0.1:8082/ServiceC.soap
到目前为止,我无法找到一种方法使
ServiceHost
(注意a,这意味着一个单一的!)处理多个web服务。构造函数(
ServiceHost(object,Uri[])
ServiceHost(type,Uri[])
清楚地指向一个一个主机,一个服务的策略。当然,可以分配多个服务端点,但这并不能解决我面临的问题

有没有办法做到这一点,或者我必须实现自己的定制
ServiceHost


更新:我的问题似乎有点不清楚。我知道我可以只创建一些其他主机,然后基本上从上面复制粘贴每个服务的代码(我甚至创建了一个版本,其中每个主机在单独的线程中运行)。我正在寻找一个单主机、多服务解决方案。我已经研究过通过添加单独的服务端点以及传递单个实现(上面示例代码中的
服务…实现
)来提供多个服务契约(web服务的接口).问题是所有操作都至少有一个重叠操作
,表示相同的操作签名(返回类型、名称和输入参数)但是,根据web服务的不同,返回值是不同的。虽然是的,我知道这些服务设计得很糟糕,但这是我必须处理的问题。

啊,以前把你的查询看错了

如果我现在理解正确,我想这就是你的意思:

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost hostA = new ServiceHost(typeof(ServiceAImplementation), new Uri("http://127.0.0.1:6600/"));
            ServiceHost hostB = new ServiceHost(typeof(ServiceBImplementation), new Uri("http://127.0.0.1:6601/"));

            InitHost<ServiceAInterface>(hostA, "ServiceA.soap");
            InitHost<ServiceBInterface>(hostB, "ServiceB.soap");

            try
            {
                hostA.Open();
                hostB.Open();
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine("Timeout");
            }
            catch (CommunicationException ex)
            {
                Console.WriteLine("Communication problem");
            }
            finally
            {
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
                hostA.Close();
                hostB.Close();
            }
        }

        private static BasicHttpBinding GetBinding()
        {
            var binding = new BasicHttpBinding();
            binding.MaxBufferSize = int.MaxValue;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.ReaderQuotas = System.Xml.XmlDictionaryReaderQuotas.Max;
            binding.AllowCookies = true;
            binding.MaxReceivedMessageSize = int.MaxValue;

            return binding;
        }

        private static void InitHost<T>(ServiceHost host, string endpointEnd)
        {
            host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
            host.Description.Behaviors.Add(
                new ServiceDebugBehavior
                {
                    IncludeExceptionDetailInFaults = true
                });
            host.AddServiceEndpoint(typeof(T), GetBinding(), endpointEnd);

            ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
            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(),
              "mex"
            );
        }
    }
}
使用系统;
使用System.ServiceModel;
使用System.ServiceModel.Description;
名称空间控制台
{
班级计划
{
静态void Main(字符串[]参数)
{
ServiceHost hostA=新ServiceHost(类型of(ServiceAimImplementation),新Uri(“http://127.0.0.1:6600/"));
ServiceHost hostB=新的ServiceHost(类型化(ServiceBImplementation)),新的Uri(“http://127.0.0.1:6601/"));
InitHost(hostA,“ServiceA.soap”);
InitHost(hostB,“ServiceB.soap”);
尝试
{
hostA.Open();
hostB.Open();
}
捕获(TimeoutException例外)
{
控制台写入线(“超时”);
}
捕获(通信例外)
{
Console.WriteLine(“通信问题”);
}
最后
{
Console.WriteLine(“按任意键退出…”);
Console.ReadLine();
hostA.Close();
hostB.Close();
}
}
私有静态BasicHttpBinding GetBinding()
{
var binding=新的BasicHttpBinding();
binding.MaxBufferSize=int.MaxValue;
binding.MaxReceivedMessageSize=int.MaxValue;
binding.ReaderQuotas=System.Xml.XmlDictionaryReaderQuotas.Max;
binding.AllowCookies=true;
binding.MaxReceivedMessageSize=int.MaxValue;
返回绑定;
}
私有静态void InitHost(ServiceHost主机,字符串端点)
{
host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
host.Description.Behaviors.Add(
新的ServiceDebuggBehavior
{
IncludeExceptionDetailInFaults=true
});
AddServiceEndpoint(typeof(T),GetBinding(),endpointEnd);
ServiceMetadataBehavior smb=host.Description.Behaviors.Find();
如果(smb==null)
smb=新的ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
“墨西哥”
);
}
}
}

如果我错了,请纠正我(在C#和web服务方面没有那么精明),但是教程不使用2台主机吗?我可以清楚地看到XML配置文件有两个
条目。此外,我希望使用代码,因为我将在将来使用配置(上面的示例只是一个游乐场)是一个自定义的。啊,现在更好地理解你了,看看上面。我已经有了(3个服务)。我的问题是,这是否可以避免(多个主机,每个web服务一个),如果可以,那么如何避免。我甚至研究了创建一个通用实现(ServiceHost需要什么)然后简单地为不同的服务契约(aka接口)提供单独的服务端点。但是在我的真实场景中,这是不可能的,因为服务有一些重叠的操作(相同的名称、相同的参数和相同的返回类型但不同的返回值),这些操作不能合并