Skip to content

Commit b5b2a16

Browse files
author
davidebbo
committed
Make host.json optional, and use a default host id from machine key
1 parent db07435 commit b5b2a16

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/WebJobs.Script.WebHost/App_Start/AutofacBootstrap.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2+
using System.Configuration;
23
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using System.Web;
7+
using System.Web.Configuration;
68
using System.Web.Hosting;
79
using Autofac;
810
using Microsoft.Azure.WebJobs.Script;
@@ -40,6 +42,15 @@ internal static void Initialize(ContainerBuilder builder)
4042
RootLogPath = logFilePath,
4143
FileLoggingEnabled = true
4244
};
45+
46+
// If there is an explicit machine key, it makes a good default host id. It can still be
47+
// overridden in host.json
48+
var section = (MachineKeySection)ConfigurationManager.GetSection("system.web/machineKey");
49+
if (section.Decryption != "Auto" && section.ValidationKey.Length >= 32)
50+
{
51+
scriptHostConfig.HostConfig.HostId = section.ValidationKey.Substring(0, 32);
52+
}
53+
4354
WebScriptHostManager scriptHostManager = new WebScriptHostManager(scriptHostConfig);
4455
builder.RegisterInstance<WebScriptHostManager>(scriptHostManager);
4556

src/WebJobs.Script/Host/ScriptHost.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Microsoft.Azure.WebJobs.Script
2121
public class ScriptHost : JobHost
2222
{
2323
private const string HostAssemblyName = "ScriptHost";
24-
private const string HostFileName = "host.json";
24+
private const string HostConfigFileName = "host.json";
2525
internal const string FunctionConfigFileName = "function.json";
2626
private readonly TraceWriter _traceWriter;
2727
private readonly AutoResetEvent _restartEvent = new AutoResetEvent(false);
@@ -125,7 +125,14 @@ protected virtual void Initialize()
125125
}
126126

127127
// read host.json and apply to JobHostConfiguration
128-
string hostConfigFilePath = Path.Combine(ScriptConfig.RootScriptPath, HostFileName);
128+
string hostConfigFilePath = Path.Combine(ScriptConfig.RootScriptPath, HostConfigFileName);
129+
130+
// If it doesn't exist, create an empty JSON file
131+
if (!File.Exists(hostConfigFilePath))
132+
{
133+
File.WriteAllText(hostConfigFilePath, "{}");
134+
}
135+
129136
_traceWriter.Verbose(string.Format("Reading host configuration file '{0}'", hostConfigFilePath));
130137
string json = File.ReadAllText(hostConfigFilePath);
131138
JObject hostConfig = JObject.Parse(json);
@@ -370,12 +377,16 @@ internal static void ApplyConfiguration(JObject config, ScriptHostConfiguration
370377
{
371378
JobHostConfiguration hostConfig = scriptConfig.HostConfig;
372379

380+
// We may already have a host id, but the one from the JSON takes precedence
373381
JToken hostId = (JToken)config["id"];
374-
if (hostId == null)
382+
if (hostId != null)
383+
{
384+
hostConfig.HostId = (string)hostId;
385+
}
386+
else if (hostConfig.HostId == null)
375387
{
376388
throw new InvalidOperationException("An 'id' must be specified in the host configuration.");
377389
}
378-
hostConfig.HostId = (string)hostId;
379390

380391
JToken watchFiles = (JToken)config["watchFiles"];
381392
if (watchFiles != null && watchFiles.Type == JTokenType.Boolean)
@@ -465,7 +476,7 @@ private void OnFileChanged(object sender, FileSystemEventArgs e)
465476
{
466477
string fileName = Path.GetFileName(e.Name);
467478

468-
if (((string.Compare(fileName, HostFileName, ignoreCase: true) == 0) || string.Compare(fileName, FunctionConfigFileName, ignoreCase: true) == 0) ||
479+
if (((string.Compare(fileName, HostConfigFileName, ignoreCase: true) == 0) || string.Compare(fileName, FunctionConfigFileName, ignoreCase: true) == 0) ||
469480
((Directory.EnumerateDirectories(ScriptConfig.RootScriptPath).Count() != _directoryCountSnapshot)))
470481
{
471482
// a host level configuration change has been made which requires a

src/WebJobs.Script/Host/ScriptHostManager.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public ScriptHost Instance
5454
{
5555
IsRunning = false;
5656

57-
_config.HostConfig = new JobHostConfiguration();
5857
ScriptHost newInstance = ScriptHost.Create(_config);
5958

6059
// TODO: consider using StartAsync here to speed up

0 commit comments

Comments
 (0)