private TestServer CreateTestServer(IEnumerable <string> origins)
        {
            HttpConfiguration      config       = new HttpConfiguration();
            MobileAppConfiguration mobileConfig = new MobileAppConfiguration();

            if (origins == null)
            {
                mobileConfig = mobileConfig.MapLegacyCrossDomainController();
            }
            else
            {
                mobileConfig = mobileConfig.MapLegacyCrossDomainController(origins);
            }

            mobileConfig.ApplyTo(config);

            return(TestServer.Create(appBuilder =>
            {
                appBuilder.UseWebApi(config);
            }));
        }
Exemple #2
0
        /// <summary>
        /// Configurations the specified application.
        /// </summary>
        /// <param name="app">The application.</param>
        public void Configuration(IAppBuilder app)
        {
            // Create HTTP configuration object
            var httpConfig =
                new HttpConfiguration
            {
                IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
            };

            httpConfig.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            httpConfig.Formatters.JsonFormatter.SerializerSettings.ContractResolver  = new CamelCasePropertyNamesContractResolver();
            httpConfig.Formatters.JsonFormatter.SerializerSettings.Formatting        = Formatting.Indented;
            JsonConvert.DefaultSettings =
                () =>
                new JsonSerializerSettings
            {
                ContractResolver  = new CamelCasePropertyNamesContractResolver(),
                Formatting        = Formatting.Indented,
                NullValueHandling = NullValueHandling.Ignore,
            };

            // Enable swagger support
            SwaggerConfig.Register(httpConfig);

            // Create autofac container (it may need to update the HTTP configuration)
            var builder = new ContainerBuilder();

            builder.RegisterModule(new IocModule(httpConfig));
            var container = builder.Build();

            // Get telemetry client and log
            var telemetryClient = container.Resolve <TelemetryClient>();

            telemetryClient.TrackEvent("Service startup");

            // Setup WEBAPI dependency resolver
            httpConfig.DependencyResolver = new AutofacWebApiDependencyResolver(container);

            // Setup mobile application app settings
            var provider = httpConfig.GetMobileAppSettingsProvider();
            var options  = provider.GetMobileAppSettings();

            // Setup mobile app configuration settings
            var mobileAppConfig = new MobileAppConfiguration()
                                  //.MapApiControllers()
                                  .AddMobileAppHomeController()
                                  .AddPushNotifications()
                                  .AddTables(
                new MobileAppTableConfiguration()
                .MapTableControllers()
                .AddEntityFramework());

            mobileAppConfig.ApplyTo(httpConfig);

            // Enable attribute routing for everything else we expose
            httpConfig.MapHttpAttributeRoutes();

            // Hook up autofac webapi controller dependency injection
            app.UseAutofacWebApi(httpConfig);
            app.UseWebApi(httpConfig);

            // Initialise database (including execution of any pending schema migrations)
            Database.SetInitializer(new MobileServiceInitializer());
        }
        public static void ConfigureMobileApp(IAppBuilder app)
        {
            //var container = new UnityContainer();
            //container.RegisterType<DbContext, EShopeMobileServiceContext>(new HierarchicalLifetimeManager());
            //container.RegisterType<IRepository<Order>, EFRepository<Order>>(new HierarchicalLifetimeManager());

            HttpConfiguration config = new HttpConfiguration();
            //config.DependencyResolver = new UnityResolver(container);
            //config.Formatters.JsonFormatter.SupportedMediaTypes
            //config.EnableSystemDiagnosticsTracing();

            //config.MapHttpAttributeRoutes();

            //config.Routes.MapHttpRoute(
            //    name: "DefaultApi",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: new { id = RouteParameter.Optional });

            //config.Routes.MapHttpRoute(
            //name: "ActionApi",
            //routeTemplate: "api/{controller}/{action}/{id}",
            //defaults: new { id = RouteParameter.Optional });

            //config.Services
            //app.Use
            // Swagger
            //SwaggerConfig.Register(config);

            var mobileAppConfiguration = new MobileAppConfiguration();

            mobileAppConfiguration.UseDefaultConfiguration();
            //.AddMobileAppHomeController()             // from the Home package
            //.MapApiControllers()
            //.AddTables(                               // from the Tables package
            //    new MobileAppTableConfiguration()
            //        .MapTableControllers()
            //        .AddEntityFramework()             // from the Entity package
            //    )
            ////.AddPushNotifications()                   // from the Notifications package
            //.MapLegacyCrossDomainController()         // from the CrossDomain package

            //.AddTablesWithEntityFramework()
            //mobileAppConfiguration.MapApiControllers();
            mobileAppConfiguration.ApplyTo(config);

            // Use Entity Framework Code First to create database tables based on your DbContext
            //Database.SetInitializer(new MobileServiceInitializer());

            //var migrator = new DbMigrator(new Migrations.Configuration());
            //migrator.Update();

            MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings();

            if (string.IsNullOrEmpty(settings.HostName))
            {
                app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions
                {
                    // This middleware is intended to be used locally for debugging. By default, HostName will
                    // only have a value when running in an App Service application.
                    SigningKey     = ConfigurationManager.AppSettings["SigningKey"],
                    ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] },
                    ValidIssuers   = new[] { ConfigurationManager.AppSettings["ValidIssuer"] },
                    TokenHandler   = config.GetAppServiceTokenHandler()
                });
            }

            app.UseWebApi(config);
            SwaggerConfig.Register(config);
        }