可以在单个ServiceHost中使用多种服务类型和服务端点吗?

问题描述 投票:0回答:1

也欢迎使用.NET 3.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();
                }
            }
        }
    }
}

我正在考虑将多种服务类型和服务点添加到同一主机。

假设我有以下服务:

  • ServiceAServiceAInterfaceServiceAImplementation,可在http://127.0.0.1:8080/ServiceA.soap上找到
  • ServiceBServiceBInterfaceServiceBImplementation,可在http://127.0.0.1:8081/ServiceB.soap上找到
  • ServiceCServiceCInterfaceServiceCImplementation,可在http://127.0.0.1:8082/ServiceC.soap上找到

到目前为止,我无法找到一种方法来制作ServiceHost(注意a,这意味着单个!)处理多个Web服务。单独的构造函数(ServiceHost(object, Uri[])ServiceHost(type, Uri[])明确指向一个主机,一个服务策略。当然可以分配多个服务端点,但这不能解决我面临的问题。

有没有办法做到这一点,还是我必须实现自己的自定义ServiceHost呢?

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

c# web-services wsdl
1个回答
0
投票

啊,以前读错了你的查询。

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

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"
            );
        }
    }
}

screenshot of WCFClient (sorry might be blocked by my firewall)

© www.soinside.com 2019 - 2024. All rights reserved.