handle type creation errors

This commit is contained in:
Luke Pulverenti 2013-10-02 12:20:11 -04:00
parent 9a4712cf22
commit 76faf89aca
2 changed files with 27 additions and 11 deletions

View File

@ -159,7 +159,7 @@ namespace MediaBrowser.Common.Implementations
protected IZipClient ZipClient { get; set; }
protected IIsoManager IsoManager { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="BaseApplicationHost{TApplicationPathsType}"/> class.
/// </summary>
@ -243,7 +243,7 @@ namespace MediaBrowser.Common.Implementations
/// Runs the startup tasks.
/// </summary>
/// <returns>Task.</returns>
public virtual Task RunStartupTasks()
public virtual Task RunStartupTasks()
{
return Task.Run(() =>
{
@ -384,7 +384,7 @@ namespace MediaBrowser.Common.Implementations
/// </summary>
/// <param name="type">The type.</param>
/// <returns>System.Object.</returns>
public object CreateInstance(Type type)
protected object CreateInstance(Type type)
{
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>
/// Registers the specified obj.
/// </summary>
@ -496,7 +515,11 @@ namespace MediaBrowser.Common.Implementations
/// <returns>IEnumerable{``0}.</returns>
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)
{

View File

@ -89,13 +89,6 @@ namespace MediaBrowser.Common
/// <returns>Task.</returns>
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>
/// Resolves this instance.
/// </summary>