第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Net Core:從WebApplicationFactory查找服務(wù)

Net Core:從WebApplicationFactory查找服務(wù)

C#
繁星淼淼 2023-08-13 16:06:40
如何從 CustomWebApplicationFactory 獲取所有服務(wù)?Startup類對(duì)IDepartmentRepository和IDepartmentAppService進(jìn)行了依賴注入。想要獲取新的 DI 存儲(chǔ)庫或服務(wù)。我們正在創(chuàng)建指向原始應(yīng)用程序啟動(dòng)的集成測(cè)試。namespace Integrationtest{    public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class    {        protected override void ConfigureWebHost(IWebHostBuilder builder)        {            builder.ConfigureAppConfiguration((hostingContext, configurationBuilder) =>            {                var type = typeof(TStartup);                var path = @"C:\\OriginalApplicationWebAPI";                configurationBuilder.AddJsonFile($"{path}\\appsettings.json", optional: true, reloadOnChange: true);                configurationBuilder.AddEnvironmentVariables();            });        }    }}public class DepartmentAppServiceTest : IClassFixture<CustomWebApplicationFactory<OriginalApplication.Startup>>{    public testDbContext context;    private readonly HttpClient _client;    public DepartmentAppServiceTest(CustomWebApplicationFactory<OriginalApplication.Startup> factory)    {        _client = factory.CreateClient();        factory.  // trying to find services here for IDepartmentRepository     }https://fullstackmark.com/post/20/painless-integration-testing-with-aspnet-core-web-api
查看完整描述

2 回答

?
皈依舞

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超3個(gè)贊

是factory.Host.Services。不要忘記,您需要?jiǎng)?chuàng)建一個(gè)范圍來訪問范圍服務(wù),例如您的上下文:


using (var scope = _factory.Host.Services.CreateScope())

{

    var foo = scope.ServiceProvider.GetRequiredService<Foo>();

    // do something

}


查看完整回答
反對(duì) 回復(fù) 2023-08-13
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

收到錯(cuò)誤:

“CustomWebApplicationFactory”不包含“Host”的定義,并且找不到接受“CustomWebApplicationFactory”類型的第一個(gè)參數(shù)的可訪問擴(kuò)展方法“Host”(您是否缺少 using 指令或程序集引用?)

因此,我必須在添加服務(wù)器的情況下使用它,如下所示:

using (var scope = _factory.Server.Host.Services.CreateScope())
{
    // do something
    }


查看完整回答
反對(duì) 回復(fù) 2023-08-13
  • 2 回答
  • 0 關(guān)注
  • 145 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)