Skip to content

Commit 9540c6d

Browse files
authored
Upgraded HostedContent to .NET 8 WebApplication (#35)
1 parent ab4a519 commit 9540c6d

File tree

3 files changed

+34
-54
lines changed

3 files changed

+34
-54
lines changed
+31-40
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,58 @@
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;
71
using Microsoft.AspNetCore.Builder;
82
using Microsoft.AspNetCore.Hosting;
9-
using Microsoft.AspNetCore.Hosting.Server.Features;
103
using Microsoft.AspNetCore.StaticFiles;
11-
using Microsoft.Extensions.DependencyInjection;
124
using Microsoft.Extensions.FileProviders;
5+
using Microsoft.Extensions.Hosting;
136
using Microsoft.Extensions.Logging;
7+
using System.Collections.Generic;
8+
using System.IO;
9+
using System.Linq;
10+
using System.Net;
1411

1512
namespace SharpWebview.Content
1613
{
1714
public sealed class HostedContent : IWebviewContent
1815
{
19-
private readonly IWebHost _webHost;
16+
private readonly WebApplication _webApp;
2017

2118
public HostedContent(int port = 0, bool activateLog = false, IDictionary<string, string> additionalMimeTypes = null)
2219
{
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));
3122

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+
}
4027

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
5231
{
5332
FileProvider = new PhysicalFileProvider(Path.Combine(System.Environment.CurrentDirectory, "app")),
5433
RequestPath = "",
5534
EnableDirectoryBrowsing = true,
5635
};
57-
if(startupParameters.AdditionalMimeTypes != null)
36+
37+
if (additionalMimeTypes != null)
5838
{
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+
{
6143
extensionProvider.Mappings.Add(mimeType);
44+
}
45+
6246
fileServerOptions.StaticFileOptions.ContentTypeProvider = extensionProvider;
6347
}
64-
app.UseFileServer(fileServerOptions);
48+
49+
_webApp.UseFileServer(fileServerOptions);
50+
_webApp.Start();
51+
}
52+
53+
public string ToWebviewUrl()
54+
{
55+
return _webApp.Urls.First();
6556
}
6657
}
6758
}

src/SharpWebview/SharpWebview.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
4+
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
<AssemblyName>SharpWebview</AssemblyName>
77
<Authors>Gerrit 'Geaz' Gazic</Authors>
@@ -33,9 +33,9 @@
3333
</ItemGroup>
3434

3535
<ItemGroup>
36-
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
37-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
36+
<FrameworkReference Include="Microsoft.AspNetCore.App" />
3837
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
38+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
3939
</ItemGroup>
4040

4141
</Project>

src/SharpWebview/StartupParameters.cs

-11
This file was deleted.

0 commit comments

Comments
 (0)