[英]Creating and using .NET Core class library in ASP.NET project
I started a new ASP.NET Core WebApi project from Visual Studio. 我从Visual Studio开始了一个新的ASP.NET Core WebApi项目。 Project initialized and started without problem.
项目已初始化,并且启动没有问题。
Now I want to add a .NET Core class library to contain logic separated from the app. 现在,我想添加一个.NET Core类库以包含与应用程序分离的逻辑。 So I right-clicked the
src
folder, chose Add -> New Project
and chose Class LOibrary (.NET Core)
. 因此,我右键单击
src
文件夹,选择Class LOibrary (.NET Core)
Add -> New Project
然后选择Class LOibrary (.NET Core)
。
In my class library I created a new middleware: 在我的类库中,我创建了一个新的中间件:
namespace MyClassLib
{
public class MyMiddleware
{
private readonly RequestDelegate _next;
public MyMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await _next.Invoke(context);
}
}
}
And created extension method for it: 并为其创建扩展方法:
namespace MyClassLib
{
public static class MyMiddlewareExtensions
{
public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyMiddleware>();
}
}
}
So far nothing fancy. 到目前为止,没有任何幻想。
In my web API I added a reference to that project: 在我的Web API中,我添加了对该项目的引用:
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.0",
"type": "platform"
},
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"MyClassLib": "1.0.0-*"
}
But when I try to use my new class library in my WebApi startup
class it doesn't seem to recognize it - showing the using MyClassLib
in red (error) and not allowing me to use any of the classes inside it. 但是,当我尝试在WebApi
startup
类中使用新的类库时,似乎无法识别它- using MyClassLib
红色显示using MyClassLib
(错误),并且不允许我使用其中的任何类。
What am I missing here? 我在这里想念什么? previous to .NET Core this is something I used to do all the time...
在.NET Core之前,这是我一直都在做的事情...
EDIT 编辑
Strange, but if I disable ReSharper than everything works. 奇怪,但是如果我禁用ReSharper,则一切正常。 If I re-enable ReSharper than it shows errors again but still compiles.
如果我重新启用ReSharper,它将再次显示错误,但仍可以编译。 I've seen posts on GitHub from people who encountered this problem on RC1 - but it was mentioned that it was fixed.
我在RC1上看到过在GitHub上遇到此问题的人的帖子-但有人提到它已修复。 Anyone knows how I can continue working with ReSharper and .NET Core?
有谁知道我如何继续使用ReSharper和.NET Core?
The problem might be that there's an existing package in the NuGet gallery called MyClassLib
that has a version 1.0.0 , so NuGet is probably pulling that down and referencing that. 问题可能是NuGet画廊中有一个名为
MyClassLib
的现有软件包 ,其版本为1.0.0 ,因此NuGet可能会将其下拉并引用。
In order to instruct the project system to use the local project, instead of a published package, try doing 为了指示项目系统使用本地项目而不是已发布的程序包,请尝试执行
"MyClassLib": { "target": "project" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.