|
1 |
| -using System.Collections.Generic; |
2 |
| -using System.IO; |
3 |
| -using System.Linq; |
4 |
| -using System.Net; |
5 |
| -using System.Reflection; |
6 |
| -using Microsoft.AspNetCore; |
7 | 1 | using Microsoft.AspNetCore.Builder;
|
8 | 2 | using Microsoft.AspNetCore.Hosting;
|
9 |
| -using Microsoft.AspNetCore.Hosting.Server.Features; |
10 | 3 | using Microsoft.AspNetCore.StaticFiles;
|
11 |
| -using Microsoft.Extensions.DependencyInjection; |
12 | 4 | using Microsoft.Extensions.FileProviders;
|
| 5 | +using Microsoft.Extensions.Hosting; |
13 | 6 | using Microsoft.Extensions.Logging;
|
| 7 | +using System.Collections.Generic; |
| 8 | +using System.IO; |
| 9 | +using System.Linq; |
| 10 | +using System.Net; |
14 | 11 |
|
15 | 12 | namespace SharpWebview.Content
|
16 | 13 | {
|
17 | 14 | public sealed class HostedContent : IWebviewContent
|
18 | 15 | {
|
19 |
| - private readonly IWebHost _webHost; |
| 16 | + private readonly WebApplication _webApp; |
20 | 17 |
|
21 | 18 | public HostedContent(int port = 0, bool activateLog = false, IDictionary<string, string> additionalMimeTypes = null)
|
22 | 19 | {
|
23 |
| - _webHost = WebHost.CreateDefaultBuilder() |
24 |
| - .ConfigureServices(s => s.AddSingleton(x => new StartupParameters() { AdditionalMimeTypes = additionalMimeTypes })) |
25 |
| - .UseStartup<Startup>() |
26 |
| - .UseKestrel(options => options.Listen(IPAddress.Loopback, port)) |
27 |
| - .ConfigureLogging((logger) => { if(!activateLog) logger.ClearProviders(); }) |
28 |
| - .Build(); |
29 |
| - _webHost.Start(); |
30 |
| - } |
| 20 | + WebApplicationBuilder builder = WebApplication.CreateBuilder(); |
| 21 | + builder.WebHost.UseKestrel(options => options.Listen(IPAddress.Loopback, port)); |
31 | 22 |
|
32 |
| - public string ToWebviewUrl() |
33 |
| - { |
34 |
| - return _webHost.ServerFeatures |
35 |
| - .Get<IServerAddressesFeature>() |
36 |
| - .Addresses |
37 |
| - .First(); |
38 |
| - } |
39 |
| - } |
| 23 | + if (!activateLog) |
| 24 | + { |
| 25 | + builder.Logging.ClearProviders(); |
| 26 | + } |
40 | 27 |
|
41 |
| - internal sealed class Startup |
42 |
| - { |
43 |
| - private StartupParameters startupParameters; |
44 |
| - public Startup(StartupParameters startupParameters) |
45 |
| - { |
46 |
| - this.startupParameters = startupParameters; |
47 |
| - } |
48 |
| - public void ConfigureServices(IServiceCollection services) { } |
49 |
| - public void Configure(IApplicationBuilder app, IHostingEnvironment env) |
50 |
| - { |
51 |
| - var fileServerOptions = new FileServerOptions |
| 28 | + _webApp = builder.Build(); |
| 29 | + |
| 30 | + FileServerOptions fileServerOptions = new FileServerOptions |
52 | 31 | {
|
53 | 32 | FileProvider = new PhysicalFileProvider(Path.Combine(System.Environment.CurrentDirectory, "app")),
|
54 | 33 | RequestPath = "",
|
55 | 34 | EnableDirectoryBrowsing = true,
|
56 | 35 | };
|
57 |
| - if(startupParameters.AdditionalMimeTypes != null) |
| 36 | + |
| 37 | + if (additionalMimeTypes != null) |
58 | 38 | {
|
59 |
| - var extensionProvider = new FileExtensionContentTypeProvider(); |
60 |
| - foreach (var mimeType in startupParameters.AdditionalMimeTypes) |
| 39 | + FileExtensionContentTypeProvider extensionProvider = new FileExtensionContentTypeProvider(); |
| 40 | + |
| 41 | + foreach (var mimeType in additionalMimeTypes) |
| 42 | + { |
61 | 43 | extensionProvider.Mappings.Add(mimeType);
|
| 44 | + } |
| 45 | + |
62 | 46 | fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider;
|
63 | 47 | }
|
64 |
| - app.UseFileServer(fileServerOptions); |
| 48 | + |
| 49 | + _webApp.UseFileServer(fileServerOptions); |
| 50 | + _webApp.Start(); |
| 51 | + } |
| 52 | + |
| 53 | + public string ToWebviewUrl() |
| 54 | + { |
| 55 | + return _webApp.Urls.First(); |
65 | 56 | }
|
66 | 57 | }
|
67 | 58 | }
|
0 commit comments