注册服务到容器
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.BLL;
using WebNetCore5_Img_Storage.DAL;
using WebNetCore5_Img_Storage.IBLL;
using WebNetCore5_Img_Storage.IDAL;
namespace XUnitTestProject1
{
public class BaseTest
{
public readonly static IServiceCollection services = null;
public readonly static ServiceProvider serviceProvider = null;
static BaseTest()
{
services = new Microsoft.Extensions.DependencyInjection.ServiceCollection();
try
{
string path = AppDomain.CurrentDomain.BaseDirectory;
Assembly bll_impl = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.BLL.dll");
Assembly bll_interface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IBLL.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.AddScoped(item, impl);
}
}
Assembly dalAssemblys = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.DAL.dll");
Assembly dalInterface = Assembly.LoadFrom(path + "WebNetCore5_Img_Storage.IDAL.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.AddScoped(item, impl);
}
}
serviceProvider = services.BuildServiceProvider();
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
}
}
调用
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit;
using System.Collections;
using System.Collections.Generic;
[Fact]
public async Task Test5()
{
ILoginUserBLL userbll = serviceProvider.GetService<ILoginUserBLL>();
IImgCategoryBLL categoryBLL = serviceProvider.GetService<IImgCategoryBLL>();
try
{
var list = await userbll.PageUserAsync(null, 1, 10);
var page=await categoryBLL.PageImgCategoryAsync(null ,1,10);
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
ASP.NET Core MVC调用参考,构造函数注入
using Microsoft.AspNetCore.Mvc;
using MvcSimplePager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNetCore5_Img_Storage.Handler;
using WebNetCore5_Img_Storage.IBLL;
using WebNetCore5_Img_Storage.Model;
public class CategoryController : BaseController
{
private readonly IImgCategoryBLL imgCategoryBLL;
private readonly IAtlasTagBLL atlasTagBLL;
private readonly IImgBLL imgBLL;
public CategoryController(IImgCategoryBLL _imgCategoryBLL, IAtlasTagBLL _atlasTagBLL, IImgBLL _imgBLL)
{
imgCategoryBLL = _imgCategoryBLL;
atlasTagBLL = _atlasTagBLL;
imgBLL = _imgBLL;
}
public async Task<IActionResult> CategoryList(Img_category img_Category, int p = 1, int rows = 10)
{
var pageTag = await atlasTagBLL.PageAtlas_tag(null, 1, 200);
ViewBag.tagList = pageTag.List;
return View();
}
}