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.Azure.ConfigurationManager;
11 : using Cqrs.Hosts;
12 : #if NET472
13 : using Microsoft.Azure.WebJobs;
14 : #endif
15 : #if NETCOREAPP3_0
16 : using Microsoft.Extensions.Hosting;
17 : #endif
18 :
19 : namespace Cqrs.Azure.WebJobs
20 : {
21 : /// <summary>
22 : /// Execute command and event handlers in an Azure WebJob
23 : /// </summary>
24 : public abstract class CqrsJobHost<TAuthenticationToken> : TelemetryCoreHost<TAuthenticationToken>
25 1 : {
26 : /// <summary>
27 : /// The <see cref="CoreHost"/> that gets executed by the Azure WebJob.
28 : /// </summary>
29 : protected static CoreHost<TAuthenticationToken> CoreHost { get; set; }
30 :
31 : #if NET472
32 : /// <summary>
33 : /// An <see cref="Action"/> that is execute pre <see cref="JobHost.RunAndBlock"/>.
34 : /// </summary>
35 : #endif
36 : #if NETCOREAPP3_0
37 : /// <summary>
38 : /// An <see cref="Action"/> that is execute pre <see cref="HostingAbstractionsHostExtensions.Run(IHost)"/>.
39 : /// </summary>
40 : #endif
41 : protected static Action PreRunAndBlockAction { get; set; }
42 :
43 : /// <remarks>
44 : /// Please set the following connection strings in app.config for this WebJob to run:
45 : /// AzureWebJobsDashboard and AzureWebJobsStorage
46 : /// Better yet, add them to your Azure portal so they can be changed at runtime without re-deploying or re-compiling.
47 : /// </remarks>
48 1 : protected static void StartHost()
49 : {
50 : // 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.
51 : // This actually does all the work... Just sit back and relax... but also stay in memory and don't shutdown.
52 : CoreHost.Run();
53 :
54 : #if NET472
55 : JobHost host;
56 : bool disableHostControl;
57 : // I set this to false ... just because.
58 : if (!bool.TryParse(_configurationManager.GetSetting("Cqrs.Azure.WebJobs.DisableWebJobHostControl"), out disableHostControl))
59 : disableHostControl = false;
60 :
61 : if (disableHostControl)
62 : {
63 : var jobHostConfig = new JobHostConfiguration
64 : {
65 : // https://github.com/Azure/azure-webjobs-sdk/issues/741
66 : DashboardConnectionString = null
67 : };
68 : host = new JobHost(jobHostConfig);
69 : }
70 : else
71 : host = new JobHost();
72 :
73 : if (PreRunAndBlockAction != null)
74 : PreRunAndBlockAction();
75 :
76 : // The following code ensures that the WebJob will be running continuously
77 : host.RunAndBlock();
78 : #endif
79 : #if NETCOREAPP3_0
80 : string environment = null;
81 : // I set this to false ... just because.
82 : environment = _configurationManager.GetSetting("Cqrs.Azure.WebJobs.Environment");
83 :
84 : var builder = new HostBuilder();
85 : if (!string.IsNullOrWhiteSpace(environment))
86 : builder.UseEnvironment(environment);
87 : builder.ConfigureWebJobs(builder =>
88 : {
89 : builder.AddAzureStorageCoreServices();
90 : });
91 : IHost host = builder.Build();
92 : using (host)
93 : {
94 : host.Run();
95 : }
96 : #endif
97 : }
98 : }
99 : }
|