在ASP.NET项目中创建和使用.NET Core类库
问题描述:
我从Visual Studio启动了一个新的ASP.NET Core WebApi项目。项目初始化并开始没有问题。在ASP.NET项目中创建和使用.NET Core类库
现在我想添加一个.NET Core类库来包含与应用程序分离的逻辑。所以我右键单击src
文件夹,选择Add -> New Project
并选择Class LOibrary (.NET Core)
。
在我的类库我创建了一个新的中间件:
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);
}
}
}
而且创建扩展方法吧:
namespace MyClassLib
{
public static class MyMiddlewareExtensions
{
public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyMiddleware>();
}
}
}
到目前为止,没有任何幻想。
在我的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-*"
}
但是当我尝试使用我的新类库在我的WebAPI startup
类似乎不承认它 - 显示using MyClassLib
红色(错误),并且不允许我使用其中的任何类。
我在这里错过了什么?以前到.NET核心这是我用来做所有的时间...
编辑
奇怪,但如果我禁用ReSharper的不是一切正常。如果我重新启用ReSharper比它再次显示错误,但仍编译。我在RC1上看到了在GitHub上遇到过这个问题的人的帖子 - 但有人提到它已经修复。任何人都知道我可以如何继续使用ReSharper和.NET Core?
答
问题可能是因为NuGet库中有一个名为MyClassLib
的existing package,它的版本号为1.0.0,所以NuGet可能会将其拉下来并引用它。
为了指导项目系统使用本地的项目,而不是发布的包,尝试做
"MyClassLib": { "target": "project" }
您好,感谢回答。请参阅原始文章中关于ReSharper的编辑 – developer82
我会检查ReSharper EAP程序。我不确定.NET Core是否得到完全支持,但他们已经在[2016.2发布](https://blog.jetbrains.com/dotnet/2016/05/27/resharper-ultimate)中完成了大量错误修正-2016-2-EAP-踢断/)。 – khellang
我与ReSharper有类似的问题,目前在EAP版本上。有时甚至有时会引用参考文献,但总体上有所改进。 – steamrolla