Use (project).blazor.config to allow calling UseBlazor with just an assembly name. Allows ReferenceOutputAssembly=false.

This commit is contained in:
Steve Sanderson 2017-12-12 19:48:15 +00:00
parent d0096ec78e
commit 91e1cd1030
8 changed files with 63 additions and 23 deletions

View File

@ -10,7 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Blazor.Server\Microsoft.Blazor.Server.csproj" />
<ProjectReference Include="..\HostedInAspNet.Client\HostedInAspNet.Client.csproj" />
<ProjectReference Include="..\HostedInAspNet.Client\HostedInAspNet.Client.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>

View File

@ -24,9 +24,7 @@ namespace HostedInAspNet.Server
app.UseDeveloperExceptionPage();
}
app.UseBlazor(
assemblyPath: typeof(Client.Program).Assembly.Location,
staticFilesRoot: "../HostedInAspNet.Client/wwwroot");
app.UseBlazor("HostedInAspNet.Client");
}
}
}

View File

@ -10,7 +10,7 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Blazor.Server\Microsoft.Blazor.Server.csproj" />
<ProjectReference Include="..\MonoSanityClient\MonoSanityClient.csproj" />
<ProjectReference Include="..\MonoSanityClient\MonoSanityClient.csproj" ReferenceOutputAssembly="false" />
</ItemGroup>
</Project>

View File

@ -12,9 +12,7 @@ namespace MonoSanity
{
app.UseDeveloperExceptionPage();
app.UseFileServer(new FileServerOptions { EnableDefaultFiles = true });
app.UseBlazor(
assemblyPath: typeof(MonoSanityClient.Examples).Assembly.Location,
staticFilesRoot: null);
app.UseBlazor("MonoSanityClient");
}
}
}

View File

@ -20,12 +20,12 @@ namespace Microsoft.Blazor.DevHost.Server
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseBlazor(
assemblyPath: FindClientAssembly(app).Location,
staticFilesRoot: "wwwroot");
var clientAssemblyPath = FindClientAssemblyPath(app);
app.UseBlazorInternal(clientAssemblyPath);
}
private static Assembly FindClientAssembly(IApplicationBuilder app)
private static string FindClientAssemblyPath(IApplicationBuilder app)
{
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
var contentRoot = env.ContentRootPath;
@ -37,7 +37,7 @@ namespace Microsoft.Blazor.DevHost.Server
throw new FileNotFoundException($"Could not locate application assembly at expected location {assemblyPath}");
}
return Assembly.LoadFile(assemblyPath);
return assemblyPath;
}
private static string FindClientBinDir(string clientAppSourceRoot)

View File

@ -1,11 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Blazor.Server;
using Microsoft.Blazor.Server.FrameworkFiles;
using Microsoft.Blazor.Server.WebRootFiles;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
@ -16,18 +15,28 @@ namespace Microsoft.AspNetCore.Builder
{
public static void UseBlazor(
this IApplicationBuilder applicationBuilder,
string assemblyPath,
string staticFilesRoot)
string clientAssemblyName)
{
var frameworkFileProvider = FrameworkFileProvider.Instantiate(assemblyPath);
var binDir = Path.GetDirectoryName(typeof(BlazorConfig).Assembly.Location);
var clientAssemblyPath = Path.Combine(binDir, $"{clientAssemblyName}.dll");
applicationBuilder.UseBlazorInternal(clientAssemblyPath);
}
if (staticFilesRoot != null)
// TODO: Change this combination of APIs to make it possible to supply either
// an assembly name (resolved to current bin dir) or full assembly path
internal static void UseBlazorInternal(
this IApplicationBuilder applicationBuilder,
string clientAssemblyPath)
{
var config = BlazorConfig.Read(clientAssemblyPath);
var frameworkFileProvider = FrameworkFileProvider.Instantiate(
config.SourceOutputAssemblyPath);
if (config.WebRootPath != null)
{
var env = applicationBuilder.ApplicationServices.GetRequiredService<IHostingEnvironment>();
var clientWebRoot = Path.GetFullPath(Path.Combine(env.ContentRootPath, staticFilesRoot));
var webRootFileProvider = WebRootFileProvider.Instantiate(
clientWebRoot,
Path.GetFileNameWithoutExtension(assemblyPath),
config.WebRootPath,
Path.GetFileNameWithoutExtension(config.SourceOutputAssemblyPath),
frameworkFileProvider.GetDirectoryContents("/_bin"));
applicationBuilder.UseDefaultFiles(new DefaultFilesOptions

View File

@ -0,0 +1,34 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.IO;
using System.Linq;
namespace Microsoft.Blazor.Server
{
internal class BlazorConfig
{
public string SourceMSBuildPath { get; }
public string SourceOutputAssemblyPath { get; }
public string WebRootPath { get; }
public static BlazorConfig Read(string assemblyPath)
=> new BlazorConfig(assemblyPath);
private BlazorConfig(string assemblyPath)
{
var configFilePath = Path.ChangeExtension(assemblyPath, ".blazor.config");
var configLines = File.ReadLines(configFilePath).ToList();
SourceMSBuildPath = configLines[0];
var sourceMsBuildDir = Path.GetDirectoryName(SourceMSBuildPath);
SourceOutputAssemblyPath = Path.Combine(sourceMsBuildDir, configLines[1]);
var webRootPath = Path.Combine(sourceMsBuildDir, "wwwroot");
if (Directory.Exists(webRootPath))
{
WebRootPath = webRootPath;
}
}
}
}

View File

@ -1,3 +1,4 @@
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.Blazor.DevHost")]
[assembly: InternalsVisibleTo("Microsoft.Blazor.Server.Test")]