@@ -36,22 +36,22 @@ public sealed class DotNetFunctionInvoker : FunctionInvokerBase
36
36
private FunctionSignature _functionSignature ;
37
37
private IFunctionMetadataResolver _metadataResolver ;
38
38
private Action _reloadScript ;
39
+ private Action _shutdown ;
39
40
private Action _restorePackages ;
40
41
private Action < MethodInfo , object [ ] , object [ ] , object > _resultProcessor ;
41
42
private string [ ] _watchedFileTypes ;
42
43
43
- private static readonly string [ ] AssemblyFileTypes = { ".dll" , ".exe" } ;
44
-
45
44
internal DotNetFunctionInvoker ( ScriptHost host , FunctionMetadata functionMetadata ,
46
45
Collection < FunctionBinding > inputBindings , Collection < FunctionBinding > outputBindings ,
47
46
IFunctionEntryPointResolver functionEntryPointResolver , FunctionAssemblyLoader assemblyLoader ,
48
- ICompilationServiceFactory compilationServiceFactory , ITraceWriterFactory traceWriterFactory = null )
47
+ ICompilationServiceFactory compilationServiceFactory , ITraceWriterFactory traceWriterFactory = null ,
48
+ IFunctionMetadataResolver metadataResolver = null )
49
49
: base ( host , functionMetadata , traceWriterFactory )
50
50
{
51
51
_metricsLogger = Host . ScriptConfig . HostConfig . GetService < IMetricsLogger > ( ) ;
52
52
_functionEntryPointResolver = functionEntryPointResolver ;
53
53
_assemblyLoader = assemblyLoader ;
54
- _metadataResolver = new FunctionMetadataResolver ( functionMetadata , host . ScriptConfig . BindingProviders , TraceWriter ) ;
54
+ _metadataResolver = metadataResolver ?? new FunctionMetadataResolver ( functionMetadata , host . ScriptConfig . BindingProviders , TraceWriter ) ;
55
55
_compilationService = compilationServiceFactory . CreateService ( functionMetadata . ScriptType , _metadataResolver ) ;
56
56
_inputBindings = inputBindings ;
57
57
_outputBindings = outputBindings ;
@@ -66,6 +66,9 @@ internal DotNetFunctionInvoker(ScriptHost host, FunctionMetadata functionMetadat
66
66
_reloadScript = ReloadScript ;
67
67
_reloadScript = _reloadScript . Debounce ( ) ;
68
68
69
+ _shutdown = ( ) => Host . Shutdown ( ) ;
70
+ _shutdown = _shutdown . Debounce ( ) ;
71
+
69
72
_restorePackages = RestorePackages ;
70
73
_restorePackages = _restorePackages . Debounce ( ) ;
71
74
}
@@ -74,7 +77,7 @@ private void InitializeFileWatcher()
74
77
{
75
78
if ( InitializeFileWatcherIfEnabled ( ) )
76
79
{
77
- _watchedFileTypes = AssemblyFileTypes
80
+ _watchedFileTypes = ScriptConstants . AssemblyFileTypes
78
81
. Concat ( _compilationService . SupportedFileTypes )
79
82
. ToArray ( ) ;
80
83
}
@@ -84,7 +87,14 @@ protected override void OnScriptFileChanged(object sender, FileSystemEventArgs e
84
87
{
85
88
// The ScriptHost is already monitoring for changes to function.json, so we skip those
86
89
string fileExtension = Path . GetExtension ( e . Name ) ;
87
- if ( _watchedFileTypes . Contains ( fileExtension ) )
90
+ if ( ScriptConstants . AssemblyFileTypes . Contains ( fileExtension , StringComparer . OrdinalIgnoreCase ) )
91
+ {
92
+ TraceWriter . Info ( "Assembly changes detected. Restarting host..." ) ;
93
+
94
+ // As a result of an assembly change, we initiate a full host shutdown
95
+ _shutdown ( ) ;
96
+ }
97
+ else if ( _watchedFileTypes . Contains ( fileExtension ) )
88
98
{
89
99
_reloadScript ( ) ;
90
100
}
@@ -146,7 +156,7 @@ private void ReloadScript()
146
156
( _functionSignature == null ||
147
157
( _functionSignature . HasLocalTypeReference || ! _functionSignature . Equals ( signature ) ) ) )
148
158
{
149
- Host . RestartEvent . Set ( ) ;
159
+ Host . Restart ( ) ;
150
160
}
151
161
}
152
162
@@ -172,19 +182,31 @@ private void RestorePackages()
172
182
. ContinueWith ( t => t . Exception . Handle ( e => true ) , TaskContinuationOptions . OnlyOnFaulted | TaskContinuationOptions . ExecuteSynchronously ) ;
173
183
}
174
184
175
- private async Task RestorePackagesAsync ( bool reloadScriptOnSuccess = true )
185
+ internal async Task RestorePackagesAsync ( bool reloadScriptOnSuccess = true )
176
186
{
177
187
TraceOnPrimaryHost ( "Restoring packages." , TraceLevel . Info ) ;
178
188
179
189
try
180
190
{
181
- await _metadataResolver . RestorePackagesAsync ( ) ;
191
+ PackageRestoreResult result = await _metadataResolver . RestorePackagesAsync ( ) ;
182
192
183
193
TraceOnPrimaryHost ( "Packages restored." , TraceLevel . Info ) ;
184
194
185
195
if ( reloadScriptOnSuccess )
186
196
{
187
- _reloadScript ( ) ;
197
+ if ( ! result . IsInitialInstall && result . ReferencesChanged )
198
+ {
199
+ TraceOnPrimaryHost ( "Package references have changed. Restarting host..." , TraceLevel . Info ) ;
200
+
201
+ // If this is not the initial package install and references changed,
202
+ // shutdown the host, which will cause it to have a clean start and load the new
203
+ // assembly references
204
+ _shutdown ( ) ;
205
+ }
206
+ else
207
+ {
208
+ _reloadScript ( ) ;
209
+ }
188
210
}
189
211
}
190
212
catch ( Exception exc )
0 commit comments