c# - 如何在 Azure Service Fabric ASP.NET Core 有状态服务中获取 PartitionInfo?

标签 c# azure asp.net-core azure-service-fabric

我们如何访问有状态服务的 Controller 类中的 ParitionInfo 对象?

以下是对象的链接:https://learn.microsoft.com/en-us/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 框架将其实例传递给 Controller ​​类吗?有没有更好的捷径来达到访问ParitionInfo数据的目的?

最佳答案

你走在正确的道路上。将参数 IStatefulServicePartition 添加到您的 MyConntroller 构造函数中。 在 ConfigureServices 中,注册服务 partition使用this.Partition

例如:

.AddSingleton<IStatefulServicePartition>(this.Partition)

关于c# - 如何在 Azure Service Fabric ASP.NET Core 有状态服务中获取 PartitionInfo?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52229077/

相关文章:

c# - Entity Framework Core 2.1 group by 无法正常工作

nginx - Nginx 背后的 SignalR - 获得 200 响应而不是 websocket 升级

c# - Roslyn/MicrosoftCodeAnalysis.CSharp.Scripting 抛出 'FileNotFoundException'

azure - 我需要为 Azure AD 中的用户设置 "job info",例如 "department"。 Set-AzADUser 没有这方面的参数。还有其他命令吗?

c# - Kotlin 的数据类 == C# 的结构?

azure - 将外部API管理实例切换到内部

java - 线程 "main"java.lang.NoClassDefFoundError : org/apache/qpid/proton/engine/Extendable 中出现异常

ASP.NET 5、EF 7 和 SQLite - SQLite 错误 1 ​​: 'no such table: Blog'

c# - 如何从 3D 图像生成 2D 平面

c# - WindowsImpersonationContext 下的程序集绑定(bind)。如何防止 FileLoadException?