Beispiel #1
0
        static Container CreateContainer(IDetailedExceptionLogger detailedLogger)
        {
            Container container = null;

            try {
                container = new Container();

                // ShipHub Configuration
                var config = ShipHubCloudConfiguration.Instance;
                container.RegisterSingleton(config);

                // AutoMapper
                container.RegisterSingleton(() => {
                    var mapperConfig = new MapperConfiguration(cfg => {
                        cfg.AddProfile <GitHubToDataModelProfile>();
                    });
                    return(mapperConfig.CreateMapper());
                });

                // Service Bus
                container.RegisterSingleton <IServiceBusFactory>(() => {
                    // HACK: This is gross
                    var sbf = new ServiceBusFactory();
                    sbf.Initialize().GetAwaiter().GetResult();
                    return(sbf);
                });

                // Orleans
                container.RegisterSingleton <IAsyncGrainFactory>(() => {
                    var factory = new OrleansAzureClient(config.DeploymentId, config.DataConnectionString);
                    factory.Configuration.DefaultTraceLevel = Orleans.Runtime.Severity.Error;
                    return(factory);
                });

                // Queue Client
                container.RegisterSingleton <IShipHubQueueClient, ShipHubQueueClient>();

                // IDetailedExceptionLogger
                container.RegisterSingleton(() => detailedLogger);

                // ChargeBee
                var chargeBeeHostAndApiKey = ShipHubCloudConfiguration.Instance.ChargeBeeHostAndKey;
                if (!chargeBeeHostAndApiKey.IsNullOrWhiteSpace())
                {
                    var parts = chargeBeeHostAndApiKey.Split(':');
                    container.RegisterSingleton(() => new cb.ChargeBeeApi(parts[0], parts[1]));
                }

                container.Verify();
            } catch {
                if (container != null)
                {
                    container.Dispose();
                    throw;
                }
            }

            return(container);
        }
        public static void Register(IShipHubConfiguration config)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();

            // ShipHubConfiguration
            container.RegisterSingleton(config);

            // AutoMapper
            container.RegisterSingleton(() => new MapperConfiguration(cfg => {
                cfg.AddProfile <GitHubToDataModelProfile>();
                cfg.AddProfile <DataModelToApiModelProfile>();
            }).CreateMapper());

            // Service Bus
            container.RegisterSingleton <IServiceBusFactory>(() => {
                // HACK: This is gross
                var sbf = new ServiceBusFactory();
                sbf.Initialize().GetAwaiter().GetResult();
                return(sbf);
            });

            // Orleans
            container.RegisterSingleton <IAsyncGrainFactory>(new OrleansAzureClient(config.DeploymentId, config.DataConnectionString));

            // Queue Client
            container.RegisterSingleton <IShipHubQueueClient, ShipHubQueueClient>();

            // Sync Manager
            container.RegisterSingleton <ISyncManager, SyncManager>();

            // Mailer
            container.RegisterSingleton <IShipHubMailer, ShipHubMailer>();

            // ChargeBee
            if (!config.ChargeBeeHostAndKey.IsNullOrWhiteSpace())
            {
                var parts = config.ChargeBeeHostAndKey.Split(':');
                container.RegisterSingleton(() => new ChargeBeeApi(parts[0], parts[1]));
            }

            // Mixpanel (maybe not safe as a singleton?)
            container.Register <IMixpanelClient>(() => new MixpanelClient(config.MixpanelToken, new MixpanelConfig()
            {
                ErrorLogFn = (message, exception) => {
                    Log.Exception(exception, message);
                },
                IpAddressHandling = MixpanelIpAddressHandling.IgnoreRequestIp,
            }));

            // This is an extension method from the integration package.
            container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

            container.Verify();

            GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);
        }
Beispiel #3
0
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            Log.Trace();

            // Just use the Microsoft container.

            var config = ShipHubCloudConfiguration.Instance;

            // Configuration
            services.AddSingleton(config);

            // Transient configuration
            services.AddSingleton <IShipHubRuntimeConfiguration, ShipHubRuntimeConfiguration>();

            services.AddSingleton <IFactory <ShipHubContext> >(
                new GenericFactory <ShipHubContext>(() => new ShipHubContext(config.ShipHubContext)));

            // AutoMapper
            services.AddSingleton(
                new MapperConfiguration(cfg => {
                cfg.AddProfile <GitHubToDataModelProfile>();
            }).CreateMapper());

            // Service Bus
            Log.Info($"Creating {nameof(ServiceBusFactory)}");
            // HACK: This is gross
            var sbf = new ServiceBusFactory();

            sbf.Initialize().GetAwaiter().GetResult();
            services.AddSingleton <IServiceBusFactory>(sbf);
            Log.Info($"Created {nameof(ServiceBusFactory)}");

            // Queue Client
            services.AddSingleton <IShipHubQueueClient, ShipHubQueueClient>();

            // TimeLoggerFilter Interceptor
            services.AddSingleton <IGrainCallFilter, TimeLoggerFilter>();

            return(services.BuildServiceProvider());
        }