public Result Initialize(RunDescriptor run, EndpointBehaviour endpointBehaviour, IDictionary<Type, string> routingTable, string endpointName) { try { behaviour = endpointBehaviour; scenarioContext = run.ScenarioContext; configuration = ((IEndpointConfigurationFactory)Activator.CreateInstance(endpointBehaviour.EndpointBuilderType)).Get(); configuration.EndpointName = endpointName; if (!string.IsNullOrEmpty(configuration.CustomMachineName)) { NServiceBus.Support.RuntimeEnvironment.MachineNameAction = () => configuration.CustomMachineName; } //apply custom config settings endpointBehaviour.CustomConfig.ForEach(customAction => customAction(config)); config = configuration.GetConfiguration(run, routingTable); if (scenarioContext != null) { config.Configurer.RegisterSingleton(scenarioContext.GetType(), scenarioContext); scenarioContext.ContextPropertyChanged += scenarioContext_ContextPropertyChanged; } bus = config.CreateBus(); Configure.Instance.ForInstallationOn<Windows>().Install(); Task.Factory.StartNew(() => { while (!stopped) { contextChanged.WaitOne(TimeSpan.FromSeconds(5)); //we spin around each 5 s since the callback mechanism seems to be shaky lock (behaviour) { foreach (var when in behaviour.Whens) { if (executedWhens.Contains(when.Id)) continue; if(when.ExecuteAction(scenarioContext, bus)) executedWhens.Add(when.Id); } } } }); return Result.Success(); } catch (Exception ex) { Logger.Error("Failed to initalize endpoint " + endpointName, ex); return Result.Failure(ex); } }
static string GetEndpointNameForRun(RunDescriptor runDescriptor, EndpointBehaviour endpointBehaviour) { var endpointName = Conventions.EndpointNamingConvention(endpointBehaviour.EndpointBuilderType) + "." + runDescriptor.Key; return endpointName; }
static ActiveRunner PrepareRunner(string endpointName, EndpointBehaviour endpointBehaviour) { var domainSetup = new AppDomainSetup { ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase, LoaderOptimization = LoaderOptimization.SingleDomain }; var endpoint = ((IEndpointConfigurationFactory) Activator.CreateInstance(endpointBehaviour.EndpointBuilderType)).Get(); if (endpoint.AppConfigPath != null) domainSetup.ConfigurationFile = endpoint.AppConfigPath; var appDomain = AppDomain.CreateDomain(endpointName, AppDomain.CurrentDomain.Evidence, domainSetup); return new ActiveRunner { Instance = (EndpointRunner)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(EndpointRunner).FullName), AppDomain = appDomain, EndpointName = endpointName }; }