简体   繁体   中英

Add startup class in .Net Core Class Library

I am trying to add the OWIN startup class in a new .Net core class library project. I have installed the package Microsoft.AspNetCore.Owin package. But I still don't see the option to create OWIN Startup class in Add New Items wizard. It used to be there in .Net class library earlier. Is it different in .Net Core class library?

I basically want to create a separate project for my SingalR hub and use it from wherever I want by just referencing it.

This has to do with the tooling of Visual Studio. When you are working on a web project Visual Studio recognizes this and presents web options in the Add New Items Wizard. Since you are working in a class library project Visual Studio does not think you need web based options and thus does not present it. Luckily, the startup class you want is a plain class with some conventions. You should be able to add a class called startup to your class library project and give it the following definition to get what you want:

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)
        {
        }
    }
}

Once I've created a ChatHub which derives from Microsoft.AspNetCore.SignalR.Hub<IChatClient> .

All components have been located in separate .net standard library.

IChatClient looks like (it's used for type safety):

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

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

Finally I used that ChatHub in an ASP.net core project, where the hub is configured in Startup like this:

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");
                     });
}

Additionally, I've configured something more for SignalR in the ConfigureServices method:

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

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

    ...
}

I suppose you can easily use such Hubs in other projects as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM