简体   繁体   English

在ASP.NET项目中创建和使用.NET Core类库

[英]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.

相关问题 在ASP.NET 5(Core)中添加对类库项目的引用的问题 - Issue adding reference to class library project in ASP.NET 5 (Core) asp.net core 2.2在类库中使用HttpCookieCollection - asp.net core 2.2 using HttpCookieCollection in a class library NET Standard与Net Core App:创建.NET Core Project时(使用控制台或类库) - NET Standard vs Net Core App: when creating .NET Core Project (using console or class library) 使用第三个数据类库项目在asp.net core项目和.net core控制台项目之间共享Sqlite数据库 - Share Sqlite database between asp.net core project and .net core console project with a third data class library project 是否可以从 ASP.NET Core MVC 项目中引用 .net 框架 4.7.2 class 库? - Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project? 创建项目 ASP.NET Core (.NET Core) 和 ASP.NET Core (.NET Framework) 有什么区别 - What is the difference between creating a project ASP.NET Core (.NET Core) and ASP.NET Core (.NET Framework) asp.net core 3.1中class库工程中如何获取连接字符串 - How to get connection string in class library project in asp.net core 3.1 ASP.NET MVC 和 Web API 共享类库项目 - ASP.NET MVC and Web API sharing Class Library Project ASP.NET Core MVC-使用IClientModelValidator创建RequiredIf属性 - ASP.NET Core MVC - Creating a RequiredIf Attribute using IClientModelValidator 在测试项目中使用 ASP.NET Core 的 ConfigurationBuilder - Using ASP.NET Core's ConfigurationBuilder in a Test Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM