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