public static void MockRun(object commandHandler, ServerlessOptions serverlessOptions, IReadOnlyDictionary <string, string> startupValues = null, IReadOnlyDictionary <string, string> adapterValues = null) { ServerlessOptions = serverlessOptions; Runner.startupValues = startupValues; AdapterValues = adapterValues; BuildMethodsDictionary(commandHandler); }
public static IServiceCollection AddServerless(this IServiceCollection services, Action <ServerlessOptions> configure = null) { var serverlessOptions = new ServerlessOptions(); if (configure != null) { configure.Invoke(serverlessOptions); } services.AddSingleton(serverlessOptions); services.AddTransient <IServerlessService, ServerlessService>(); services.AddMemoryCache(); return(services); }
public ServerlessService(ServerlessOptions serverlessOptions, IMemoryCache memoryCache, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, ICloudFilesService cloudFilesService) { this.serverlessOptions = serverlessOptions; this.memoryCache = memoryCache; this.loggerFactory = loggerFactory; this.cloudFilesService = cloudFilesService; logger = loggerFactory.CreateLogger <ServerlessService>(); // if (serverlessOptions.CloudFilesOptions == null) // { // serverlessOptions.CloudFilesOptions = serviceProvider.GetService<CloudFilesOptions>(); // } //cloudFilesOptions = serverlessOptions.CloudFilesOptions; }
private static int RunServerless(ServerlessOptions options) { var root = Path.GetFullPath(options.Root, Environment.CurrentDirectory); var outputRoot = Path.Combine(root, options.OutputDir); Directory.CreateDirectory(outputRoot); var serverlessBuilder = new ServerlessBuilder(BuildServiceDiscoveryOptions(options, root), new NamingConventions(options.Name)); var serverlessConfig = serverlessBuilder.Build(); var outputPath = Path.GetFullPath(options.Output, outputRoot); File.WriteAllText(outputPath, serverlessConfig); return(0); }
async public static Task Run(object commandHandler) { Timer idleTimer = null; try { Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; var commandLineArgs = Environment.GetCommandLineArgs(); try { ServerlessOptions = JsonConvert.DeserializeObject <ServerlessOptions>(Encoding.UTF8.GetString(Convert.FromBase64String(commandLineArgs[1]))); } catch (Exception ex) { AdapterLogger.LogWarning(ex, $"Failed to parse ServerlessOptions, using defaults instead."); ServerlessOptions = new ServerlessOptions(); } try { startupValues = new Dictionary <string, string>(JsonConvert.DeserializeObject <IDictionary <string, string> >(Encoding.UTF8.GetString(Convert.FromBase64String(commandLineArgs[2]))), StringComparer.OrdinalIgnoreCase); //if (startupValues.TryGetValue("CorrelationId", out var correlationId)) // //CorrelationId = correlationId; } catch (Exception ex) { AdapterLogger.LogWarning(ex, $"Failed to parse StartupValues."); } try { AdapterValues = new Dictionary <string, string>(JsonConvert.DeserializeObject <IDictionary <string, string> >(Encoding.UTF8.GetString(Convert.FromBase64String(commandLineArgs[3]))), StringComparer.OrdinalIgnoreCase); } catch (Exception ex) { AdapterLogger.LogWarning(ex, $"Failed to parse AdapterValues."); } var methodsDictionary = BuildMethodsDictionary(commandHandler); while (true) { idleTimer = new Timer( callback: state => { idleTimer.Dispose(); throw new TimeoutException("Timed out waiting for command."); }, state: null, dueTime: TimeSpan.FromSeconds(ServerlessOptions.IdleTimeout), period: Timeout.InfiniteTimeSpan); var input = await Console.In.ReadLineAsync(); try { idleTimer.Dispose(); if (input == Constants.QuitCommand) { break; } if (input == null) { continue; } var inputSegments = input.Split(Constants.Delimiter); if (inputSegments.Length != 4) { throw new Exception("Wrong data format."); } if (inputSegments[1] == Constants.ExpectedCommand) { var expectedValuesString = JsonConvert.SerializeObject(expectedStartupValues); await Console.Out.WriteLineAsync($"{Constants.Delimiter}{expectedValuesString.Replace("\n", Constants.NewLineIdentifier).Replace("\r", "")}{Constants.Delimiter}"); continue; } if (methodsDictionary.TryGetValue(inputSegments[1], out var handlerMethodInfo)) { object result = null; object inputTyped = null; if (inputSegments[2] != Constants.NullIdentifier && handlerMethodInfo.ParameterType != null) { var inputDenormalized = inputSegments[2].Replace(Constants.NewLineIdentifier, "\n"); if (handlerMethodInfo.ParameterType == typeof(string)) { inputTyped = inputDenormalized; } else if (handlerMethodInfo.ParameterType.IsPrimitive) { inputTyped = Convert.ChangeType(inputDenormalized, handlerMethodInfo.ParameterType); } else { inputTyped = JsonConvert.DeserializeObject(inputDenormalized, handlerMethodInfo.ParameterType); } } if (handlerMethodInfo.Void) { if (handlerMethodInfo.ParameterType == null) { await(Task) handlerMethodInfo.MethodInfo.Invoke(commandHandler, null); } else { await(Task) handlerMethodInfo.MethodInfo.Invoke(commandHandler, new object[] { inputTyped }); } } else { Task task; if (handlerMethodInfo.ParameterType == null) { task = (Task)handlerMethodInfo.MethodInfo.Invoke(commandHandler, null); } else { task = (Task)handlerMethodInfo.MethodInfo.Invoke(commandHandler, new object[] { inputTyped }); } await task.ConfigureAwait(false); result = task.GetType().GetProperty(nameof(Task <object> .Result)).GetValue(task); } string resultString = null; if (result == null) { resultString = Constants.NullIdentifier; } else if (result.GetType() == typeof(string) || result.GetType().IsPrimitive()) { resultString = result.ToString(); } else { resultString = JsonConvert.SerializeObject(result); } await Console.Out.WriteLineAsync($"{Constants.Delimiter}{resultString.Replace("\n", Constants.NewLineIdentifier).Replace("\r", "")}{Constants.Delimiter}"); } else { throw new MissingMethodException(commandHandler.GetType().FullName, inputSegments[1]); } } catch (Exception ex) { await Console.Out.WriteLineAsync($"{Constants.ErrorIdentifier}{ex.ToString().Replace("\n", Constants.NewLineIdentifier).Replace("\r", "")}"); } } ; } catch (Exception ex) { AdapterLogger.LogError(ex, "Terminal error."); } finally { idleTimer?.Dispose(); } }