// Unity.WebApi.UnityDependencyScope using System; using System.Collections.Generic; using System.Web.Http.Controllers; using System.Web.Http.Dependencies; using Unity; using Unity.Resolution;
using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Web.Http.Dependencies;
///<summary> /// Autofac implementation of the <see cref="T:System.Web.Http.Dependencies.IDependencyScope" /> interface. ///</summary> publicclassAutofacWebApiDependencyScope : IDependencyScope, IDisposable { privatebool _disposed;
privatereadonly ILifetimeScope _lifetimeScope;
///<summary> /// Gets the lifetime scope for the current dependency scope. ///</summary> public ILifetimeScope LifetimeScope => _lifetimeScope;
///<summary> /// Initializes a new instance of the <see cref="T:Autofac.Integration.WebApi.AutofacWebApiDependencyScope" /> class. ///</summary> ///<param name="lifetimeScope">The lifetime scope to resolve services from.</param> publicAutofacWebApiDependencyScope(ILifetimeScope lifetimeScope) { if (lifetimeScope == null) { thrownew ArgumentNullException("lifetimeScope"); } _lifetimeScope = lifetimeScope; }
///<summary> /// Finalizes an instance of the <see cref="T:Autofac.Integration.WebApi.AutofacWebApiDependencyScope" /> class. ///</summary> ~AutofacWebApiDependencyScope() { Dispose(disposing: false); }
///<summary> /// Try to get a service of the given type. ///</summary> ///<param name="serviceType">ControllerType of service to request.</param> ///<returns>An instance of the service, or null if the service is not found.</returns> publicobjectGetService(Type serviceType) { return ResolutionExtensions.ResolveOptional((IComponentContext)(object)_lifetimeScope, serviceType); }
///<summary> /// Try to get a list of services of the given type. ///</summary> ///<param name="serviceType">ControllerType of services to request.</param> ///<returns>An enumeration (possibly empty) of the service.</returns> public IEnumerable<object> GetServices(Type serviceType) { if (!ResolutionExtensions.IsRegistered((IComponentContext)(object)_lifetimeScope, serviceType)) { return Enumerable.Empty<object>(); } Type enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType); return (IEnumerable<object>)ResolutionExtensions.Resolve((IComponentContext)(object)_lifetimeScope, enumerableServiceType); }
///<summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ///</summary> publicvoidDispose() { Dispose(disposing: true); GC.SuppressFinalize(this); }
///<summary> /// Releases unmanaged and - optionally - managed resources. ///</summary> ///<param name="disposing"> ///<see langword="true" /> to release both managed and unmanaged resources; ///<see langword="false" /> to release only unmanaged resources. ///</param> protectedvirtualvoidDispose(bool disposing) { if (!_disposed) { if (disposing && _lifetimeScope != null) { ((IDisposable)_lifetimeScope).Dispose(); } _disposed = true; } } }
using Autofac; using Autofac.Core.Lifetime; using Autofac.Integration.WebApi; using System; using System.Collections.Generic; using System.Web.Http.Dependencies;
///<summary> /// Autofac implementation of the <see cref="T:System.Web.Http.Dependencies.IDependencyResolver" /> interface. ///</summary> publicclassAutofacWebApiDependencyResolver : IDependencyResolver, IDependencyScope, IDisposable { privatebool _disposed;
///<summary> /// Gets the root container provided to the dependency resolver. ///</summary> public ILifetimeScope Container => _container;
///<summary> /// Initializes a new instance of the <see cref="T:Autofac.Integration.WebApi.AutofacWebApiDependencyResolver" /> class. ///</summary> ///<param name="container">The container that nested lifetime scopes will be create from.</param> ///<param name="configurationAction">A configuration action that will execute during lifetime scope creation.</param> publicAutofacWebApiDependencyResolver(ILifetimeScope container, Action<ContainerBuilder> configurationAction) : this(container) { if (configurationAction == null) { thrownew ArgumentNullException("configurationAction"); } _configurationAction = configurationAction; }
///<summary> /// Initializes a new instance of the <see cref="T:Autofac.Integration.WebApi.AutofacWebApiDependencyResolver" /> class. ///</summary> ///<param name="container">The container that nested lifetime scopes will be create from.</param> publicAutofacWebApiDependencyResolver(ILifetimeScope container) { if (container == null) { thrownew ArgumentNullException("container"); } _container = container; _rootDependencyScope = (IDependencyScope)(object)new AutofacWebApiDependencyScope(container); }
///<summary> /// Finalizes an instance of the <see cref="T:Autofac.Integration.WebApi.AutofacWebApiDependencyResolver" /> class. ///</summary> ~AutofacWebApiDependencyResolver() { Dispose(disposing: false); }
///<summary> /// Try to get a service of the given type. ///</summary> ///<param name="serviceType">Type of service to request.</param> ///<returns>An instance of the service, or null if the service is not found.</returns> publicvirtualobjectGetService(Type serviceType) { return _rootDependencyScope.GetService(serviceType); }
///<summary> /// Try to get a list of services of the given type. ///</summary> ///<param name="serviceType">ControllerType of services to request.</param> ///<returns>An enumeration (possibly empty) of the service.</returns> publicvirtual IEnumerable<object> GetServices(Type serviceType) { return _rootDependencyScope.GetServices(serviceType); }
///<summary> /// Starts a resolution scope. Objects which are resolved in the given scope will belong to /// that scope, and when the scope is disposed, those objects are returned to the container. ///</summary> ///<returns> /// The dependency scope. ///</returns> public IDependencyScope BeginScope() { return (IDependencyScope)(object)new AutofacWebApiDependencyScope((_configurationAction == null) ? _container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag) : _container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag, _configurationAction)); }
///<summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. ///</summary> publicvoidDispose() { Dispose(disposing: true); GC.SuppressFinalize(this); }
///<summary> /// Releases unmanaged and - optionally - managed resources. ///</summary> ///<param name="disposing"> ///<see langword="true" /> to release both managed and unmanaged resources; ///<see langword="false" /> to release only unmanaged resources. ///</param> protectedvirtualvoidDispose(bool disposing) { if (!_disposed) { if (disposing && _rootDependencyScope != null) { ((IDisposable)_rootDependencyScope).Dispose(); } _disposed = true; } } }