【问题标题】:Add startup class in .Net Core Class Library在 .Net Core 类库中添加启动类
【发布时间】:2020-08-27 18:17:05
【问题描述】:

我正在尝试在新的 .Net 核心类库项目中添加 OWIN 启动类。我已经安装了包 Microsoft.AspNetCore.Owin 包。但我仍然没有在添加新项目向导中看到创建 OWIN 启动类的选项。它以前曾在.Net 类库中。 .Net Core 类库有区别吗?

我基本上想为我的 SingalR 集线器创建一个单独的项目,并通过引用它在任何我想要的地方使用它。

【问题讨论】:

  • 模板因项目类型而异。我认为 OWIN Startup 仅在 ASP.NET Core 项目中。您可以在 ASP.NET Core 项目中创建类并在库项目中复制/粘贴。
  • 我可以将类从 web 项目复制到库项目但会执行?
  • 为什么要使用 OWIN 而不是使用具有精心设计的中间件基础架构的 ASP.net 核心?
  • @Peter 那么如何在类库项目中配置 SignalR 服务呢?所有的教程都只是展示了如何在一个 web 项目中添加,服务是在启动类中配置的。

标签: c# asp.net-core signalr signalr-hub


【解决方案1】:

这与 Visual Studio 的工具有关。当您处理 Web 项目时,Visual Studio 会识别这一点并在“添加新项向导”中显示 Web 选项。由于您在类库项目中工作,Visual Studio 认为您不需要基于 Web 的选项,因此不提供它。幸运的是,您想要的启动类是一个带有一些约定的普通类。您应该能够在您的类库项目中添加一个名为 startup 的类,并为其提供以下定义以获得您想要的:

using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace MyClassLibrary
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
        }
    }
}

【讨论】:

  • 它在哪里调用,或者如何调用它?
  • @Marc.2377 你的意思是 Startup 类吗?如果您在谈论 Startup 类,那么您不要调用它。您将您的启动类告诉 ASP.NET,并让框架处理调用它。更多信息可以在这里找到:docs.microsoft.com/en-us/aspnet/core/fundamentals/startup
【解决方案2】:

一旦我创建了一个派生自Microsoft.AspNetCore.SignalR.Hub<IChatClient>ChatHub

所有组件都位于单独的 .net 标准库中。

IChatClient 看起来像(用于类型安全):

public interface IChatClient
{
    Task ReceiveChatMessage(string user, string message, DateTime sentAt, bool isMarkedAsImportant);

    Task ReceiveChatActivity(string user, Activity activity, DateTime sentAt);
}

最后我在一个 ASP.net 核心项目中使用了 ChatHub,其中集线器在 Startup 中配置,如下所示:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseCors(builder =>
                    {
                        builder.WithOrigins("https://localhost:3000")
                               .AllowAnyHeader()
                               .AllowAnyMethod()
                               .AllowCredentials();
                    });
        IdentityModelEventSource.ShowPII = true;
    }
    else
    {
        app.UseGlobalExceptionHandler();

        app.UseHttpsRedirection();
        app.NwebSecApiSetup();
    }

    app.UseDefaultFiles();
    app.UseStaticFiles();

    app.UseRouting();
    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
                     {
                         endpoints.MapControllers();
                         endpoints.MapHub<ChatHub>("/api/chat");
                         endpoints.MapHub<EventHub>("/api/events");
                     });
}

此外,我在 ConfigureServices 方法中为 SignalR 配置了更多内容:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors();
    services.AddControllers().AddControllersAsServices();

    services.AddHttpContextAccessor();
    services.AddConnections();
    services.AddSignalR(options =>
                        {
                            options.EnableDetailedErrors = true;
                        })
            .AddNewtonsoftJsonProtocol();

    ...
}

我想您也可以在其他项目中轻松使用此类 Hub。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 2017-12-16
    相关资源
    最近更新 更多