Примерно так ...
publi c class ServicesCollections {
public IConfiguration Configuration { get; }
public IServiceCollection Services { get; }
public ServicesCollections()
{
}
public ServicesCollections(IConfiguration configuration, IServiceCollection services)
{
Configuration = configuration;
Services = services;
}
}
/// <summary> Generic singleton with double check pattern and with instance parameter </summary>
/// <typeparam name="T"></typeparam>
public class SingleObject<T> where T : class, new()
{
/// <summary> Lock object </summary>
private static readonly object _lockingObject = new object();
/// <summary> Instance </summary>
private static T _singleObject;
/// <summary> ctor </summary>
public SingleObject()
{
}
/// <summary> Instance with parameter </summary>
/// <param name="param">Parameters</param>
/// <returns>Instance</returns>
public static T Instance(params dynamic[] param)
{
if (_singleObject == null)
{
lock (_lockingObject)
{
if (_singleObject == null)
{
_singleObject = (T)Activator.CreateInstance(typeof(T), param);
}
}
}
return _singleObject;
}
}
При запуске ---
publi c void ConfigureServices (IServiceCollection services) {services.AddControllersWithViews ();
services.Configure<CookiePolicyOptions>(options =>
{
//This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None;
}
);
services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);
services.AddMvc()
.AddControllersAsServices();
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddRazorPages();
var configs = Interfaces.SingleObject<Interfaces.ServicesCollections>.Instance(Configuration, services);
services.AddScoped<ServicesCollections>();
services.AddTransient(ctx => new SSO.Gateway.Web.Controllers.LoginController(configs));
ConfigureIdentityServer (services);
}
In ваш контроллер -
public class YourController : Controller
{
public YourController (ServicesCollections obj)
{
Configuration = obj.Configuration;
Services = obj.Services;
ConfigureServices();
}
public IConfiguration Configuration { get; }
public IServiceCollection Services { get; }
public void ConfigureServices()
{
}