问题描述
我正在尝试在新的.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");
});
}
我想您也可以在其他项目中轻松使用这样的集线器。