| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using Autofac;
- using Autofac.Core.Lifetime;
- using Autofac.Integration.Mvc;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web;
- namespace GreenTree.Nachtragsmanagement.Core.Plugins
- {
- public static class PluginExtensions
- {
- public static string GetLogoUrl(this PluginDescriptor pluginDescriptor, IWebHelper webHelper)
- {
- if (pluginDescriptor == null)
- throw new ArgumentNullException("pluginDescriptor");
- if (webHelper == null)
- throw new ArgumentNullException("webHelper");
- if (pluginDescriptor.OriginalAssemblyFile == null || pluginDescriptor.OriginalAssemblyFile.Directory == null)
- {
- return null;
- }
- var pluginDirectory = pluginDescriptor.OriginalAssemblyFile.Directory;
- var logoLocalPath = Path.Combine(pluginDirectory.FullName, "logo.jpg");
- if (!File.Exists(logoLocalPath))
- {
- return null;
- }
- string logoUrl = string.Format("~/plugins/{0}/logo.jpg", pluginDirectory.Name);
- return logoUrl;
- }
- #region Container
- public static T Resolve<T>(this IContainer container, string key = "", ILifetimeScope scope = null) where T : class
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- if (string.IsNullOrEmpty(key))
- {
- return scope.Resolve<T>();
- }
- return scope.ResolveKeyed<T>(key);
- }
- public static object Resolve(this Type type, IContainer container, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- return scope.Resolve(type);
- }
- public static T[] ResolveAll<T>(this IContainer container, string key = "", ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- if (string.IsNullOrEmpty(key))
- {
- return scope.Resolve<IEnumerable<T>>().ToArray();
- }
- return scope.ResolveKeyed<IEnumerable<T>>(key).ToArray();
- }
- public static T ResolveUnregistered<T>(this IContainer container, ILifetimeScope scope = null) where T : class
- {
- return ResolveUnregistered(container, typeof(T), scope) as T;
- }
- public static object ResolveUnregistered(this IContainer container, Type type, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- var constructors = type.GetConstructors();
- foreach (var constructor in constructors)
- {
- try
- {
- var parameters = constructor.GetParameters();
- var parameterInstances = new List<object>();
- foreach (var parameter in parameters)
- {
- var service = Resolve(parameter.ParameterType, container, scope);
- if (service == null) throw new Exception("Unkown dependency");
- parameterInstances.Add(service);
- }
- return Activator.CreateInstance(type, parameterInstances.ToArray());
- }
- catch (Exception)
- {
- }
- }
- throw new Exception("No contructor was found that had all the dependencies satisfied.");
- }
- public static bool TryResolve(this IContainer container, Type serviceType, ILifetimeScope scope, out object instance)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- return scope.TryResolve(serviceType, out instance);
- }
- public static bool IsRegistered(this IContainer container, Type serviceType, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- return scope.IsRegistered(serviceType);
- }
- public static object ResolveOptional(this IContainer container, Type serviceType, ILifetimeScope scope = null)
- {
- if (scope == null)
- {
- //no scope specified
- scope = Scope(container);
- }
- return scope.ResolveOptional(serviceType);
- }
- public static ILifetimeScope Scope(this IContainer container)
- {
- try
- {
- if (HttpContext.Current != null)
- return AutofacDependencyResolver.Current.RequestLifetimeScope;
- //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
- return container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
- }
- catch (Exception)
- {
- //we can get an exception here if RequestLifetimeScope is already disposed
- //for example, requested in or after "Application_EndRequest" handler
- //but note that usually it should never happen
- //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
- return container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
- }
- }
- #endregion
- }
- }
|