Exemple #1
0
 public void DependencyResolver_GetService_should_return_null_for_registered_interface_types()
 {
     using (ApplicationDependencyInjection di = CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver resolver))
     {
         ITestTransient service = resolver.GetService <ITestTransient>();
         service.ShouldBeNull();
     }
 }
Exemple #2
0
 public void DependencyResolver_GetService_should_return_null_for_registered_abstract_types()
 {
     using (ApplicationDependencyInjection di = CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver resolver))
     {
         TestAbstractClass service = resolver.GetService <TestAbstractClass>();
         service.ShouldBeNull();
     }
 }
        internal static void PreStart()
        {
            var webApiConfig = GlobalConfiguration.Configuration;

            _di = new ApplicationDependencyInjectionBuilder()
                  .ConfigureServices(ConfigureServices)
                  .AddWebApiDependencyResolver(webApiConfig)
                  .Build();
        }
        internal DependencyInjectionMvcDependencyResolver(ApplicationDependencyInjection di)
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));

            this.originalIdr         = DependencyResolver.Current;     // Will never be null.
            this.controllerActivator = new DependencyInjectionMvcControllerActivator(di);

            DependencyResolver.SetResolver(this);
        }
Exemple #5
0
 public void Mvc_DependencyResolver_GetService_should_resolve_SystemObject()
 {
     using (ApplicationDependencyInjection di = CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver resolver))
     {
         Object service = resolver.GetService <Object>();
         _ = service.ShouldNotBeNull();
         _ = service.ShouldBeOfType <Object>();
         service.GetType().ShouldBe(typeof(System.Object));
     }
 }
Exemple #6
0
        private static ApplicationDependencyInjection _di;         // This is disposed in `ApplicationShutdown()`.

        /// <summary>Invoked when the ASP.NET application starts up, before Global's Application_Start method runs. Dependency-injection should be configured here.</summary>
        internal static void PreStart()
        {
            System.Diagnostics.Debug.WriteLine(nameof(SampleApplicationStart) + "." + nameof(PreStart) + "() called.");

            // If you are using ASP.NET MVC, regardless of whether you're using ASP.NET Web Forms or the ASPX View Engine, use `.AddMvcDependencyResolver()` (you will need to reference the `AspNetDependencyInjection.Mvc` package/assembly first).

            _di = new ApplicationDependencyInjectionBuilder()
                  .ConfigureServices(ConfigureServices)
                  .Build();
        }
        internal DependencyInjectionWebApiDependencyResolver(ApplicationDependencyInjection di, IServiceProvider rootServiceProvider, HttpConfiguration httpConfiguration)
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));
            this.rootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));
            this.httpConfiguration   = httpConfiguration ?? throw new ArgumentNullException(nameof(httpConfiguration));

            this.previousResolver = httpConfiguration.DependencyResolver;

            httpConfiguration.DependencyResolver = this;
        }
        /// <summary>Invoked when the ASP.NET application starts up, before Global's Application_Start method runs. Dependency-injection should be configured here.</summary>
        internal static void PreStart()
        {
            System.Diagnostics.Debug.WriteLine(nameof(SampleApplicationStart) + "." + nameof(PreStart) + "() called.");

            // If you are using ASP.NET Web Forms without any ASP.NET MVC functionality, remove the call to `.AddMvcDependencyResolver()`.
            // If you are using ASP.NET MVC, regardless of whether you're using ASP.NET Web Forms, use `.AddMvcDependencyResolver()`:

            _di = new ApplicationDependencyInjectionBuilder()
                  .ConfigureServices(ConfigureServices)
                  .Build();
        }
Exemple #9
0
        public void Mvc_DependencyResolver_GetService_should_return_separate_instances()
        {
            using (ApplicationDependencyInjection di = CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver resolver))
            {
                Object object1 = resolver.GetService <Object>();
                Object object2 = resolver.GetService <Object>();

                _ = object1.ShouldNotBeNull();
                _ = object2.ShouldNotBeNull();

                Object.ReferenceEquals(object1, object2).ShouldBeFalse();
            }
        }
Exemple #10
0
        public UnscopedAndiSignalRDependencyResolver(ApplicationDependencyInjection di, IServiceProvider rootServiceProvider)
            : base()
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));
            this.RootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));

            //

            this.HubActivator = new UnscopedAndiSignalRHubActivator(this);

            this.Register(typeof(IHubActivator), () => this.HubActivator);

            GlobalHost.DependencyResolver = this;
        }
Exemple #11
0
        /// <summary>Constructor. Consuming applications should not create their own instances of <see cref="ScopedAndiSignalRDependencyResolver"/>.</summary>
        public ScopedAndiSignalRDependencyResolver(ApplicationDependencyInjection di, IServiceProvider rootServiceProvider)
            : base()             // <-- this results in virtual calls from DefaultDependencyResolver's constructor to `Register` - eww.
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));
            this.rootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));

            //

            this.HubActivator = new ScopedAndiSignalRHubActivator(this);

            // IHubActivator is singleton, so this is okay:
            this.Register(typeof(IHubActivator), () => this.HubActivator);

            // HubDispatcher subclasses are transient and are resolved using the `typeof(TConnection)` passed to `appBuilder.MapSignalR<TConnection>(...)`
            this.Register(typeof(ScopedAndiSignalRHubDispatcher), this.CreateHubDispatcher);

            // Overwrite `GlobalHost.DependencyResolver` immediately so consumers can reference it in OWinStartup:
            GlobalHost.DependencyResolver = this;
        }
Exemple #12
0
        public void Mvc_DependencyResolver_GetService_should_return_null_for_open_generic_types()
        {
            // If `` doesn't check for open generic types then this error happens:

            /* Test method AspNetDependencyInjection.Tests.Mvc.MvcDependencyResolverTests.DependencyResolver_GetService_should_return_null_for_open_generic_types threw exception:
             * System.ArgumentException:
             *      Cannot create an instance of AspNetDependencyInjection.Tests.Mvc.TestestGeneric`1[T] because Type.ContainsGenericParameters is true.
             *
             * RuntimeType.CreateInstanceCheckThis()
             * RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
             * RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
             * Activator.CreateInstance(Type type, Boolean nonPublic)
             * Activator.CreateInstance(Type type)
             */

            using (ApplicationDependencyInjection di = CreateMvcDIWithTestServiceRegistrations(out IDependencyResolver resolver))
            {
                Object service = resolver.GetService(typeof(ITestGeneric <>));
                service.ShouldBeNull();
            }
        }
        internal DependencyInjectionMvcDependencyResolver(ApplicationDependencyInjection di)
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));

            this.originalIdr = DependencyResolver.Current;             // Will never be null.
        }
Exemple #14
0
 internal DependencyInjectionMvcControllerActivator(ApplicationDependencyInjection di)
 {
     this.di = di ?? throw new ArgumentNullException(nameof(di));
 }
        /// <summary>Instantiates a new instance of <see cref="DependencyInjectionWebObjectActivator"/>. You do not need to normally use this constructor directly - instead use <see cref="ApplicationDependencyInjection"/>.</summary>
        /// <param name="di">Required.</param>
        public DependencyInjectionWebObjectActivator(ApplicationDependencyInjection di)
        {
            this.di = di ?? throw new ArgumentNullException(nameof(di));

            HttpRuntime.WebObjectActivator = this;
        }
 public ScopedAndiSignalRHubDispatcher(ApplicationDependencyInjection di, IServiceProvider rootServiceProvider, HubConfiguration configuration)
     : base(configuration)
 {
     this.di = di ?? throw new ArgumentNullException(nameof(di));
     this.rootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));
 }
Exemple #17
0
 /// <summary>Constructor. All parameters are required.</summary>
 public DependencyInjectionWebApiScope(ApplicationDependencyInjection di, IServiceScope scope)
 {
     this.di    = di ?? throw new ArgumentNullException(nameof(di));
     this.scope = scope ?? throw new ArgumentNullException(nameof(scope));
 }
Exemple #18
0
 internal DependencyInjectionWebApiDependencyResolver(ApplicationDependencyInjection di, IServiceProvider rootServiceProvider)
 {
     this.di = di ?? throw new ArgumentNullException(nameof(di));
     this.rootServiceProvider = rootServiceProvider ?? throw new ArgumentNullException(nameof(rootServiceProvider));
 }
Exemple #19
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();

            services.AddControllers();

            var tokenConfigurations = new TokenConfigurations();

            new ConfigureFromConfigurationOptions <TokenConfigurations>(Configuration.GetSection("TokenConfigurations")).Configure(tokenConfigurations);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(tokenConfigurations.Secret)),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });

            services.AddRouting(options => options.LowercaseUrls = true);

            services.AddOptions();

            ApplicationDependencyInjection.RegisterServices(services, Configuration);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1",
                             new OpenApiInfo
                {
                    Title       = "API",
                    Version     = "v1",
                    Description = "TooSeguros",
                    Contact     = new OpenApiContact()
                    {
                        Name = "Fernando Camilo",
                        Url  = new Uri("https://www.linkedin.com/in/fernando-azevedo-camilo-51795b63/")
                    }
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

                c.IncludeXmlComments(xmlPath);

                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "Standard Authorization header using the Bearer scheme. Example: \"bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });

                c.OperationFilter <SecurityRequirementsOperationFilter>();
            });
        }
        public static IReadOnlyList <IDependencyInjectionClient> GetClients(this ApplicationDependencyInjection di)
        {
            IHasDependencyInjectionClients asInterface = (IHasDependencyInjectionClients)di;

            return(asInterface.Clients);
        }