同一端口上的无状态WCF服务侦听器

时间:2016-11-30 21:27:51

标签: c# wcf azure-service-fabric stateless suffix

我们有一种情况,我们一直在使用基于WCF的普通Windows服务,我们在同一地址上托管了许多监听器,端口只有地址上的附加后缀,例如:

http://localhost:9000/<listener1_name>/
http://localhost:9000/<listener2_name>/
http://localhost:9000/<listener3_name>/
http://localhost:9000/<listener4_name>/

然而,现在我们希望通过使用服务结构转移到asuze,我们喜欢一步一步地采取它。 其中一个步骤是将此Windows服务移动到Service Fabric环境中......

我没有做到的就像您可能意识到的那样,设置一些无状态服务来侦听同一个端口,但后缀不同。

我认为PathSuffix中的EndPoint属性可以解决问题,但我只启动并运行其中一个服务。其他人明确表示该港口已被占用。

希望这可行:

<Resources>
  <Endpoints>
    <Endpoint Name="WcfService1Endpoint" Protocol="tcp" Port="9000" PathSuffix="Service1ListenerName" /
  <Endpoints>
</Resources>

然后在下一个:

<Resources>
  <Endpoints>
    <Endpoint Name="WcfService2Endpoint" Protocol="tcp" Port="9000" PathSuffix="Service2ListenerName" />
  <Endpoints>
</Resources>

Etc等......

有没有其他方法可以解决我的情况,或者我现在需要改变整个结构吗?

希望有人解决这个问题!

谢谢!

1 个答案:

答案 0 :(得分:0)

好的,这就是我这样做的方式:

我构建了一个基类

public class StatelessServiceBase : StatelessService
{
    .
    .
    .

    protected ICommunicationListener CreateListener(StatelessServiceContext context, object service, string interfaceName, AuthenticationInspector inspector = null)
    {
        Uri baseUri = new Uri($"{Util.GetBaseServerAddress()}{service.GetType().Name}");
        ServiceHost serviceHost = new ServiceHost(service.GetType(), baseUri);
        this.AddServiceEndpoint(serviceHost, service, interfaceName, inspector);
        return new ServiceHostCommunicationListener(serviceHost, baseUri.AbsoluteUri);
    }

    private void AddServiceEndpoint(ServiceHost serviceHost, object service, string interfaceName, AuthenticationInspector inspector)
    {
        var binding = new WSHttpBinding(SecurityMode.None);
        binding.SendTimeout = new TimeSpan(0, 10, 0);
        binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
        binding.MaxBufferPoolSize = 2147483647;
        binding.MaxReceivedMessageSize = 2147483647;
        binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas
        {
            MaxDepth = 2147483647,
            MaxStringContentLength = 2147483647,
            MaxArrayLength = 2147483647,
            MaxBytesPerRead = 2147483647,
            MaxNameTableCharCount = 2147483647
        };

        if (inspector == null)
        {
            serviceHost.AddServiceEndpoint(service.GetType().GetInterface(interfaceName), binding, string.Empty);
        }
        else
        {
            serviceHost.AddServiceEndpoint(service.GetType().GetInterface(interfaceName), binding, string.Empty).Behaviors.Add(inspector);
        }
    }
}

然后我从无状态服务类调用CreateListener:

internal class MyStatelessService : StatelessServiceBase
{
    public MyStatelessService(StatelessServiceContext context)
        : base(context)
    {
    }

    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
    {
        yield return new ServiceInstanceListener(context =>
                 this.CreateListener(context, new MyService(), "IMyService", new AuthenticationInspector()));
    }
}

Settings.xml看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Section Name="Configuration">
    <Parameter Name="BaseServerAddress" Value="http://localhost:9000/"/>
  </Section>
</Settings>

与读者功能一起:

public class Util
{
    internal static string GetBaseServerAddress()
    {
        var configurationPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");

        var baseServerAddress =
            configurationPackage.Settings.Sections["Configuration"].Parameters["BaseServerAddress"];

        return baseServerAddress.Value;
    }
}

在服务结构中添加多项服务......工作就像一个魅力! :)