简体   繁体   English

Angular 7 + dotnet核心+ SignalR IIS问题

[英]Angular 7 + dotnet core + SignalR IIS Issue

I'm trying to deploy an Angular 7/.net Core application on my local IIS and am running into an issue. 我正在尝试在本地IIS上部署Angular 7 / .net Core应用程序,并且遇到了问题。 I used the Angular template in Visual Studio to create a .net core backend with an Angular front-end. 我在Visual Studio中使用了Angular模板来创建带有Angular前端的.net核心后端。 I also added SignalR to both projects. 我还将SignalR添加到两个项目中。 Here are some code samples: 以下是一些代码示例:

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(options =>
    {
        options.AddPolicy("CorsPolicy",
            builder => builder
            .AllowAnyOrigin()
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowCredentials());
    });

        services.AddSignalR();
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }



public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
    else
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for 
       production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseCors("CorsPolicy");
app.UseSignalR(routes =>
{
   routes.MapHub<MyHub>("/myHub");
});

app.UseSpa(spa =>
{
    // To learn more about options for serving an Angular SPA from ASP.NET Core,
    // see https://go.microsoft.com/fwlink/?linkid=864501
   spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
        spa.UseAngularCliServer(npmScript: "start"); 
    }
});
}

connection.service.ts connection.service.ts

if (!this.hubConnection) {
    this.hubConnection = new 
    HubConnectionBuilder().withUrl('http://localhost:5000/myhub').build();
}

public start(): void {
    this.hubConnection
    .start()
    .then(() =>  {
        console.log('Connection started');
        this.startingSubject.next();
    })
    .catch((error: any) =>  this.startingSubject.error(error));
  }

data.component.ts data.component.ts

private getAllData(): Promise<Data> {
  const publishDate = this.getPublishDate();
  return this.connectionService.hubConnection.invoke("GetAllData", 
          publishDate);
}

As a quick summary, I have a connection service to handle the signalR connections on the Angular side. 快速总结,我有一个连接服务来处理Angular侧的signalR连接。 Essentially, app.component.ts calls the Start() method in connection.service.ts which starts the SignalR connection. 本质上,app.component.ts调用connection.service.ts中的Start()方法,该方法启动SignalR连接。 data.component.ts is subscribed to this event and when the connection is successful, it calls the GetAllData() method. data.component.ts订阅了此事件,连接成功后,它将调用GetAllData()方法。

I was trying to follow this tutorial in getting this set up via IIS, but can't get it to work. 我试图按照本教程通过IIS进行设置,但无法正常工作。 ( https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2 ) https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2

I Publish from Visual Studio, this creates a directory with my .net Core DLL and a ClientApp folder for my Angular site. 我从Visual Studio发布,这将为我的Angular站点创建一个包含.net Core DLL和ClientApp文件夹的目录。 If I do a dotnet myapp.dll command I can navigate to localhost:5000 and everything works great. 如果我执行dotnet myapp.dll命令,则可以导航至localhost:5000,并且一切正常。

  • localhost:5000/myhub returns a response from my signalR hub localhost:5000 / myhub从我的signalR集线器返回响应
  • localhost:5000/client shows the signalR client webpage perfectly localhost:5000 / client完美显示signalR客户端网页
  • localhost:5000/host shows the signalR host webpage perfectly. localhost:5000 / host完美显示signalR主机网页。

I should also note that this works when running through VS too. 我还应该注意,这也可以通过VS运行。 However, when I run through IIS, I get these results: 但是,当我通过IIS运行时,会得到以下结果:

  • localhost:5000/myhub returns a response from my signalR hub localhost:5000 / myhub从我的signalR集线器返回响应
  • localhost:5000/client shows the signalR client webpage perfectly localhost:5000 / client完美显示signalR客户端网页
  • localhost:5000/host fails with: 本地主机:5000 /主机失败,并显示以下信息:

ERROR Error: Uncaught (in promise): Error: An unexpected error occurred invoking 'GetAllData' on the server. 错误错误:未捕获(承诺):错误:调用服务器上的“ GetAllData”时发生意外错误。 Error: An unexpected error occurred invoking 'GetAllData' on the server. 错误:在服务器上调用“ GetAllData”时发生意外错误。

/Host does try to make a call to /myhub, which makes me wonder if IIS has an issue with this communicating with the same port or something. / Host确实尝试调用/ myhub,这使我想知道IIS是否存在与此端口或其他端口进行通信的问题。 or maybe I'm just setting up IIS wrong. 也许我只是在设置IIS错误。

Does anyone have any ideas as to how to get this working via IIS? 是否有人对如何通过IIS发挥作用有任何想法? I'm been scratching my head over this all afternoon. 我整个下午都在挠头。

Edit: After continuing to troubleshoot, it looks like the data.component.ts is successfully calling a "Connect" method on the hub just before the "GetAllData" method. 编辑:继续进行故障排除之后,好像data.component.ts在“ GetAllData”方法之前成功在集线器上调用了“ Connect”方法。

public Data GetAllData(DateTime? publishDate) 
{
   ... Logic here
}

PublishDate should allow nulls (in this scenario, null is actually being passed to this method), is it possible this isn't allowed for some reason? PublishDate应该允许空值(在这种情况下,实际上是将null传递给此方法),是否有可能由于某种原因而不允许这样做? Again, i have a hard time seeing why this would work everywhere but IIS, but I'm grasping at straws that this point. 再次,我很难理解为什么它可以在IIS之外的所有环境下工作,但是我很难理解这一点。 Seems weird that Connect() would work but GetAllData() wouldn't when they're on the same hub. 看起来奇怪的是,Connect()可以工作,但GetAllData()不在同一个集线器上时就不能工作。

One more edit The more I research, the more it looks like there is an actual exception within the GetAllData() method. 再进行一次编辑我研究得越深入,就越发现GetAllData()方法中存在实际异常。 I'm working at verifying this but I think what's happening is that I have a file path that I'm trying to access but this file path doesn't exist when the application is built. 我正在努力验证这一点,但是我认为正在发生的事情是我有一个尝试访问的文件路径,但是在构建应用程序时该文件路径不存在。 I'm not 100% sure as to why it's failing for IIS only but I'm continuing to dig. 我不是100%知道为什么它仅对IIS失败,但我仍在继续研究。 I'll post my findings in case anyone else stumbles across this very specific issue :) 我将发布我的发现,以防其他人偶然发现这个非常具体的问题:)

I may have missed it.. but where is your MyHub class? 我可能已经错过了..但是您的MyHub课程在哪里? Something like this: 像这样:

 Public class MyHub : Hub {  
  Public async Task GetAllData() {  
      *logic here for when client calls hub*  
   }  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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