簡體   English   中英

如何在Azure Service Fabric ASP.NET Core有狀態服務中獲取PartitionInfo?

[英]How to obtain PartitionInfo in an Azure Service Fabric ASP.NET Core stateful service?

我們如何訪問有狀態服務的Controller類中的ParitionInfo對象?

這是對象的鏈接: https : //docs.microsoft.com/zh-cn/dotnet/api/system.fabric.servicenotification.partitioninfo?view=azure-dotnet

public class MyConntroller : ControllerBase
{
  [HttpPost]
  public async Task<IActionResult> Save(MyObject obj)
  {
     //Here I would like to access ParitionInfo object. How?!
  }
}

這是ASP.NET Core有狀態服務中的服務定義,可以從定義該對象的基類輕松獲取該對象:

    /// <summary>
    /// The FabricRuntime creates an instance of this class for each service 
    /// type instance. 
    /// </summary>
    internal sealed class MyStatefulService : StatefulService
    {
        public MyStatefulService(StatefulServiceContext context)
            : base(context)
        { }

        /// <summary>
        /// Optional override to create listeners (like tcp, http) for this service instance.
        /// </summary>
        /// <returns>The collection of listeners.</returns>
        protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
        {
            return new ServiceReplicaListener[]
            {
                new ServiceReplicaListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, (url, listener) =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                        return new WebHostBuilder()
                                    .UseKestrel()
                                    .ConfigureServices(
                                        services => services
                                            .AddSingleton<StatefulServiceContext>(serviceContext)
                                            .AddSingleton<IReliableStateManager>(this.StateManager))
                                    .UseContentRoot(Directory.GetCurrentDirectory())
                                    .UseStartup<Startup>()
                                    .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
                                    .UseUrls(url)
                                    .Build();
                    }))
            };
        }

我應該創建一個單例類,通過DI將其連接起來,然后讓DI框架將其實例傳遞給控制器​​類嗎? 是否有更好的快捷方式來實現訪問ParitionInfo數據的目標?

您在正確的道路上。 將參數IStatefulServicePartition添加到MyConntroller構造函數中。 ConfigureServices ,使用this.Partition注冊服務分區

例如:

.AddSingleton<IStatefulServicePartition>(this.Partition)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM