Line data Source code
1 : #region Copyright
2 : // // -----------------------------------------------------------------------
3 : // // <copyright company="Chinchilla Software Limited">
4 : // // Copyright Chinchilla Software Limited. All rights reserved.
5 : // // </copyright>
6 : // // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using System;
10 : using Cqrs.Authentication;
11 : using Cqrs.Hosts;
12 : using Cqrs.Ninject.Azure.Wcf;
13 : using Cqrs.Ninject.Azure.WebJobs.Configuration;
14 : #if NET472
15 : using Microsoft.Azure.WebJobs;
16 : #endif
17 : #if NETCOREAPP3_0
18 : using Microsoft.Extensions.Hosting;
19 : #endif
20 :
21 : namespace Cqrs.Ninject.Azure.WebJobs
22 : {
23 : /// <summary>
24 : /// Execute command and event handlers in an Azure WebJob using Ninject
25 : /// </summary>
26 : public class CqrsNinjectJobHost<TAuthenticationToken, TAuthenticationTokenHelper> : CqrsWebHost<TAuthenticationToken, TAuthenticationTokenHelper, WebJobHostModule>
27 : where TAuthenticationTokenHelper : class, IAuthenticationTokenHelper<TAuthenticationToken>
28 1 : {
29 : /// <summary>
30 : /// The <see cref="CoreHost"/> to run, WebJobs are console apps and console apps start via a static method, this is the instance
31 : /// that static method will actually run.
32 : /// </summary>
33 : protected static CoreHost<TAuthenticationToken> CoreHost { get; set; }
34 :
35 : #if NET472
36 : /// <summary>
37 : /// An <see cref="Action"/> that is execute pre <see cref="JobHost.RunAndBlock"/>.
38 : /// </summary>
39 : #endif
40 : #if NETCOREAPP3_0
41 : /// <summary>
42 : /// An <see cref="Action"/> that is execute pre <see cref="HostingAbstractionsHostExtensions.Run(IHost)"/>.
43 : /// </summary>
44 : #endif
45 : protected static Action PreRunAndBlockAction { get; set; }
46 :
47 : /// <remarks>
48 : /// Please set the following connection strings in app.config for this WebJob to run:
49 : /// AzureWebJobsDashboard and AzureWebJobsStorage
50 : /// Better yet, add them to your Azure portal so they can be changed at runtime without re-deploying or re-compiling.
51 : /// </remarks>
52 1 : protected static void StartHost()
53 : {
54 : // We use console state as, even though a webjob runs in an azure website, it's technically loaded via something call the 'WindowsScriptHost', which is not web and IIS based so it's threading model is very different and more console based.
55 : // This actually does all the work... Just sit back and relax... but also stay in memory and don't shutdown.
56 : CoreHost.Run();
57 :
58 : #if NET472
59 : JobHost host;
60 : bool disableHostControl;
61 : // I set this to false ... just because.
62 : if (!bool.TryParse(_configurationManager.GetSetting("Cqrs.Azure.WebJobs.DisableWebJobHostControl"), out disableHostControl))
63 : disableHostControl = false;
64 :
65 : if (disableHostControl)
66 : {
67 : var jobHostConfig = new JobHostConfiguration
68 : {
69 : // https://github.com/Azure/azure-webjobs-sdk/issues/741
70 : DashboardConnectionString = null
71 : };
72 : host = new JobHost(jobHostConfig);
73 : }
74 : else
75 : host = new JobHost();
76 :
77 : if (PreRunAndBlockAction != null)
78 : PreRunAndBlockAction();
79 :
80 : // The following code ensures that the WebJob will be running continuously
81 : host.RunAndBlock();
82 : #endif
83 : #if NETCOREAPP3_0
84 : string environment = null;
85 : // I set this to false ... just because.
86 : environment = _configurationManager.GetSetting("Cqrs.Azure.WebJobs.Environment");
87 :
88 : var builder = new HostBuilder();
89 : if (!string.IsNullOrWhiteSpace(environment))
90 : builder.UseEnvironment(environment);
91 : builder.ConfigureWebJobs(builder =>
92 : {
93 : builder.AddAzureStorageCoreServices();
94 : });
95 : IHost host = builder.Build();
96 : using (host)
97 : {
98 : host.Run();
99 : }
100 : #endif
101 : }
102 : }
103 : }
|