Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Including DataPortalController in Blazor.Server #936

Open
animjn opened this issue Apr 22, 2020 · 4 comments
Open

Including DataPortalController in Blazor.Server #936

animjn opened this issue Apr 22, 2020 · 4 comments

Comments

@animjn
Copy link

animjn commented Apr 22, 2020

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 rockfordlhotka transferred this issue from MarimerLLC/csla Apr 22, 2020
@rockfordlhotka
Copy link
Member

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
Copy link
Author

animjn commented Apr 23, 2020

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
Copy link
Member

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
Copy link
Member

rockfordlhotka commented Apr 24, 2020

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();
    }
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants