C# 如何在Azure服务结构ASP.NET核心有状态服务中获取PartitionInfo?
我们如何访问有状态服务的控制器类中的ParitionInfo对象 以下是指向该对象的链接:C# 如何在Azure服务结构ASP.NET核心有状态服务中获取PartitionInfo?,c#,azure,asp.net-core,azure-service-fabric,C#,Azure,Asp.net Core,Azure Service Fabric,我们如何访问有状态服务的控制器类中的ParitionInfo对象 以下是指向该对象的链接: 公共类MyConntroller:ControllerBase { [HttpPost] 公共异步任务保存(MyObject obj) { //在这里我想访问ParitonInfo对象。如何访问?! } } 下面是ASP.NET核心有状态服务中的服务定义,在该服务中,可以从定义对象的基类轻松获取对象: /// <summary> /// The FabricRuntime c
公共类MyConntroller:ControllerBase
{
[HttpPost]
公共异步任务保存(MyObject obj)
{
//在这里我想访问ParitonInfo对象。如何访问?!
}
}
下面是ASP.NET核心有状态服务中的服务定义,在该服务中,可以从定义对象的基类轻松获取对象:
/// <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();
}))
};
}
//
///FabriRuntime为每个服务创建此类的实例
///类型实例。
///
内部密封类MyStatefulService:StatefulService
{
公共MyStatefulService(StatefulServiceContext上下文)
:基本(上下文)
{ }
///
///可选覆盖,用于为此服务实例创建侦听器(如tcp、http)。
///
///听众的集合。
受保护的重写IEnumerable CreateServiceReplicaListeners()
{
返回新的ServiceReplicaListener[]
{
新建ServiceReplicaListener(serviceContext=>
新KestreCommunicationListener(serviceContext,(url,listener)=>
{
ServiceEventSource.Current.ServiceMessage(serviceContext,$“在{url}上启动Kestrel”);
返回新的WebHostBuilder()
.UseKestrel()
.配置服务(
服务=>服务
.AddSingleton(serviceContext)
.AddSingleton(此.StateManager))
.UseContentRoot(目录.GetCurrentDirectory())
.UseStartup()
.UseServiceFabricIntegration(侦听器,ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
.useURL(url)
.Build();
}))
};
}
我是否应该创建一个单例类,通过DI将其连接起来,并让DI框架将其实例传递给控制器类?是否有更好的快捷方式来实现访问PARTIONINFO数据的目标?您走的是正确的道路。将参数
IStatefulServicePartition
添加到mycontroller
构造函数中。
在ConfigureServices
中,使用this.Partition
注册服务
例如:
.AddSingleton<IStatefulServicePartition>(this.Partition)
.AddSingleton(此.Partition)
我认为应该在下面的方法中进行更改,而不是配置服务:受保护的覆盖IEnumerable CreateServiceReplicaListeners()
.AddSingleton<IStatefulServicePartition>(this.Partition)