I want to store the value data in service globally and use in different layers doing dependency injection in .net 8 rest api project. I am using clean architecture.
public interface ISharedDataService
{
void SetTrustedPartnerDetails(object trustedPartnerDetails);
object GetTrustedPartnerDetails();
}
public class SharedDataService : ISharedDataService
{
private object _trustedPartnerDetails;
public void SetTrustedPartnerDetails(object trustedPartnerDetails)
{
_trustedPartnerDetails = trustedPartnerDetails;
}
public object GetTrustedPartnerDetails()
{
return _trustedPartnerDetails;
}
}
// In Program.cs or Startup.cs
services.AddScoped<ISharedDataService, SharedDataService>();
I have set the data in serivice in middleware and I want to get the data in api controller with out using httpcontext.
[ApiController]
[Route("[controller]")]
public class ExampleController : ControllerBase
{
private readonly ISharedDataService _sharedDataService;
public ExampleController(ISharedDataService sharedDataService)
{
_sharedDataService = sharedDataService;
}
[HttpGet]
public IActionResult GetTrustedPartnerDetails()
{
var trustedPartnerDetails = _sharedDataService.GetTrustedPartnerDetails();
if (trustedPartnerDetails == null)
{
return NotFound("Trusted Partner details not found.");
}
return Ok(trustedPartnerDetails);
}
}
But in api controller _sharedDataService.GetTrustedPartnerDetails() is always null. How can I achieve this ?