2

疑わしいコードを実行するテストを実行した後。Nunit (具体的には nunit-agent.exe) が終了するまで、Visual Studio でアセンブリを再構築できません。

エラーは次のとおりです。

Could not copy "C:\path\MyTests\MyTests.dll" to "bin\Debug\MyTests.dll". 
    Exceeded retry count of 10.
Unable to copy file "C:\path\MyTests\Debug\MyTests.dll" 
    to "bin\Debug\MyTests.dll". The process cannot access the file 
    'bin\Debug\MyTests.dll' because it is being used by another process.

現在の回避策は、nunit を閉じ、再構築してから、nunit を再度開く (そしてテストする) ことです。痛い

レッドニシンは、これがボリューム シャドウ コピーの問題か、nunit プロジェクトのプロジェクト ベース パスの設定であると考えていました。これらではありません。それがこのコードです。

AppDomain dom = AppDomain.CreateDomain("some");
string fullPath = Assembly.GetExecutingAssembly().CodeBase
                  .Replace("file:///", "").Replace("/", "\\");
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = fullPath;
Assembly assembly = dom.Load(assemblyName);
Type type = assembly.GetType("ClassName");

IMyInterface obj = Activator.CreateInstance(type) as IMyInterface;

obj.ActionMessage(param1, param2);

これは処分の問題だと思ったので、IDisposable を実装し、クラス「ClassName」に必要なコードを追加しました。動作しませんでした。

この問題を解決するにはどうすればよいですか?

4

1 に答える 1

0

解決策は実行することです

AppDomain.Unload(dom);

完全な方法:

public static object InstObject(Assembly ass, string className)
{
    AppDomain dom = null;
    try
    {
        Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

        string fullPath = ass.CodeBase.Replace("file:///", "").Replace("/", "\\");
        AssemblyName assemblyName = new AssemblyName();
        assemblyName.CodeBase = fullPath;
        dom = AppDomain.CreateDomain("TestDomain", evidence, 
                 AppDomain.CurrentDomain.BaseDirectory, 
                 System.IO.Path.GetFullPath(fullPath), true);

        Assembly assembly = dom.Load(assemblyName);
        Type type = assembly.GetType(className);

        object obj = Activator.CreateInstance(type);

        return obj;
    }
    finally
    {
        if (dom != null) AppDomain.Unload(dom);
    }
}
于 2012-10-22T02:08:22.553 に答える