在 .Net Core 类库中添加启动类

3

我试图在一个新的.Net Core类库项目中添加OWIN启动类。我已经安装了Microsoft.AspNetCore.Owin包,但在“添加新项”向导中仍然看不到创建OWIN启动类的选项。在早期的.Net类库中还存在该选项。在.Net Core类库中是否有所不同?

我的目标是为我的SingalR hub创建一个单独的项目,并通过引用它从任何地方使用。


模板因项目类型而异。我认为OWIN启动项仅适用于ASP.NET Core项目。您可以在ASP.NET Core项目中创建该类,然后将其复制/粘贴到库项目中。 - vernou
我可以将网页项目中的类复制到库项目中,但会执行吗? - Sachin Parashar
1
为什么你想要使用OWIN而不是使用具有良好设计的中间件基础架构的ASP.net Core呢? - Peter
@Peter,我该如何在类库项目中配置SignalR服务?所有的教程都只是展示在Web项目中添加并在启动类中配置服务。 - Sachin Parashar
2个回答

3
这与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)
        {
        }
    }
}

1
它在哪里被调用,或者怎样调用它? - Marc.2377
@Marc.2377 你是指Startup类吗?如果你在谈论Startup类,那么你不需要调用它。你只需告诉ASP.NET关于你的启动类,让框架来处理调用它。更多信息可以在这里找到:https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup - Greg Valainis

0

我曾经创建了一个继承自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,其中hub在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();

    ...
}

我想你也可以在其他项目中轻松使用这样的集线器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接