Skip to content

Commit

Permalink
[Joker.OData] - ApiStartup base class for non OData AspNetCore apis
Browse files Browse the repository at this point in the history
[Joker.OData] - ApiHost - host for non OData AspNetCore apis (MVC, WebApi)
  • Loading branch information
tomasfabian committed Oct 18, 2020
1 parent 8ff6b08 commit 6c5aac1
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 81 deletions.
2 changes: 2 additions & 0 deletions Joker.OData/ChangeLog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- extracted StartupBase from ODataStartupBase for AspNetCore applications
- added UseAuthentication (StartupSettings.UseAuthentication)
- added OnAddExtensions - ODataStartupBase calls UseAuthentication and UseAuthorization. ODataStartup calls UseRouting
- ApiStartup base class for non OData AspNetCore apis
- ApiHost - host for non OData AspNetCore apis (MVC, WebApi)

1.4.1
- fixed UseAuthorization, called between UseRouting and UseEndpoints for endpoint routing
Expand Down
77 changes: 77 additions & 0 deletions Joker.OData/Hosting/ApiHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace Joker.OData.Hosting
{
public class ApiHost<TStartup>
where TStartup : Startup.StartupBase
{
public void Run(string[] args, ODataWebHostConfig oDataWebHostConfig)
{
if (oDataWebHostConfig == null) throw new ArgumentNullException(nameof(oDataWebHostConfig));

var hostBuilder = CreateHostBuilder(args, oDataWebHostConfig);

hostBuilder.Build()
.Run();
}

public async Task RunAsync(string[] args, ODataWebHostConfig oDataWebHostConfig, CancellationToken cancellationToken = default(CancellationToken))
{
if (oDataWebHostConfig == null) throw new ArgumentNullException(nameof(oDataWebHostConfig));

var hostBuilder = CreateHostBuilder(args, oDataWebHostConfig);

await hostBuilder.Build()
.RunAsync(cancellationToken);
}

private IHostBuilder CreateHostBuilder(string[] args, ODataWebHostConfig oDataWebHostConfig)
{
oDataWebHostConfig.ContentRoot = oDataWebHostConfig.ContentRoot ?? Directory.GetCurrentDirectory();

var hostBuilder = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webHostBuilder =>
{
if (oDataWebHostConfig.Configuration != null)
webHostBuilder.UseConfiguration(oDataWebHostConfig.Configuration);
if (oDataWebHostConfig.Urls != null && oDataWebHostConfig.Urls.Any())
webHostBuilder.UseUrls(oDataWebHostConfig.Urls);
if (oDataWebHostConfig is KestrelODataWebHostConfig kestrelConfig)
{
kestrelConfig.ConfigureKestrelServer = kestrelConfig.ConfigureKestrelServer ?? (options => { });
webHostBuilder
.UseKestrel(kestrelConfig.ConfigureKestrelServer);
}
else
{
webHostBuilder
.UseIISIntegration();
}
webHostBuilder
.UseContentRoot(oDataWebHostConfig.ContentRoot ?? Directory.GetCurrentDirectory())
.UseStartup<TStartup>()
.ConfigureServices(oDataWebHostConfig?.ConfigureServices ?? (s => { }));
OnConfigureWebHostBuilder(webHostBuilder);
});

return hostBuilder;
}

protected virtual void OnConfigureWebHostBuilder(IWebHostBuilder webHostBuilder)
{
}
}
}
73 changes: 2 additions & 71 deletions Joker.OData/Hosting/ODataHost.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Joker.OData.Startup;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Joker.OData.Startup;

namespace Joker.OData.Hosting
{
public class ODataHost<TStartup>
public class ODataHost<TStartup> : ApiHost<TStartup>
where TStartup : ODataStartupBase
{
public void Run(string[] args, ODataWebHostConfig oDataWebHostConfig)
{
if (oDataWebHostConfig == null) throw new ArgumentNullException(nameof(oDataWebHostConfig));

var hostBuilder = CreateHostBuilder(args, oDataWebHostConfig);

hostBuilder.Build()
.Run();
}

public async Task RunAsync(string[] args, ODataWebHostConfig oDataWebHostConfig, CancellationToken cancellationToken = default(CancellationToken))
{
if (oDataWebHostConfig == null) throw new ArgumentNullException(nameof(oDataWebHostConfig));

var hostBuilder = CreateHostBuilder(args, oDataWebHostConfig);

await hostBuilder.Build()
.RunAsync(cancellationToken);
}

private IHostBuilder CreateHostBuilder(string[] args, ODataWebHostConfig oDataWebHostConfig)
{
oDataWebHostConfig.ContentRoot = oDataWebHostConfig.ContentRoot ?? Directory.GetCurrentDirectory();

var hostBuilder = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webHostBuilder =>
{
if (oDataWebHostConfig.Configuration != null)
webHostBuilder.UseConfiguration(oDataWebHostConfig.Configuration);
if (oDataWebHostConfig.Urls != null && oDataWebHostConfig.Urls.Any())
webHostBuilder.UseUrls(oDataWebHostConfig.Urls);
if (oDataWebHostConfig is KestrelODataWebHostConfig kestrelConfig)
{
kestrelConfig.ConfigureKestrelServer = kestrelConfig.ConfigureKestrelServer ?? (options => { });
webHostBuilder
.UseKestrel(kestrelConfig.ConfigureKestrelServer);
}
else
{
webHostBuilder
.UseIISIntegration();
}
webHostBuilder
.UseContentRoot(oDataWebHostConfig.ContentRoot ?? Directory.GetCurrentDirectory())
.UseStartup<TStartup>()
.ConfigureServices(oDataWebHostConfig?.ConfigureServices ?? (s => { }));
OnConfigureWebHostBuilder(webHostBuilder);
});

return hostBuilder;
}

protected virtual void OnConfigureWebHostBuilder(IWebHostBuilder webHostBuilder)
{
}
}
}
38 changes: 38 additions & 0 deletions Joker.OData/Startup/ApiStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Microsoft.AspNetCore.Hosting;

namespace Joker.OData.Startup
{
public abstract class ApiStartup : StartupBase
{
protected ApiStartup(IWebHostEnvironment env, bool enableEndpointRouting = true)
: base(env)
{
EnableEndpointRouting = enableEndpointRouting;
}

internal override bool EnableEndpointRouting { get; }

#region SetSettings

public StartupBase SetSettings(Action<StartupSettings> setStartupSettings)
{
if (setStartupSettings == null) throw new ArgumentNullException(nameof(setStartupSettings));

setStartupSettings(StartupSettings);

return this;
}

public StartupBase SetWebApiSettings(Action<WebApiStartupSettings> setWebApiStartupSettings)
{
if (setWebApiStartupSettings == null) throw new ArgumentNullException(nameof(setWebApiStartupSettings));

setWebApiStartupSettings(WebApiStartupSettings);

return this;
}

#endregion
}
}
9 changes: 0 additions & 9 deletions Joker.OData/Startup/StartupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@

namespace Joker.OData.Startup
{
public abstract class Startup : StartupBase
{
protected Startup(IWebHostEnvironment env)
: base(env)
{
}

internal override bool EnableEndpointRouting { get; } = true;
}
public abstract class StartupBase : DisposableObject
{
#region Fields
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.0.2" />
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="5.0.1" />
</ItemGroup>
</Project>

0 comments on commit 6c5aac1

Please sign in to comment.