public TrendyolDataContextBuilder <T> WithAutomaticMigrations <TConfig>() where TConfig : DbMigrationsConfiguration
        {
            _appBuilder.AfterBuild(() =>
            {
                TConfig configuration = Activator.CreateInstance <TConfig>();
                DbMigrator migrator   = new DbMigrator(configuration);
                migrator.Update();
            });

            return(this);
        }
Ejemplo n.º 2
0
        public static TrendyolAppBuilder UseAutomaticMigrations <T>(this TrendyolAppBuilder builder) where T : DbMigrationsConfiguration
        {
            builder.AfterBuild(() =>
            {
                T configuration     = Activator.CreateInstance <T>();
                DbMigrator migrator = new DbMigrator(configuration);
                migrator.Update();
            });

            return(builder);
        }
        public static TrendyolWebApiBuilder UseWebApi(this TrendyolAppBuilder builder, IAppBuilder app, string applicationName, string customSwaggerContentPath = null)
        {
            Assembly callingAssembly = Assembly.GetCallingAssembly();

            builder.BeforeBuild(() =>
            {
                HttpConfiguration config = new HttpConfiguration();

                string rootUrl = builder.DataStore.GetData <string>(Constants.ApiRootUrlDataKey);

                config.EnableSwagger("docs/{apiVersion}/swagger", c =>
                {
                    c.SingleApiVersion("v1", applicationName)
                    .Description($"{applicationName} documentation.");
                    c.DescribeAllEnumsAsStrings(true);

                    if (!String.IsNullOrEmpty(rootUrl))
                    {
                        c.RootUrl(r => rootUrl);
                    }
                })
                .EnableSwaggerUi("help/{*assetPath}", c =>
                {
                    if (!String.IsNullOrEmpty(customSwaggerContentPath))
                    {
                        c.InjectJavaScript(callingAssembly, $"{callingAssembly.GetName().Name}.{customSwaggerContentPath}");
                    }

                    c.DisableValidator();
                });

                config.MapHttpAttributeRoutes();
                config.Routes.MapHttpRoute("Default", "{controller}/{id}", new { id = RouteParameter.Optional });

                config.Formatters.Clear();
                config.Formatters.Add(CreateJsonFormatter());
                config.MessageHandlers.Insert(0, new ServerCompressionHandler(new GZipCompressor(), new DeflateCompressor()));
                config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());

                builder.DataStore.SetData(Constants.HttpConfigurationDataKey, config);
            });

            builder.AfterBuild(() =>
            {
                HttpConfiguration config = builder.DataStore.GetData <HttpConfiguration>(Constants.HttpConfigurationDataKey);
                app.UseWebApi(config);
            });

            return(new TrendyolWebApiBuilder(builder, app, applicationName));
        }
Ejemplo n.º 4
0
        public static TrendyolAppBuilder UseAutofacWebApi(this TrendyolAppBuilder builder, Assembly controllerAssembly)
        {
            builder.BeforeBuild(() =>
            {
                ContainerBuilder containerBuilder = builder.DataStore.GetData <ContainerBuilder>(Autofac.Constants.AutofacContainerBuilderDataKey);
                containerBuilder.RegisterApiControllers(controllerAssembly);
            });

            builder.AfterBuild(() =>
            {
                HttpConfiguration config  = builder.DataStore.GetData <HttpConfiguration>(WebApi.Constants.HttpConfigurationDataKey);
                IContainer container      = builder.DataStore.GetData <IContainer>(Autofac.Constants.AutofacContainerDataKey);
                config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
            });

            return(builder);
        }
        public static TrendyolAppBuilder UseAutofac(this TrendyolAppBuilder builder, Action <ContainerBuilder> action = null, Assembly serviceAssembly = null, Assembly dataAssembly = null)
        {
            builder.BeforeBuild(() =>
            {
                ContainerBuilder containerBuilder = new ContainerBuilder();

                if (serviceAssembly != null)
                {
                    containerBuilder
                    .RegisterAssemblyTypes(serviceAssembly)
                    .Where(item => item.Implements(typeof(IService)) && item.IsAbstract == false)
                    .AsImplementedInterfaces()
                    .SingleInstance();
                }

                if (dataAssembly != null)
                {
                    containerBuilder
                    .RegisterAssemblyTypes(dataAssembly)
                    .Where(item => item.Implements(typeof(IRepository)) && item.IsAbstract == false)
                    .AsImplementedInterfaces()
                    .SingleInstance();

                    containerBuilder
                    .RegisterAssemblyTypes(dataAssembly)
                    .Where(item => item.Implements(typeof(IIdentityProvider)) && item.IsAbstract == false)
                    .AsImplementedInterfaces()
                    .SingleInstance();
                }

                action?.Invoke(containerBuilder);

                builder.DataStore.SetData(Constants.AutofacContainerBuilderDataKey, containerBuilder);
            });

            builder.AfterBuild(() =>
            {
                ContainerBuilder containerBuilder = builder.DataStore.GetData <ContainerBuilder>(Constants.AutofacContainerBuilderDataKey);
                IContainer container = containerBuilder.Build();
                builder.DataStore.SetData(Constants.AutofacContainerDataKey, container);
            });

            return(builder);
        }
        public static TrendyolAppBuilder UseAutofacMvc(this TrendyolAppBuilder builder, Assembly controllerAssembly)
        {
            builder.BeforeBuild(() =>
            {
                ContainerBuilder containerBuilder = builder.DataStore.GetData <ContainerBuilder>(Autofac.Constants.AutofacContainerBuilderDataKey);

                containerBuilder
                .RegisterAssemblyTypes(controllerAssembly)
                .Where(item => item.Implements(typeof(IControllerHandler)) && item.IsAbstract == false)
                .AsImplementedInterfaces()
                .InstancePerLifetimeScope();

                containerBuilder.RegisterControllers(controllerAssembly);
            });

            builder.AfterBuild(() =>
            {
                IContainer container = builder.DataStore.GetData <IContainer>(Autofac.Constants.AutofacContainerDataKey);
                DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
            });

            return(builder);
        }
        public static TrendyolAppBuilder UseDaemon <T>(this TrendyolAppBuilder appBuilder,
                                                       string serviceName,
                                                       int defaultStartStopTimeoutInMinutes  = 3,
                                                       ServiceAccountType serviceAccountType = ServiceAccountType.LocalSystem,
                                                       string serviceAccountUserName         = "",
                                                       string serviceAccountPassword         = "",
                                                       params string[] dependencies) where T : TrendyolWindowsService
        {
            appBuilder.AfterBuild(() =>
            {
                HostFactory.Run(x =>
                {
                    x.Service <T>(s =>
                    {
                        s.ConstructUsing(srv => Activator.CreateInstance <T>());
                        s.WhenStarted(srv => srv.Start());
                        s.WhenStopped(srv => srv.Stop());
                    });

                    x.OnException((ex) =>
                    {
                        Logger.Error($"There was a problem executing {serviceName}.", ex);
                    });

                    x.StartAutomatically();
                    x.SetStartTimeout(TimeSpan.FromMinutes(defaultStartStopTimeoutInMinutes));
                    x.SetStopTimeout(TimeSpan.FromMinutes(defaultStartStopTimeoutInMinutes));
                    x.SetDescription(serviceName);
                    x.SetDisplayName(serviceName);
                    x.SetServiceName(serviceName);

                    if (dependencies != null && dependencies.Any())
                    {
                        foreach (string dependency in dependencies)
                        {
                            x.AddDependency(dependency);
                        }
                    }

                    switch (serviceAccountType)
                    {
                    case ServiceAccountType.LocalSystem:
                        x.RunAsLocalSystem();
                        break;

                    case ServiceAccountType.Custom:
                        if (String.IsNullOrEmpty(serviceAccountUserName))
                        {
                            throw new ArgumentException("ServiceAccountUserName cannot be null when account type is set to custom.", nameof(serviceAccountUserName));
                        }

                        if (String.IsNullOrEmpty(serviceAccountPassword))
                        {
                            throw new ArgumentException("ServiceAccountPassword cannot be null when account type is set to custom.", nameof(serviceAccountPassword));
                        }

                        x.RunAs(serviceAccountUserName, serviceAccountPassword);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(serviceAccountType), serviceAccountType, null);
                    }
                });
            });

            return(appBuilder);
        }