微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决在.Net Core类库中添加启动类

我正在尝试在新的.Net核心类库项目中添加OWIN启动类。我已经安装了Microsoft.AspNetCore.Owin软件包。但是我仍然看不到“添加新项”向导中创建OWIN Startup类的选项。它曾经在.Net类库中存在过。 .Net Core类库是否有所不同?

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

解决方法

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

一旦我创建了一个 // match the group with the list: const pairs = []; for(let i = 0; i < Math.floor(Tlist.length / 2); i++) { pairs.push([ Tlist[i],Tlist[i + Math.floor(Tlist.length / 2)] ]); } if(Tlist.length % 2) pairs.push([Tlist.pop()]); console.log("pairs:",pairs.join(" | ")); const result = [].concat(...pairs); 派生的 for(let i = 0; i < result.length; i++) { var innerArrayLength = result[i].length; for (let j = 0; j < innerArrayLength; j++) { //console.log('[' + i + ',' + j + '] = ' + result[i][j]); console.log(result[i] + " -> " + result[(i + 2) % result.length]); }

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

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

Microsoft.AspNetCore.SignalR.Hub<IChatClient>

最后,我在ASP.net核心项目中使用了IChatClient,其中枢纽在public interface IChatClient { Task ReceiveChatMessage(string user,string message,DateTime sentAt,bool isMarkedAsImportant); Task ReceiveChatActivity(string user,Activity activity,DateTime sentAt); } 中的配置如下:

ChatHub

此外,我还在Startup方法中为SignalR配置了其他功能:

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。