2016-11-30 118 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; 
    } 
} 

在服務織物添加多個服務和...就像一個魅力的工作! :)