@@ -153,14 +153,10 @@ private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSett
153
153
// If running on Azure Web App, derive the host ID from the default subdomain
154
154
// Otherwise, derive it from machine name and folder name
155
155
string hostId = _settingsManager . AzureWebsiteDefaultSubdomain ;
156
+
156
157
if ( string . IsNullOrEmpty ( hostId ) )
157
158
{
158
- var sanitizedPath = Path . GetFileName ( Environment . CurrentDirectory )
159
- . Where ( char . IsLetterOrDigit )
160
- . Aggregate ( new StringBuilder ( ) , ( b , c ) => b . Append ( c ) )
161
- . ToString ( ) ;
162
-
163
- hostId = $ "{ Environment . MachineName } -{ sanitizedPath } ";
159
+ hostId = MakeValidHostId ( $ "{ Environment . MachineName } -{ Path . GetFileName ( Environment . CurrentDirectory ) } ") ;
164
160
}
165
161
166
162
if ( ! String . IsNullOrEmpty ( hostId ) )
@@ -181,6 +177,51 @@ private static ScriptHostConfiguration CreateScriptHostConfiguration(WebHostSett
181
177
return scriptHostConfig ;
182
178
}
183
179
180
+ private static string MakeValidHostId ( string id )
181
+ {
182
+ var sb = new StringBuilder ( ) ;
183
+
184
+ //filter for valid characters
185
+ foreach ( var c in id )
186
+ {
187
+ if ( c == '-' )
188
+ {
189
+ //dashes are valid
190
+ //but it cannot start with one
191
+ //nor can it have consecutive dashes
192
+ if ( sb . Length != 0 && sb [ sb . Length - 1 ] != '-' )
193
+ {
194
+ sb . Append ( c ) ;
195
+ }
196
+ }
197
+ else if ( char . IsDigit ( c ) )
198
+ {
199
+ //digits are valid
200
+ sb . Append ( c ) ;
201
+ }
202
+ else if ( char . IsLetter ( c ) )
203
+ {
204
+ //letters are valid but must be lowercase
205
+ sb . Append ( char . ToLowerInvariant ( c ) ) ;
206
+ }
207
+ }
208
+
209
+ //it cannot end with a dash
210
+ if ( sb . Length > 0 && sb [ sb . Length - 1 ] == '-' )
211
+ {
212
+ sb . Length -= 1 ;
213
+ }
214
+
215
+ //length cannot exceed 32
216
+ const int MaximumHostIdLength = 32 ;
217
+ if ( sb . Length > MaximumHostIdLength )
218
+ {
219
+ sb . Length = MaximumHostIdLength ;
220
+ }
221
+
222
+ return sb . ToString ( ) ;
223
+ }
224
+
184
225
private static void InitializeFileSystem ( string scriptPath )
185
226
{
186
227
if ( ScriptSettingsManager . Instance . IsAzureEnvironment )
0 commit comments