gpt4 book ai didi

c# - 手动创建 IWebHostEnvironment asp.net core 3.1

转载 作者:行者123 更新时间:2023-12-03 20:26:08 33 4
gpt4 key购买 nike

在 asp.net core 2.1 中,我可以创建 IHostingEnvironment像这样:

public IHostingEnvironment CreateHostingEnvironment()
{
var hosting = new HostingEnvironment()
{
EnvironmentName = "IntegrationTests"
};
return hosting;
}

在 Asp.net core 3.1 中它被更改为 IWebHostEnvironment但我需要以类似的方式创建它。可能的目标是创建此对象并设置环境名称。
public IWebHostEnvironment CreateWebHostEnvironment()
{
var host = new WebHostEnvironment(); // ???
}

最佳答案

唯一内置implementationIWebHostEnvironment接口(interface)在 ASP.NET Core 3.x 中是内部的:

internal class HostingEnvironment : IHostingEnvironment, Extensions.Hosting.IHostingEnvironment, IWebHostEnvironment
{
public string EnvironmentName { get; set; } = Extensions.Hosting.Environments.Production;

public string ApplicationName { get; set; }

public string WebRootPath { get; set; }

public IFileProvider WebRootFileProvider { get; set; }

public string ContentRootPath { get; set; }

public IFileProvider ContentRootFileProvider { get; set; }
}

因此,如果您出于某种原因需要创建实现接口(interface)的类的实例,您基本上可以将上述代码复制到您的项目中,并可能更改类的名称。然后,您可以根据您的要求创建它的实例。

无论如何,框架只依赖于接口(interface)。

关于c# - 手动创建 IWebHostEnvironment asp.net core 3.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60883340/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com