Start making standalone hosting work again by automatically locating the client app assembly
This commit is contained in:
parent
9117263114
commit
9ec79ae9f7
|
|
@ -25,7 +25,7 @@ namespace HostedInAspNet.Server
|
||||||
app.UseBlazorDevelopmentServer("../HostedInAspNet.Client");
|
app.UseBlazorDevelopmentServer("../HostedInAspNet.Client");
|
||||||
}
|
}
|
||||||
|
|
||||||
app.UseBlazor<Client.Program>();
|
app.UseBlazor(clientAssembly: typeof(Client.Program).Assembly);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace MonoSanity
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseFileServer();
|
app.UseFileServer();
|
||||||
app.UseBlazor<MonoSanityClient.Examples>();
|
app.UseBlazor(clientAssembly: typeof(MonoSanityClient.Examples).Assembly);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ using System.Text;
|
||||||
|
|
||||||
namespace MonoSanityClient
|
namespace MonoSanityClient
|
||||||
{
|
{
|
||||||
public class Examples
|
public static class Examples
|
||||||
{
|
{
|
||||||
public static string AddNumbers(int a, int b)
|
public static string AddNumbers(int a, int b)
|
||||||
=> (a + b).ToString();
|
=> (a + b).ToString();
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
// 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 System;
|
||||||
|
using System.Reflection;
|
||||||
using Microsoft.AspNetCore.Builder;
|
using Microsoft.AspNetCore.Builder;
|
||||||
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
namespace Microsoft.Blazor.DevHost.Server
|
namespace Microsoft.Blazor.DevHost.Server
|
||||||
{
|
{
|
||||||
|
|
@ -17,7 +21,35 @@ namespace Microsoft.Blazor.DevHost.Server
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
app.UseDeveloperExceptionPage();
|
||||||
app.UseBlazorDevelopmentServer(".");
|
app.UseBlazorDevelopmentServer(".");
|
||||||
app.UseBlazor<object>(); // TODO
|
app.UseBlazor(clientAssembly: FindClientAssembly(app));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Assembly FindClientAssembly(IApplicationBuilder app)
|
||||||
|
{
|
||||||
|
var env = app.ApplicationServices.GetRequiredService<IHostingEnvironment>();
|
||||||
|
var contentRoot = env.ContentRootPath;
|
||||||
|
var binDir = FindClientBinDir(contentRoot);
|
||||||
|
var appName = Path.GetFileName(contentRoot); // TODO: Allow for the possibility that the assembly name has been overridden
|
||||||
|
var assemblyPath = Path.Combine(binDir, $"{appName}.dll");
|
||||||
|
if (!File.Exists(assemblyPath))
|
||||||
|
{
|
||||||
|
throw new FileNotFoundException($"Could not locate application assembly at expected location {assemblyPath}");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Assembly.LoadFile(assemblyPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string FindClientBinDir(string clientAppSourceRoot)
|
||||||
|
{
|
||||||
|
var binDebugDir = Path.Combine(clientAppSourceRoot, "bin", "Debug");
|
||||||
|
var subdirectories = Directory.GetDirectories(binDebugDir);
|
||||||
|
if (subdirectories.Length != 1)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"Could not locate bin directory for Blazor app. " +
|
||||||
|
$"Expected to find exactly 1 subdirectory in '{binDebugDir}', but found {subdirectories.Length}.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Path.Combine(binDebugDir, subdirectories[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,19 +5,20 @@ using Microsoft.AspNetCore.StaticFiles;
|
||||||
using Microsoft.Blazor.Server.ClientFilesystem;
|
using Microsoft.Blazor.Server.ClientFilesystem;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Mime;
|
using System.Net.Mime;
|
||||||
|
using System.Reflection;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.Builder
|
namespace Microsoft.AspNetCore.Builder
|
||||||
{
|
{
|
||||||
public static class BlazorAppBuilderExtensions
|
public static class BlazorAppBuilderExtensions
|
||||||
{
|
{
|
||||||
public static void UseBlazor<TProgram>(this IApplicationBuilder applicationBuilder)
|
public static void UseBlazor(
|
||||||
|
this IApplicationBuilder applicationBuilder,
|
||||||
|
Assembly clientAssembly)
|
||||||
{
|
{
|
||||||
var clientAppAssembly = typeof(TProgram).Assembly;
|
|
||||||
|
|
||||||
applicationBuilder.UseStaticFiles(new StaticFileOptions
|
applicationBuilder.UseStaticFiles(new StaticFileOptions
|
||||||
{
|
{
|
||||||
RequestPath = "/_framework",
|
RequestPath = "/_framework",
|
||||||
FileProvider = ClientFileProvider.Instantiate(clientAppAssembly),
|
FileProvider = ClientFileProvider.Instantiate(clientAssembly),
|
||||||
ContentTypeProvider = CreateContentTypeProvider(),
|
ContentTypeProvider = CreateContentTypeProvider(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue