-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Joker.OData] - ApiStartup base class for non OData AspNetCore apis
[Joker.OData] - ApiHost - host for non OData AspNetCore apis (MVC, WebApi)
- Loading branch information
1 parent
8ff6b08
commit 6c5aac1
Showing
6 changed files
with
120 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
NugetProjects/Autofac.Extensions.DependencyInjection.csprojimport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |