1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
7
+ using System . Net . Http ;
8
+ using Microsoft . Azure . WebJobs . Host ;
9
+ using Microsoft . Azure . WebJobs . Script . Config ;
10
+
11
+ namespace Microsoft . Azure . WebJobs . Script . WebHost
12
+ {
13
+ /// <summary>
14
+ /// Provides the current HostName for the Function App.
15
+ /// <remarks>
16
+ /// The environment value for WEBSITE_HOSTNAME is unreliable and shouldn't be used directly. AppService site swaps change
17
+ /// the site’s hostname under the covers, and the worker process is NOT recycled (for performance reasons). That means the
18
+ /// site will continue to run with the same hostname environment variable, leading to an incorrect host name.
19
+ ///
20
+ /// WAS_DEFAULT_HOSTNAME is a header injected by front end on every request which provides the correct hostname. We check
21
+ /// this header on all http requests, and updated the cached hostname value as needed.
22
+ /// </remarks>
23
+ /// </summary>
24
+ public static class HostNameProvider
25
+ {
26
+ private static string _hostName ;
27
+
28
+ public static string Value
29
+ {
30
+ get
31
+ {
32
+ if ( string . IsNullOrEmpty ( _hostName ) )
33
+ {
34
+ // default to the the value specified in environment
35
+ var settings = ScriptSettingsManager . Instance ;
36
+ _hostName = settings . GetSetting ( EnvironmentSettingNames . AzureWebsiteHostName ) ;
37
+ if ( string . IsNullOrEmpty ( _hostName ) )
38
+ {
39
+ string websiteName = settings . GetSetting ( EnvironmentSettingNames . AzureWebsiteName ) ;
40
+ if ( ! string . IsNullOrEmpty ( websiteName ) )
41
+ {
42
+ _hostName = $ "{ websiteName } .azurewebsites.net";
43
+ }
44
+ }
45
+ }
46
+ return _hostName ;
47
+ }
48
+ }
49
+
50
+ public static void Synchronize ( HttpRequestMessage request , TraceWriter traceWriter )
51
+ {
52
+ string hostNameHeaderValue = request . GetHeaderValueOrDefault ( ScriptConstants . AntaresDefaultHostNameHeader ) ;
53
+ if ( ! string . IsNullOrEmpty ( hostNameHeaderValue ) &&
54
+ string . Compare ( Value , hostNameHeaderValue ) != 0 )
55
+ {
56
+ traceWriter . InfoFormat ( "HostName updated from '{0}' to '{1}'" , Value , hostNameHeaderValue ) ;
57
+ _hostName = hostNameHeaderValue ;
58
+ }
59
+ }
60
+
61
+ internal static void Reset ( )
62
+ {
63
+ _hostName = null ;
64
+ }
65
+ }
66
+ }
0 commit comments