2016-11-30 120 views
0

我们有一种情况,我们一直在使用基于WCF的普通的Windows服务,我们在同一个地址上只有一个附加的后缀,例如:无状态的WCF服务侦听器在同一个端口

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

但是,现在我们希望通过使用服务结构转移到asuze,我们希望一步一步地进行。 而其中一个步骤是将此Windows服务移动到服务结构环境中...

我没有做的是你可能会意识到,要设置许多无状态的服务来监听同一端口,但具有不同的后缀。

我认为EndPoint中的PathSuffix属性可以做到这一点,但我只获得并运行了其中一项服务。其他人明确指出港口已经被占用。

希望这会工作:

<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> 

等等,等等......

是否有任何其他的方式来解决我的情况,或我现在需要改变整个结构吗?

希望有人在那里解决了这个问题!

谢谢!

+1

您是否按照此处所述的步骤操作? https://msdn.microsoft.com/zh-cn/library/ms734772(v=vs.110).aspx – LoekD

+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; 
    } 
} 

在服务织物添加多个服务和...就像一个魅力的工作! :)