How can we access the ParitionInfo object in a Controller class of a stateful service?
Here is the link to the object: 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?!
}
}
And here is the service definition in the ASP.NET Core stateful service where the object can easily be obtained from the base class where it's defined:
/// <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();
}))
};
}
Should I create a singleton class, wire it up through DI, and have the DI framework pass its instance to the controller class? Are there better shortcuts to achieve the goal of accessing ParitionInfo data?
You're on the right path. Add an argument IStatefulServicePartition
to your MyConntroller
constructor.
In ConfigureServices
, register the service partition using this.Partition
.
For example:
.AddSingleton<IStatefulServicePartition>(this.Partition)