3

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.

Sachin Parashar
  • 1,067
  • 2
  • 18
  • 28
  • The templates differ from the type of project. I think the OWIN Startup is only in ASP.NET Core project. You can create the class in ASP.NET Core projet and copy/paste in the library project. – vernou Aug 25 '20 at 17:08
  • I can copy the class from web project to library project but will execute? – Sachin Parashar Aug 25 '20 at 17:15
  • 1
    Why do you want to use OWIN and are not using ASP.net core which has a well designed middleware infrastructure? – Peter Aug 25 '20 at 19:23
  • @Peter How can I configure SignalR services in a class library project then? All tutorials just show adding in a web project and services are configured in the startup class. – Sachin Parashar Aug 27 '20 at 06:17

2 Answers2

3

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)
        {
        }
    }
}
Greg Valainis
  • 190
  • 1
  • 8
  • 1
    And where is it called, or how to call it? – Marc.2377 Jan 23 '21 at 02:44
  • @Marc.2377 do you mean the Startup class? If you are talking about the Startup class, then you don't call it. You tell ASP.NET about your startup class and let framework handle calling it. More information can be found here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/startup – Greg Valainis Jan 24 '21 at 18:22
0

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.

Peter
  • 743
  • 5
  • 26