2017-05-19 27 views
2

我是服務結構的新手,在我的練習中,我做了一個可靠的無狀態服務,在RunAsync中增加了他的屬性「計數器」。我證實我可以通過接口IService公開一個返回此計數器值(通過ServiceProxy從客戶端調用的方法)的方法,顯然會覆蓋CreateServiceInstanceListeners並添加CreateServiceRemotingListener。 然後我試圖添加其他定製通信偵聽SS1ServiceEndpoint(即一個指定端口7080上聽):服務結構 - 遠程處理監聽器和通信監聽器不能同時工作

<Endpoint Name="RemoteListener" /> 
<Endpoint Name="SS1ServiceEndpoint" Protocol="http" Port="7080" Type="Input" /> 

但服務最初引發

Exception thrown: 'System.ArgumentException' in Microsoft.ServiceFabric.FabricTransport.dll 

然後定製聽者的使用openAsync方法被稱爲越來越多,並且在每次調用之後拋出另一個異常:

Exception thrown: 'System.ObjectDisposedException' in mscorlib.dll 
Exception thrown: 'System.AggregateException' in mscorlib.dll 
Exception thrown: 'System.Fabric.FabricElementAlreadyExistsException' in 
System.Fabric.dll 
Exception thrown: 'System.ArgumentException' in 
Microsoft.ServiceFabric.FabricTransport.dll 

如果我刪除CreateServiceRemotingListener中的Cr eateServiceInstanceListeners,服務啓動,我可以從偵聽端口上的瀏覽器調用它。

我的問題是:多個聽衆不支持?我還沒有嘗試過兩個自定義偵聽器(在不同的端口上)。

回答

3

支持多個偵聽器,但您必須爲偵聽器提供一個名稱,否則該名稱是可選的。嘗試在您的CreateServiceInstanceListeners中爲您的聽衆添加一個名稱。

protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() 
{ 
    return new[] 
    { 
     new ServiceInstanceListener(context => this.CreateServiceRemotingListener(context), "RemotingListener"), 
     new ServiceInstanceListener(context => new CustomListener(), "CustomListenerName") 
    }; 
} 
+1

太棒了!它解決了我的問題:)從MSDN文檔中不太清楚! 只是另一個問題:以這種方式我可以公開一個包含在同一個項目中的真正的WebAPI,還是更好地將無狀態服務項目與WebAPI項目分開? 在此先感謝! –

+0

您可以在同一服務中添加WebAPI。你是否將它放在相同的服務中取決於你的設計。請記住,您正在使用微服務架構。如果您的WebAPI只會返回計數器,則應將其添加到相同的服務中(WebAPI僅充當計數器的端點)。如果你的WebAPI有很多功能,其中之一是返回計數器,爲它創建一個不同的服務,並使用服務遠程處理(Counter是更大的一部分)來獲得計數器。 –

+0

完全同意它不清楚,當我和多個聽衆一起玩時,我也錯過了超負荷! – Mardoxx