/// <summary>Call this method to set-up AspNetDependencyInjection for ASP.NET MVC.</summary>
        /// <param name="builder">Cannot be null.</param>
        /// <remarks>Internally, this method adds <see cref="DependencyInjectionMvcDependencyResolver"/> to <paramref name="builder"/> using <see cref="ApplicationDependencyInjectionBuilder.AddClient(Func{ApplicationDependencyInjection, IDependencyInjectionClient}[])"/>.</remarks>
        public static ApplicationDependencyInjectionBuilder AddMvcDependencyResolver(this ApplicationDependencyInjectionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder
                   .AddClient(di => new DependencyInjectionMvcDependencyResolver(di)));
        }
コード例 #2
0
        /// <summary>Use this method to add AspNetDependencyInjection support to SignalR. It only supports Singleton and Transient service lifetimes and does not support scoped lifetimes. It uses a custom <see cref="Microsoft.AspNet.SignalR.Hubs.IHubActivator"/> that uses the root <see cref="IServiceProvider"/>. This approach is much simpler than the approach used by <see cref="AddScopedSignalRDependencyResolver(ApplicationDependencyInjectionBuilder)"/> so you may choose to use this method if you experience problems with scoped lifetimes.</summary>
        public static ApplicationDependencyInjectionBuilder AddUnscopedSignalRDependencyResolver(this ApplicationDependencyInjectionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            // TODO: I want to add a way to add validation so there'll be an exception if both AddScoped and AddUnscoped are used at the same time.

            return(builder
                   .AddClient((di, rootSP) => new UnscopedAndiSignalRDependencyResolver(di, rootSP)));
        }
コード例 #3
0
        /// <summary>Use this method to set-up AspNetDependencyInjection for a self-hosted ASP.NET Web API application, where your application is responsible for instantiating and configuring a <see cref="HttpConfiguration"/> instance.</summary>
        /// <param name="builder">Cannot be <see langword="null"/>.</param>
        /// <param name="httpConfiguration">Cannot be null.</param>
        /// <remarks>Internally, this method adds <see cref="DependencyInjectionWebApiDependencyResolver"/> to <paramref name="builder"/> using <see cref="ApplicationDependencyInjectionBuilder.AddClient(Func{ApplicationDependencyInjection, IDependencyInjectionClient}[])"/>.</remarks>
        public static ApplicationDependencyInjectionBuilder AddWebApiDependencyResolver(this ApplicationDependencyInjectionBuilder builder, HttpConfiguration httpConfiguration)
        {
            if (builder           is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }
            if (httpConfiguration is null)
            {
                throw new ArgumentNullException(nameof(httpConfiguration));
            }

            return(builder
                   .AddClient((di, rootSP) => new DependencyInjectionWebApiDependencyResolver(di, rootSP, httpConfiguration)));
        }
コード例 #4
0
        /// <summary>Use this method to add AspNetDependencyInjection support to SignalR. It uses a custom <see cref="Microsoft.AspNet.SignalR.Hubs.HubDispatcher"/> to manage scope lifetime of <see cref="Microsoft.AspNet.SignalR.Hubs.IHub"/> objects. You should prefer this method instead of <see cref="AddUnscopedSignalRDependencyResolver(ApplicationDependencyInjectionBuilder)"/> unless you're certain you don't want scoped lifetimes in SignalR or if you experience issues with scoped lifetimes in SignalR.</summary>
        public static ApplicationDependencyInjectionBuilder AddScopedSignalRDependencyResolver(this ApplicationDependencyInjectionBuilder builder)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            return(builder
                   .AddClient((di, rootSP) => new ScopedAndiSignalRDependencyResolver(di, rootSP)));
        }
コード例 #5
0
        /// <summary>Use this method overload to set-up AspNetDependencyInjection for ASP.NET-hosted Web API application. The current <see cref="HttpConfiguration"/> will be obtained from <c>System.Web.Http.GlobalConfiguration</c> using reflection.</summary>
        /// <param name="builder">Cannot be <see langword="null"/>.</param>
        /// <remarks>
        /// Reflection is used to access <c>System.Web.Http.GlobalConfiguration</c> because the type exists in <c>System.Web.Http.WebHost.dll</c> which is not available in non-ASP.NET-hosted Web API projects, and I didn't want to add another NuGet dependency to <c>AspNetDependencyInjection.WebApi</c> just for that one static property.<br />
        /// Internally, this method uses <see cref="TryGetGlobalHttpConfiguration"/> to get <see cref="HttpConfiguration"/> from  <c>System.Web.Http.GlobalConfiguration</c> - if it fails then it throws <see cref="InvalidOperationException"/>, and adds <see cref="DependencyInjectionWebApiDependencyResolver"/> to <paramref name="builder"/> using <see cref="ApplicationDependencyInjectionBuilder.AddClient(Func{ApplicationDependencyInjection, IDependencyInjectionClient}[])"/>.</remarks>
        /// <exception cref="InvalidOperationException">Thrown when <see cref="TryGetGlobalHttpConfiguration(out HttpConfiguration, out String)"/> fails.</exception>
        public static ApplicationDependencyInjectionBuilder AddWebApiDependencyResolver(this ApplicationDependencyInjectionBuilder builder)
        {
            if (builder is null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (TryGetGlobalHttpConfiguration(out HttpConfiguration globalConfiguration, out String errorMessage))
            {
                return(builder
                       .AddClient((di, rootSP) => new DependencyInjectionWebApiDependencyResolver(di, rootSP, globalConfiguration)));
            }
            else
            {
                throw new InvalidOperationException(errorMessage);
            }
        }