فهرست منبع

handle type creation errors

Luke Pulverenti 11 سال پیش
والد
کامیت
76faf89aca
2فایلهای تغییر یافته به همراه27 افزوده شده و 11 حذف شده
  1. 27 4
      MediaBrowser.Common.Implementations/BaseApplicationHost.cs
  2. 0 7
      MediaBrowser.Common/IApplicationHost.cs

+ 27 - 4
MediaBrowser.Common.Implementations/BaseApplicationHost.cs

@@ -159,7 +159,7 @@ namespace MediaBrowser.Common.Implementations
         protected IZipClient ZipClient { get; set; }
         protected IZipClient ZipClient { get; set; }
 
 
         protected IIsoManager IsoManager { get; set; }
         protected IIsoManager IsoManager { get; set; }
-        
+
         /// <summary>
         /// <summary>
         /// Initializes a new instance of the <see cref="BaseApplicationHost{TApplicationPathsType}"/> class.
         /// Initializes a new instance of the <see cref="BaseApplicationHost{TApplicationPathsType}"/> class.
         /// </summary>
         /// </summary>
@@ -243,7 +243,7 @@ namespace MediaBrowser.Common.Implementations
         /// Runs the startup tasks.
         /// Runs the startup tasks.
         /// </summary>
         /// </summary>
         /// <returns>Task.</returns>
         /// <returns>Task.</returns>
-        public virtual  Task RunStartupTasks()
+        public virtual Task RunStartupTasks()
         {
         {
             return Task.Run(() =>
             return Task.Run(() =>
             {
             {
@@ -384,7 +384,7 @@ namespace MediaBrowser.Common.Implementations
         /// </summary>
         /// </summary>
         /// <param name="type">The type.</param>
         /// <param name="type">The type.</param>
         /// <returns>System.Object.</returns>
         /// <returns>System.Object.</returns>
-        public object CreateInstance(Type type)
+        protected object CreateInstance(Type type)
         {
         {
             try
             try
             {
             {
@@ -398,6 +398,25 @@ namespace MediaBrowser.Common.Implementations
             }
             }
         }
         }
 
 
+        /// <summary>
+        /// Creates the instance safe.
+        /// </summary>
+        /// <param name="type">The type.</param>
+        /// <returns>System.Object.</returns>
+        protected object CreateInstanceSafe(Type type)
+        {
+            try
+            {
+                return Container.GetInstance(type);
+            }
+            catch (Exception ex)
+            {
+                Logger.Error("Error creating {0}", ex, type.Name);
+
+                return null;
+            }
+        }
+
         /// <summary>
         /// <summary>
         /// Registers the specified obj.
         /// Registers the specified obj.
         /// </summary>
         /// </summary>
@@ -496,7 +515,11 @@ namespace MediaBrowser.Common.Implementations
         /// <returns>IEnumerable{``0}.</returns>
         /// <returns>IEnumerable{``0}.</returns>
         public IEnumerable<T> GetExports<T>(bool manageLiftime = true)
         public IEnumerable<T> GetExports<T>(bool manageLiftime = true)
         {
         {
-            var parts = GetExportTypes<T>().Select(CreateInstance).Cast<T>().ToList();
+            var parts = GetExportTypes<T>()
+                .Select(CreateInstanceSafe)
+                .Where(i => i != null)
+                .Cast<T>()
+                .ToList();
 
 
             if (manageLiftime)
             if (manageLiftime)
             {
             {

+ 0 - 7
MediaBrowser.Common/IApplicationHost.cs

@@ -89,13 +89,6 @@ namespace MediaBrowser.Common
         /// <returns>Task.</returns>
         /// <returns>Task.</returns>
         Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress<double> progress);
         Task UpdateApplication(PackageVersionInfo package, CancellationToken cancellationToken, IProgress<double> progress);
 
 
-        /// <summary>
-        /// Creates an instance of type and resolves all constructor dependancies
-        /// </summary>
-        /// <param name="type">The type.</param>
-        /// <returns>System.Object.</returns>
-        object CreateInstance(Type type);
-
         /// <summary>
         /// <summary>
         /// Resolves this instance.
         /// Resolves this instance.
         /// </summary>
         /// </summary>