Re-integrate Blazor to the ABP Framework

pull/5399/head
Halil İbrahim Kalkan 5 years ago
parent a3138f4dd8
commit 8cba157614

@ -10,7 +10,16 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.1" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="3.2.1" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />
<ProjectReference Include="..\..\..\..\..\framework\src\Volo.Abp.Http.Client.IdentityModel\Volo.Abp.Http.Client.IdentityModel.csproj" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\MyCompanyName.MyProjectName.HttpApi.Client\MyCompanyName.MyProjectName.HttpApi.Client.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,50 @@
using System;
using System.Net.Http;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Autofac;
using Volo.Abp.Http.Client.IdentityModel;
using Volo.Abp.Modularity;
namespace MyCompanyName.MyProjectName.Blazor
{
[DependsOn(
typeof(AbpAutofacModule),
typeof(MyProjectNameHttpApiClientModule),
typeof(AbpHttpClientIdentityModelModule)
)]
public class MyProjectNameBlazorModule : AbpModule
{
public override void ConfigureServices(ServiceConfigurationContext context)
{
var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
var builder = context.Services.GetSingletonInstance<WebAssemblyHostBuilder>();
ConfigureAuthentication(builder);
ConfigureHttpClient(context, environment);
ConfigureUI(builder);
}
private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
{
builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("AuthServer", options.ProviderOptions);
});
}
private static void ConfigureUI(WebAssemblyHostBuilder builder)
{
builder.RootComponents.Add<App>("app");
}
private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment)
{
context.Services.AddTransient(sp => new HttpClient
{
BaseAddress = new Uri(environment.BaseAddress)
});
}
}
}

@ -1,4 +1,6 @@
@page "/"
@using Volo.Abp.Identity
@inject IProfileAppService ProfileAppService
<h1>Hello, world!</h1>
@ -8,4 +10,17 @@
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
<h2>Remote Service Test</h2>
<h4>Current user name: @_userName</h4>
<button class="btn btn-primary" @onclick="GetUserNameAsync">Click me</button>
@code
{
private string _userName = "?";
private async Task GetUserNameAsync()
{
_userName = (await ProfileAppService.GetAsync()).UserName;
}
}

@ -1,9 +1,9 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Autofac;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
namespace MyCompanyName.MyProjectName.Blazor
{
@ -12,15 +12,18 @@ namespace MyCompanyName.MyProjectName.Blazor
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("app");
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
//TODO: Should be done in the ABP framework!
builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
builder.Services.AddSingleton(builder);
builder.Services.AddOidcAuthentication(options =>
builder.Services.AddApplication<MyProjectNameBlazorModule>(opts =>
{
builder.Configuration.Bind("AuthServer", options.ProviderOptions);
opts.UseAutofac();
});
builder.ConfigureContainer(builder.Services.GetSingletonInstance<IServiceProviderFactory<ContainerBuilder>>());
await builder.Build().RunAsync();
}
}

@ -3,5 +3,10 @@
"Authority": "https://localhost:44305",
"ClientId": "MyProjectName_Blazor",
"ResponseType": "code"
}
},
"RemoteServices": {
"Default": {
"BaseUrl": "https://localhost:44305"
}
}
}

Loading…
Cancel
Save