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> <ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.Blazor.Server\Microsoft.Blazor.Server.csproj" /> <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> </ItemGroup>
</Project> </Project>

View File

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

View File

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

View File

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

View File

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

View File

@ -1,11 +1,10 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.AspNetCore.StaticFiles;
using Microsoft.Blazor.Server;
using Microsoft.Blazor.Server.FrameworkFiles; using Microsoft.Blazor.Server.FrameworkFiles;
using Microsoft.Blazor.Server.WebRootFiles; using Microsoft.Blazor.Server.WebRootFiles;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net.Mime; using System.Net.Mime;
@ -16,18 +15,28 @@ namespace Microsoft.AspNetCore.Builder
{ {
public static void UseBlazor( public static void UseBlazor(
this IApplicationBuilder applicationBuilder, this IApplicationBuilder applicationBuilder,
string assemblyPath, string clientAssemblyName)
string staticFilesRoot)
{ {
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( var webRootFileProvider = WebRootFileProvider.Instantiate(
clientWebRoot, config.WebRootPath,
Path.GetFileNameWithoutExtension(assemblyPath), Path.GetFileNameWithoutExtension(config.SourceOutputAssemblyPath),
frameworkFileProvider.GetDirectoryContents("/_bin")); frameworkFileProvider.GetDirectoryContents("/_bin"));
applicationBuilder.UseDefaultFiles(new DefaultFilesOptions 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; using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Microsoft.Blazor.DevHost")]
[assembly: InternalsVisibleTo("Microsoft.Blazor.Server.Test")] [assembly: InternalsVisibleTo("Microsoft.Blazor.Server.Test")]