简体   繁体   English

在 .NetCore 库中使用 IHostingEnvironment

[英]Using IHostingEnvironment in .NetCore library

I build an ASP.NET Core application and I create a .NET Core Class Library for unit testing.我构建了一个 ASP.NET Core 应用程序,并为单元测试创​​建了一个 .NET Core 类库。

I want to use IHostingEnvironment in my library (to get physical path of a file), so I've added this line to Startup.cs of my ASP.NET Core application :我想在我的库中使用IHostingEnvironment (以获取文件的物理路径),因此我已将此行添加到我的 ASP.NET Core 应用程序的 Startup.cs 中:

services.AddSingleton<IHostingEnvironment>();

In the Library I've added reference to my ASP.NET application, and in my class I wrote this:在库中,我添加了对我的 ASP.NET 应用程序的引用,在我的类中我写了这个:

private IHostingEnvironment _env;
public Class1(IHostingEnvironment env)
{
    _env = env;
}

But when I run it then it gives me this error:但是当我运行它时,它给了我这个错误:

the following constructor parameters did not have matching fixture date : IHostingEnvironment env以下构造函数参数没有匹配的夹具日期:IHostingEnvironment env

What is the problem?问题是什么? How can I use it in .NET Core Class Library?如何在 .NET Core 类库中使用它?


EDIT: I tried to use this too:编辑:我也尝试使用它:

IServiceCollection services = new ServiceCollection();
services.AddSingleton<IHostingEnvironment>();
IServiceProvider provider = services.BuildServiceProvider();
IHostingEnvironment service = provider.GetService<IHostingEnvironment>();
var p = service.WebRootPath; 

The last one gives me this error:最后一个给了我这个错误:

Cannot instantiate implementation type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' for service type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment'无法为服务类型“Microsoft.AspNetCore.Hosting.IHostingEnvironment”实例化实现类型“Microsoft.AspNetCore.Hosting.IHostingEnvironment”

Note: services.AddSingleton<IHostingEnvironment>();注意: services.AddSingleton<IHostingEnvironment>(); means you are registering IHostingEnvironment as an implementation for IHostingEnvironment in a singleton scope (always reuse).您注册手段IHostingEnvironment为实现IHostingEnvironment在单身范围(总是重复使用)。

Since you can't create an instance of an interface, you get this error.由于您无法创建接口的实例,因此会出现此错误。

solution解决方案

define the class you want to be created (that implements IHostingEnvironment ), eg:定义要创建的类(实现IHostingEnvironment ),例如:

services.AddSingleton<IHostingEnvironment>(new HostingEnvironment());

Behind the scenes dotnet core (Hosting nuget package)后台 dotnet 核心(托管 nuget 包)

In the WebHostBuilder The first row in the constructor is:WebHostBuilder构造函数中的第一行是:

this._hostingEnvironment = (IHostingEnvironment) new HostingEnvironment();

This hosting environment is later filled with more settings, by the webhost builder.该托管环境稍后会由 webhost 构建器填充更多设置。

You should look at their github page or decompile the sources: https://github.com/aspnet/Hosting您应该查看他们的 github 页面或反编译源代码: https : //github.com/aspnet/Hosting

Note: Most of the properties/settings of HostingEnvironment are set on Build() method of the WebHostBuilder .注意: HostingEnvironment大部分属性/设置都是在HostingEnvironmentBuild()方法上WebHostBuilder If you want to moq/test this yourself you should set these properties yourself or just also include the WebHostBuilder in your test.如果您想自己进行WebHostBuilder订量/测试,您应该自己设置这些属性,或者在您的测试中也包括WebHostBuilder

For my .net class library all I had to do is install the following nuget package for version 2.1.0:对于我的 .net 类库,我所要做的就是为 2.1.0 版安装以下 nuget 包:

Microsoft.AspNetCore.Hosting.Abstractions Microsoft.AspNetCore.Hosting.Abstractions

https://www.nuget.org/packages/Microsoft.AspNetCore.Hosting.Abstractions/ https://www.nuget.org/packages/Microsoft.AspNetCore.Hosting.Abstractions/

and then I just injected IHostingEnvironment into my constructor.然后我只是将 IHostingEnvironment 注入到我的构造函数中。

I didn't even need to modify Startup.cs我什至不需要修改 Startup.cs

This worked for me in both .net core class library and console application:这在 .net 核心类库和控制台应用程序中都对我有用:

Using references,使用参考,

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Internal;

Adding DI registration,添加DI注册,

services.AddSingleton<IHostingEnvironment, HostingEnvironment>();

A note for reference as I ended up here.一个笔记供我参考,因为我在这里结束了。

If you target netstandard (netstandard2.0) in your class library, add Microsoft.Extensions.Hosting.Abstractions from NuGet to get the IHostingEnvironment interface without any implementations.如果在类库中以 netstandard (netstandard2.0) 为目标,请从 NuGet 添加 Microsoft.Extensions.Hosting.Abstractions 以获取 IHostingEnvironment 接口,而无需任何实现。

I know question specifies .net core, anyways.. might help out those being where I were.我知道问题指定了 .net 核心,无论如何......可能会帮助那些我所在的地方。

Try this, its simple enough试试这个,很简单

private IHostEnvironment env;
public Startup(IHostEnvironment env)
{
    this.env = env;
}
public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IHostEnvironment>(env);
}

then you can use it in your class然后你可以在你的课堂上使用它

private IHostingEnvironment _env;
public Class1(IHostingEnvironment env)
{
    _env = env;
}

hope it does the job ^_^希望它能完成工作^_^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM