Created two WebAssembly projects and two servers in one solution both clients should work with each server

39 Views Asked by At

In a wasm project I added a second client and a second server, using MapWhen, The two clients work with the first server project, And I switch between them by changing ports: 5001 and 5002.

But the problem is in the second server, Server2, Only Client2 works with server2, But for Client1 and server2, the page loads but its api methods are not working and return a web page (when request with postman):

I decorate the api controller in server2 with:

[Route("Client/api/[controller]")]
[Route("Client2/api/[controller]")]

server2 launchSettings:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:19590",//19590
      "sslPort": 44328 //44328
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "http://localhost:5025",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "https://localhost:7201;http://localhost:5025",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "OTS.UI.Server2": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "https://localhost:5001;https://localhost:5002;",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Client1 launchSettings:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:45144",
      "sslPort": 44367
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "http://localhost:5025",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "https://localhost:7201;http://localhost:5025",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "OTS.UI.Client": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

MapWhens in Server2 project.

app.MapWhen(ctx => ctx.Request.Host.Port == 5001 ||
                                   ctx.Request.Host.Equals("firstapp.com"), first =>
                                   {
                                       first.Use((ctx, nxt) =>
                                       {
                                           ctx.Request.Path = "/Client" + ctx.Request.Path;
                                           return nxt();
                                       });

                                       app.UseExceptionHandler();
                                       first.UseBlazorFrameworkFiles("/Client");
                                       first.UseStaticFiles();
                                       first.UseStaticFiles("/Client");
                                       first.UseRouting();
                                       app.UseAuthentication();
                                       app.UseAuthorization();
                                       app.UseSession();
                                       first.UseEndpoints(endpoints =>
                                       {
                                           endpoints.MapRazorPages();
                                           endpoints.MapControllers();
                                           endpoints.MapFallbackToFile("/Client/{*path:nonfile}",
                                               "Client/index.html");
                                       });

                                   });

app.MapWhen(ctx => ctx.Request.Host.Port == 5002 ||
                   ctx.Request.Host.Equals("secondapp.com"), second =>
                   {
                       second.Use((ctx, nxt) =>
                       {
                           ctx.Request.Path = "/Client2" + ctx.Request.Path;
                           return nxt();
                       });

                       app.UseExceptionHandler();
                       second.UseBlazorFrameworkFiles("/Client2");
                       second.UseStaticFiles();
                       second.UseStaticFiles("/Client2");
                       second.UseRouting();
                       app.UseAuthentication();
                       app.UseAuthorization();
                       app.UseSession();
                       second.UseEndpoints(endpoints =>
                       {
                           endpoints.MapRazorPages();
                           endpoints.MapControllers();
                           endpoints.MapFallbackToFile("/Client2/{*path:nonfile}",
                                           "Client2/index.html");
                       });

                   });

Of course I added the second server and Client projects later but the server1 and clien1 were there when the project first created.

How to make server2 and client1 work?

Server1 works with both clients and server2 only works with client2 but not client1

0

There are 0 best solutions below