gpt4 book ai didi

angular - 尝试从 Angular 应用程序连接到 asp.net core signalr hub 时出错

转载 作者:行者123 更新时间:2023-12-03 08:37:16 25 4
gpt4 key购买 nike

我有一个使用 Microsoft.AspNetCore.SignalR 1.1.0 和 Microsoft.Azure.SignalR 的 asp.net core 3.1 应用程序

启动信号是这样设置的

 services.AddSignalR().AddAzureSignalR("Endpoint=https://xxxxx.service.signalr.net;AccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=;Version=1.0;");
...
app.UseAzureSignalR(routes =>
{
routes.MapHub<TransactionHUB>("/updateAll");
});

在安装的 Angular 应用程序中

npm install @aspnet/signalr

我正在像这样开始连接

 public startConnection = () => {
this.hubConnection = new signalR.HubConnectionBuilder()
.withUrl('https://localhost:44391/updateAll')
.build();
this.hubConnection
.start()
.then(() => console.log('Connection started'))
.catch(err => console.log('Error while starting connection: ' + err));
}

当应用程序加载时,我收到错误。

Error: Failed to start the connection: Error: None of the transports supported by the client are supported by the server.

更新

        using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoWrapper;
using Entities;
using Infrastructure;
using Infrastructure.Contracts;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OData;
using Microsoft.OData.Edm;
using Services;
using Services.Contracts;

namespace BBBankApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

// readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// services.AddCors(options =>
// {
// options.AddPolicy(MyAllowSpecificOrigins,
// //builder =>
// //{
// // builder.WithOrigins("http://localhost:4200");
// //});
// builder => builder.AllowAnyOrigin()
//.AllowAnyMethod()
//.AllowAnyHeader());
// });
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder => builder
.WithOrigins("http://localhost:4200")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
//services.Configure<ApiBehaviorOptions>(options =>
//{
// options.SuppressModelStateInvalidFilter = true;
//});// stopping default asp.net core model state validation
services.AddControllers(config =>
{
config.Filters.Add(new ModelValidationCheckFilter());
});
services.AddScoped<ITransactionService, TransactionService>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<DbContext, BBBankContext>();
services.AddScoped<ITransactionRepository, TransactionRepository>();
services.AddScoped(typeof(IRepository<>), typeof(SQLServerRepository<>));
services.AddScoped<IAccountService, AccountService>();
var connection = @"Server=(localdb)\mssqllocaldb;Database=xxxxx;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BBBankContext>(
b => b.UseSqlServer(connection)
.UseLazyLoadingProxies(false) //Install-Package Microsoft.EntityFrameworkCore.Proxies -Version 3.1.1
//Lazy Loading means that the navigation properties will be loaded automatically as soon as they are called
//if true the odata expand will work on Transactions as well.
);

services.AddOData();
services.AddODataQueryFilter();

services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
}).AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

services.AddSignalR().AddAzureSignalR("Endpoint=https://xxxxxx.service.signalr.net;AccessKey=xxxxxxxxxxxx+9xdFN63uV8mPjIakR2ZWoJhmTk=;Version=1.0;");
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions { ShowStatusCode = true, UseApiProblemDetailsException = true, IsDebug = true, EnableExceptionLogging = true, LogRequestDataOnException = true }); //
// app.UseCustomExceptionMiddleware();
// was using this middleware only for logging. but do we really need it if Autowrapper has its own middleware
// that logs using default logging configuration of asp.net core.
app.UseCustomLogginMiddleware();
// app.UseCors(MyAllowSpecificOrigins);
app.UseCors("CorsPolicy");
app.UseMvc(b =>
{
b.MapODataServiceRoute("odata", "odata", GetEdmModel());
});
app.UseHttpsRedirection();



app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();

});

app.UseAzureSignalR(routes =>
{
routes.MapHub<TransactionHUB>("/updateAll");
});
}

private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EnableLowerCamelCase();
builder.EntitySet<Account>("Accounts").EntityType.Count().Page(50, 3).Expand().Filter().Select().OrderBy();
builder.EntitySet<User>("Users");
builder.EntitySet<Transaction>("Transactions");
return builder.GetEdmModel();
}
}
}

更新2

我也尝试过不同版本的microsoft-signal,但都给了我同样的错误。服务器端安装的包是这样的最近安装了“***Common”只是为了检查。不确定是否需要。 enter image description here

更新2我删除了 Azure Signalr,只在服务器端使用 Microsoft.AspNetCore.SignalR 1.1.0,在客户端使用 @microsoft/signalr@latest。但它是同样的错误。

最佳答案

问题出在这一行。

app.UseApiResponseAndExceptionWrapper(new AutoWrapperOptions { ShowStatusCode = true, UseApiProblemDetailsException = true, IsDebug = true, EnableExceptionLogging = true, LogRequestDataOnException = true }); //

我在添加 signalR 之前包装了响应,signalR 没有正确拾取该响应

这个安排对我来说很有效。 enter image description here

关于angular - 尝试从 Angular 应用程序连接到 asp.net core signalr hub 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63535647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com