MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

Including DataPortalController in Blazor.Server #936

Open animjn opened 4 years ago

animjn commented 4 years ago

Do you foresee any issues with including DataPortalController in Blazor Server Project - is there a possibility of conflict in ApplicationContext.User if I combine ProjectTracker.AppServerCore and ProjectTracker.Ui.Blazor.Server in an attempt to reduce the number of .NET Core apps?

Version and Platform CSLA version: 5.1 Platform: Blazor Server

rockfordlhotka commented 4 years ago

No, there should be no problem hosting a data portal endpoint in an ASP.NET Core site, regardless of whether it is also hosting server-side Blazor or not.

animjn commented 4 years ago

Hi! To achieve this (consolidate the two), when I add services.AddHttpContextAccessor(), which is otherwise not present in Startup.cs of ProjectTracker.AppServerCore, and try to access Dataportal I receive DataPortalException... at Csla.DataPortalClient.HttpProxy.Update

If I were to add Csla.ApplicationContext.WebContextManager = null in DataPortalController it would continue to work normally.

I'd like to get your thoughts on this. Could I be missing something?

rockfordlhotka commented 4 years ago

I think this might be fixed in 5.2.

At least the ProjectTracker I have in master has no problem when I add services.AddHttpContextAccessor() in the configuration.

Though I don't recall this problem existing in 5.1 either, but version 5.2 is when client-side Blazor will be fully supported.

rockfordlhotka commented 4 years ago

I figured it out (I think). The problem is a CORS issue - you need the CORS configuration to allow the Blazor client to call the server.

This must be a change in the latest Blazor preview, because the BlazorExample app did work, but now does not - regardless of whether the AddHttpContextAccessor call is made or not.

So here's a server-side Startup.cs that works:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Linq;
using Csla.Configuration;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace BlazorExample.Server
{
  public class Startup
  {
    private const string BlazorClientPolicy = "AllowAllOrigins";

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddCors(options =>
      {
        options.AddPolicy(BlazorClientPolicy,
          builder =>
          {
            builder
            .AllowAnyOrigin()
            .AllowAnyHeader()
            .AllowAnyMethod();
          });
      });
      services.AddMvc(
        (o) => o.EnableEndpointRouting = false)
        .AddNewtonsoftJson();
      services.AddResponseCompression(opts =>
      {
        opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                  new[] { "application/octet-stream" });
      });
      services.AddMvc((o) => o.EnableEndpointRouting = false);

      // If using Kestrel:
      services.Configure<KestrelServerOptions>(options =>
      {
        options.AllowSynchronousIO = true;
      });

      // If using IIS:
      services.Configure<IISServerOptions>(options =>
      {
        options.AllowSynchronousIO = true;
      });
      services.AddHttpContextAccessor();

      services.AddTransient(typeof(DataAccess.IPersonDal), typeof(DataAccess.Mock.PersonDal));
      services.AddCsla();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
      app.UseResponseCompression();

      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
        app.UseWebAssemblyDebugging();
      }

      app.UseCors(BlazorClientPolicy);
      app.UseBlazorFrameworkFiles();
      app.UseStaticFiles();

      app.UseRouting();

      app.UseEndpoints(endpoints =>
      {
        endpoints.MapControllers();
        endpoints.MapFallbackToFile("index.html");
      });

      app.UseCsla();
    }
  }
}