PluginExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Autofac;
  2. using Autofac.Core.Lifetime;
  3. using Autofac.Integration.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Web;
  11. namespace GreenTree.Nachtragsmanagement.Core.Plugins
  12. {
  13. public static class PluginExtensions
  14. {
  15. public static string GetLogoUrl(this PluginDescriptor pluginDescriptor, IWebHelper webHelper)
  16. {
  17. if (pluginDescriptor == null)
  18. throw new ArgumentNullException("pluginDescriptor");
  19. if (webHelper == null)
  20. throw new ArgumentNullException("webHelper");
  21. if (pluginDescriptor.OriginalAssemblyFile == null || pluginDescriptor.OriginalAssemblyFile.Directory == null)
  22. {
  23. return null;
  24. }
  25. var pluginDirectory = pluginDescriptor.OriginalAssemblyFile.Directory;
  26. var logoLocalPath = Path.Combine(pluginDirectory.FullName, "logo.jpg");
  27. if (!File.Exists(logoLocalPath))
  28. {
  29. return null;
  30. }
  31. string logoUrl = string.Format("~/plugins/{1}/logo.jpg", pluginDirectory.Name);
  32. return logoUrl;
  33. }
  34. #region Container
  35. public static T Resolve<T>(this IContainer container, string key = "", ILifetimeScope scope = null) where T : class
  36. {
  37. if (scope == null)
  38. {
  39. //no scope specified
  40. scope = Scope(container);
  41. }
  42. if (string.IsNullOrEmpty(key))
  43. {
  44. return scope.Resolve<T>();
  45. }
  46. return scope.ResolveKeyed<T>(key);
  47. }
  48. public static object Resolve(this Type type, IContainer container, ILifetimeScope scope = null)
  49. {
  50. if (scope == null)
  51. {
  52. //no scope specified
  53. scope = Scope(container);
  54. }
  55. return scope.Resolve(type);
  56. }
  57. public static T[] ResolveAll<T>(this IContainer container, string key = "", ILifetimeScope scope = null)
  58. {
  59. if (scope == null)
  60. {
  61. //no scope specified
  62. scope = Scope(container);
  63. }
  64. if (string.IsNullOrEmpty(key))
  65. {
  66. return scope.Resolve<IEnumerable<T>>().ToArray();
  67. }
  68. return scope.ResolveKeyed<IEnumerable<T>>(key).ToArray();
  69. }
  70. public static T ResolveUnregistered<T>(this IContainer container, ILifetimeScope scope = null) where T : class
  71. {
  72. return ResolveUnregistered(container, typeof(T), scope) as T;
  73. }
  74. public static object ResolveUnregistered(this IContainer container, Type type, ILifetimeScope scope = null)
  75. {
  76. if (scope == null)
  77. {
  78. //no scope specified
  79. scope = Scope(container);
  80. }
  81. var constructors = type.GetConstructors();
  82. foreach (var constructor in constructors)
  83. {
  84. try
  85. {
  86. var parameters = constructor.GetParameters();
  87. var parameterInstances = new List<object>();
  88. foreach (var parameter in parameters)
  89. {
  90. var service = Resolve(parameter.ParameterType, container, scope);
  91. if (service == null) throw new Exception("Unkown dependency");
  92. parameterInstances.Add(service);
  93. }
  94. return Activator.CreateInstance(type, parameterInstances.ToArray());
  95. }
  96. catch (Exception)
  97. {
  98. }
  99. }
  100. throw new Exception("No contructor was found that had all the dependencies satisfied.");
  101. }
  102. public static bool TryResolve(this IContainer container, Type serviceType, ILifetimeScope scope, out object instance)
  103. {
  104. if (scope == null)
  105. {
  106. //no scope specified
  107. scope = Scope(container);
  108. }
  109. return scope.TryResolve(serviceType, out instance);
  110. }
  111. public static bool IsRegistered(this IContainer container, Type serviceType, ILifetimeScope scope = null)
  112. {
  113. if (scope == null)
  114. {
  115. //no scope specified
  116. scope = Scope(container);
  117. }
  118. return scope.IsRegistered(serviceType);
  119. }
  120. public static object ResolveOptional(this IContainer container, Type serviceType, ILifetimeScope scope = null)
  121. {
  122. if (scope == null)
  123. {
  124. //no scope specified
  125. scope = Scope(container);
  126. }
  127. return scope.ResolveOptional(serviceType);
  128. }
  129. public static ILifetimeScope Scope(this IContainer container)
  130. {
  131. try
  132. {
  133. if (HttpContext.Current != null)
  134. return AutofacDependencyResolver.Current.RequestLifetimeScope;
  135. //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
  136. return container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
  137. }
  138. catch (Exception)
  139. {
  140. //we can get an exception here if RequestLifetimeScope is already disposed
  141. //for example, requested in or after "Application_EndRequest" handler
  142. //but note that usually it should never happen
  143. //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks)
  144. return container.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);
  145. }
  146. }
  147. #endregion
  148. }
  149. }