how to fix the assembly problem when project A call project B as reference

1. Project B is coded with CodedUI Test

2. Project A is a Console Application, which has a reference from project B.

When run project A, there is a exception from project B on "playback.playbacksettings": 

       "微笑": Could not load file or assembly 'Microsoft.VisualStudio.TestTools.UITest.Playback, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.":"Microsoft.VisualStudio.TestTools.UITest.Playback, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 

Resolution:

1. Add reference to Microsoft.VisualStudio.TestTools.UITesting.dll, Microsoft.VisualStudio.TestTools.UITest.Extension.dll and Microsoft.VisualStudio.TestTools.UITest.Common.dll with CopyLocal as false.

2. Write an assembly resolver. Follow the code below

using System;
using System.Reflection;
using System.Globalization;
using System.IO;
using Microsoft.Win32;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            
            // Your code here.
        }

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            AssemblyName assemblyName = new AssemblyName(args.Name);
            if (assemblyName.Name.StartsWith("Microsoft.VisualStudio.TestTools.UITest", StringComparison.Ordinal))
            {
                string path = string.Empty;
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\VisualStudio\10.0"))
                {
                    if (key != null)
                    {
                        path = key.GetValue("InstallDir") as string;
                    }
                }

                if (!string.IsNullOrWhiteSpace(path))
                {
                    string assemblyPath = Path.Combine(path, "PublicAssemblies",
                        string.Format(CultureInfo.InvariantCulture, "{0}.dll", assemblyName.Name));

                    if (!File.Exists(assemblyPath))
                    {
                        assemblyPath = Path.Combine(path, "PrivateAssemblies",
                            string.Format(CultureInfo.InvariantCulture, "{0}.dll", assemblyName.Name));

                        if (!File.Exists(assemblyPath))
                        {
                            string commonFiles = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86);
                            if (string.IsNullOrWhiteSpace(commonFiles))
                            {
                                commonFiles = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles);
                            }

                            assemblyPath = Path.Combine(commonFiles, "Microsoft Shared", "VSTT", "10.0",
                                string.Format(CultureInfo.InvariantCulture, "{0}.dll", assemblyName.Name));
                        }
                    }

                    if (File.Exists(assemblyPath))
                    {
                        return Assembly.LoadFrom(assemblyPath);
                    }
                }
            }

            return null;
        }
    }
}

info from:  http://blogs.kungfucoder.com/blog/post/2011/10/11/Coded-UI-Testing-Could-not-load-Visual-Studio-Test-Assembly.aspx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值