using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using WebInfoFormReport.Model;
namespace WebInfoFormReport
{
public class Startup
{
public static ServiceProvider Provider = null;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//数据层接口自动匹配
string path = AppDomain.CurrentDomain.BaseDirectory;
Assembly dalAssemblys = Assembly.LoadFrom(path + "WebInfoFormReport.DataImpl.dll");
Assembly dalInterface = Assembly.LoadFrom(path + "WebInfoFormReport.IData.dll");
var dalTypesImpl = dalAssemblys.GetTypes();
var dalTypesInterface = dalInterface.GetTypes();
foreach (var item in dalTypesInterface)
{
var name = item.Name.Substring(1);
string implDalName = name + "Impl";
var impl = dalTypesImpl.FirstOrDefault(w => w.Name.Equals(implDalName));
if (impl != null)
{
services.AddSingleton(item, impl);
}
}
//业务层接口自动匹配
Assembly bll_impl = Assembly.LoadFrom(path + "WebInfoFormReport.BusinessImpl.dll");
Assembly bll_interface = Assembly.LoadFrom(path + "WebInfoFormReport.IBusiness.dll");
var typesInterface = bll_interface.GetTypes();
var typesImpl = bll_impl.GetTypes();
foreach (var item in typesInterface)
{
var name = item.Name.Substring(1);
string implBLLImpName = name + "Impl";
var impl = typesImpl.FirstOrDefault(w => w.Name.Equals(implBLLImpName));
if (impl != null)
{
services.AddSingleton(item, impl);
}
}
Provider = services.BuildServiceProvider();
//其他类里面调用已经注册的接口类
var imgBusiness = Provider.GetService<IBusiness.IImgBusiness>();
}
}
}
.net core获取已经注册的服务,调用它的方法
最新推荐文章于 2024-07-05 10:15:02 发布