public interface IFoo
{
string GetInputString(string input);
}
public class Foo : IFoo
{
public string GetInputString(string input)
{
return $"输入的字符串为:{ input }";
}
}
public void ConfigureServices(IServiceCollection services)
{
services.Add(new ServiceDescriptor(serviceType: typeof(IFoo),
implementationType: typeof(Foo),
lifetime: ServiceLifetime.Transient));
}
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"DIServices": [
{
"serviceType": "[namesapce].IFoo",
"implementationType": "[namesapce].Foo",
"lifetime": "Transient"
}
]
}
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class Service
{
public string ServiceType { get; set; }
public string ImplementationType { get;set; }
[JsonConverter(typeof(StringEnumConverter))]
public ServiceLifetime Lifetime { get; set; }
}
public void ConfigureServices(IServiceCollection services)
{
//services.Add(new ServiceDescriptor(serviceType: typeof(IFoo),
// implementationType: typeof(Foo),
// lifetime: ServiceLifetime.Transient));
var jsonServices = JObject.Parse(File.ReadAllText("appSettings.json"))["DIServices"];
var requiredServices = JsonConvert.DeserializeObject<List<Service>>(jsonServices.ToString());
foreach (var service in requiredServices) {
services.Add(new ServiceDescriptor(serviceType: Type.GetType(service.ServiceType),
implementationType: Type.GetType(service.ImplementationType),
lifetime: service.Lifetime));
}
}
public class HomeController : Controller
{
private readonly IFoo _foo;
public HomeController(IFoo foo)
{
_foo = foo;
}
public IActionResult About()
{
ViewData["Message"] = _foo.GetInputString("Your application description page.");
return View();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有