public Type Resolve(string typeName) { var types = ReferencesUtil .GetAllLoadedTypes() .Where(t => t.Name.Equals(typeName, StringComparison.OrdinalIgnoreCase)); if (types.Count() == 1) { return(types.First()); } else if (types.Count() == 0) { return(Type.GetType(typeName, true, true)); } else { throw new Exception($"There are multiple types named {typeName}:\n{string.Join("\n", types.Select(t => t.AssemblyQualifiedName))}"); } }
private static TextcMessageReceiverBuilder SetupCommands(IServiceProvider serviceProvider, IDictionary <string, object> settings, TextcMessageReceiverCommandSettings[] commandSettings, TextcMessageReceiverBuilder builder) { foreach (var commandSetting in commandSettings) { var syntaxes = commandSetting.Syntaxes.Select(CsdlParser.Parse).ToArray(); if (syntaxes.Length > 0) { var syntaxBuilder = builder.ForSyntaxes(syntaxes); if (!string.IsNullOrEmpty(commandSetting.ReturnText)) { var returnVariables = new List <string>(); var returnTextVariableMatches = ReturnVariablesRegex.Matches(commandSetting.ReturnText); if (returnTextVariableMatches.Count > 0) { returnVariables.AddRange(returnTextVariableMatches.Cast <Match>().Select(m => m.Value)); } builder = syntaxBuilder.Return((IRequestContext context) => { var returnText = commandSetting.ReturnText; foreach (var returnVariable in returnVariables) { var returnVariableValue = context .GetVariable(returnVariable.TrimStart('{').TrimEnd('}'))?.ToString() ?? ""; returnText = returnText.Replace(returnVariable, returnVariableValue); } return(returnText.AsCompletedTask()); }); } else if (commandSetting.ReturnJson != null) { var mediaType = MediaType.Parse(commandSetting.ReturnJsonMediaType ?? "application/json"); var document = new JsonDocument(commandSetting.ReturnJson, mediaType); builder = syntaxBuilder.Return(() => document.AsCompletedTask()); } else if (!string.IsNullOrEmpty(commandSetting.ProcessorType) && !string.IsNullOrEmpty(commandSetting.Method)) { builder = syntaxBuilder .ProcessWith(o => { var processorTypeName = commandSetting.ProcessorType; var methodName = commandSetting.Method; var assembly = typeof(TextcMessageReceiverBuilder).Assembly; var path = new FileInfo(assembly.Location).DirectoryName; ReferencesUtil.LoadAssembliesAndReferences(path, assemblyFilter: ReferencesUtil.IgnoreSystemAndMicrosoftAssembliesFilter, ignoreExceptionLoadingReferencedAssembly: true); var processorType = TypeResolver.Instance.Resolve(processorTypeName); object processor; if (!ProcessorInstancesDictionary.TryGetValue(processorType, out processor)) { processor = Bootstrapper.CreateAsync <object>(processorType, serviceProvider, settings) .Result; ProcessorInstancesDictionary.Add(processorType, processor); } var method = processorType.GetMethod(methodName); if (method == null || method.ReturnType != typeof(Task)) { return(new ReflectionCommandProcessor(processor, methodName, true, o, syntaxes)); } return(new ReflectionCommandProcessor(processor, methodName, true, syntaxes: syntaxes)); }); } } } return(builder); }
/// <summary> /// Creates ans starts an application with the given settings. /// </summary> /// <param name="application">The application instance. If not defined, the class will look for an application.json file in the current directory.</param> /// <param name="loadAssembliesFromWorkingDirectory">if set to <c>true</c> indicates to the bootstrapper to load all assemblies from the current working directory.</param> /// <param name="path">Assembly path to load</param> /// <param name="builder">The builder instance to be used.</param> /// <param name="typeResolver">The type provider.</param> /// <returns></returns> /// <exception cref="System.IO.FileNotFoundException">Could not find the '{DefaultApplicationFileName}'</exception> /// <exception cref="System.ArgumentException">At least an access key or password must be defined</exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="ArgumentException">At least an access key or password must be defined</exception> /// <exception cref="FileNotFoundException"></exception> /// <exception cref="ArgumentException">At least an access key or password must be defined</exception> public static async Task <IStoppable> StartAsync( Application application = null, bool loadAssembliesFromWorkingDirectory = true, string path = ".", MessagingHubClientBuilder builder = null, ITypeResolver typeResolver = null) { if (application == null) { if (!File.Exists(DefaultApplicationFileName)) { throw new FileNotFoundException($"Could not find the '{DefaultApplicationFileName}' file", DefaultApplicationFileName); } application = Application.ParseFromJsonFile(DefaultApplicationFileName); } if (loadAssembliesFromWorkingDirectory) { ReferencesUtil.LoadAssembliesAndReferences(path, assemblyFilter: ReferencesUtil.IgnoreSystemAndMicrosoftAssembliesFilter, ignoreExceptionLoadingReferencedAssembly: true); } if (builder == null) { builder = new MessagingHubClientBuilder(); } if (application.Identifier != null) { if (application.Password != null) { builder = builder.UsingPassword(application.Identifier, application.Password); } else if (application.AccessKey != null) { builder = builder.UsingAccessKey(application.Identifier, application.AccessKey); } else { throw new ArgumentException("At least an access key or password must be defined", nameof(application)); } } else { builder = builder.UsingGuest(); } if (application.Instance != null) { builder = builder.UsingInstance(application.Instance); } if (application.RoutingRule != null) { builder = builder.UsingRoutingRule(application.RoutingRule.Value); } if (application.Domain != null) { builder = builder.UsingDomain(application.Domain); } if (application.Scheme != null) { builder = builder.UsingScheme(application.Scheme); } if (application.HostName != null) { builder = builder.UsingHostName(application.HostName); } if (application.Port != null) { builder = builder.UsingPort(application.Port.Value); } if (application.SendTimeout != 0) { builder = builder.WithSendTimeout(TimeSpan.FromMilliseconds(application.SendTimeout)); } if (application.SessionEncryption.HasValue) { builder = builder.UsingEncryption(application.SessionEncryption.Value); } if (application.SessionCompression.HasValue) { builder = builder.UsingCompression(application.SessionCompression.Value); } if (application.Throughput != 0) { builder = builder.WithThroughput(application.Throughput); } if (application.ChannelBuffer != 0) { builder = builder.WithChannelBuffer(application.ChannelBuffer); } if (application.DisableNotify) { builder = builder.WithAutoNotify(false); } if (application.ChannelCount.HasValue) { builder = builder.WithChannelCount(application.ChannelCount.Value); } if (application.ReceiptEvents != null && application.ReceiptEvents.Any()) { builder = builder.WithReceiptEvents(application.ReceiptEvents); } else if (application.ReceiptEvents != null) { builder = builder.WithReceiptEvents(new[] { Event.Failed }); } if (typeResolver == null) { typeResolver = TypeResolver.Instance; } var localServiceProvider = BuildServiceProvider(application, typeResolver); localServiceProvider.RegisterService(typeof(MessagingHubClientBuilder), builder); var client = await BuildMessagingHubClientAsync(application, builder.Build, localServiceProvider, typeResolver); await client.StartAsync().ConfigureAwait(false); var stoppables = new IStoppable[2]; stoppables[0] = client; var startable = await BuildStartupAsync(application, localServiceProvider, typeResolver); if (startable != null) { stoppables[1] = startable as IStoppable; } return(new StoppableWrapper(stoppables)); }