When a http request comes in, I would like to instantiate a class and pass this instance to any class that needs it.
For e.g., I have a custom Customer
class with two properties. Based on the Request parameters and business logic, I'd like to set the Actions property and any controller or repository that needs this class should have access to the Actions property. I could not use "AddInstance" since ConfigureServices method does not have access to current request.
CustomerClass.cs
public class CustomerClass
{
public string UserName{get;set;}
public string Actions{get;set;}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<CustomerClass, CustomerClass>();
}
public void Configure(IApplicationBuilder app, CustomerClass cs)
{
app.Use((context, next) =>
{
cs.UserName = context.User.Identity.Name;
// cs.Actions = BUSINESSLOGICBASEDONREQUEST
return next();
});
}
Controller
public class CustomerController : Controller
{
CustomerClass _local;
public CustomerController(CustomerClass cls)
{
// here, I expect to use the class instantiated in Startup.cs >
// Configure method since I set the CustomerClass to have "Scoped"
// life span. However, a new Customer class instance is being
// requested.
}
}