WPF单.exe Microsoft.Threading.Tasks.dll

时间:2014-07-04 08:26:44

标签: c# wpf wpf-4.0

关于使用模板http://visualstudiogallery.msdn.microsoft.com/15e7309a-fda2-46fa-ad49-a8dd3af26feb在单个.exe中打包WPF .NET4程序集,我有一个奇怪的问题 一切正常,除了Microsoft.Bcl库,特别是(Microsoft.Threading.Tasks.dll)。我试图添加(Microsoft.Threading.Tasks.dll)并将其标记为资源和/或嵌入资源,但是如果指定的dll不在主.exe附近,则应用程序将以非正常方式退出。任何想法?

负责打包.dll的主要代码是(取自Template):

private static Assembly OnResolveAssembly(object sender, ResolveEventArgs e)
    {
        var thisAssembly = Assembly.GetExecutingAssembly();

        // Get the Name of the AssemblyFile
        var assemblyName = new AssemblyName(e.Name);
        var dllName = assemblyName.Name + ".dll";

        // Load from Embedded Resources - This function is not called if the Assembly is already
        // in the same folder as the app.
        var resources = thisAssembly.GetManifestResourceNames().Where(s => s.EndsWith(dllName));
        if (resources.Any())
        {

            // 99% of cases will only have one matching item, but if you don't,
            // you will have to change the logic to handle those cases.
            var resourceName = resources.First();
            using (var stream = thisAssembly.GetManifestResourceStream(resourceName))
            {
                if (stream == null) return null;
                var block = new byte[stream.Length];

                // Safely try to load the assembly.
                try
                {
                    stream.Read(block, 0, block.Length);
                    return Assembly.Load(block);
                }
                catch (IOException)
                {
                    return null;
                }
                catch (BadImageFormatException)
                {
                    return null;
                }
            }
        }

        // in the case the resource doesn't exist, return null.
        return null;
    }

0 个答案:

没有答案