... or one of its dependencies. The system cannot find the file specified... 加载指定dll失败

我们在写代码的时候偶尔会遇到下面的问题。

... or one of its dependencies. The system cannot find the file specified... 这类问题主要是告诉用户当前的dll找不到或者是缺少相应的引用。大家不妨加上下面的catch语句试下。我们看下errorMessage后就知道问题出在哪里了。

using System.IO;
using System.Reflection;

try
{
    //The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
    StringBuilder sb = new StringBuilder();
    foreach (Exception exSub in ex.LoaderExceptions)
    {
        sb.AppendLine(exSub.Message);
        if (exSub is FileNotFoundException)
        {
            FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
            if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
            {
                sb.AppendLine("Fusion Log:");
                sb.AppendLine(exFileNotFound.FusionLog);
            }
        }
        sb.AppendLine();
    }
    string errorMessage = sb.ToString();
    //Display or log the error based on your application.
}

你可能感兴趣的:(WCF,WPF)