private static IEnumerable <AssemblyBinding> AddBindingRedirects(
            Project project,
            IFileSystemProvider fileSystemProvider,
            AppDomain domain,
            IDictionary <string, HashSet <string> > projectAssembliesCache,
            IVsFrameworkMultiTargeting frameworkMultiTargeting)
        {
            var redirects = Enumerable.Empty <AssemblyBinding>();

            // Create a project system
            IFileSystem fileSystem = VsProjectSystemFactory.CreateProjectSystem(project, fileSystemProvider);

            // Run this on the UI thread since it enumerates all references
            IEnumerable <string> assemblies = ThreadHelper.Generic.Invoke(() => project.GetAssemblyClosure(fileSystemProvider, projectAssembliesCache));

            redirects = BindingRedirectResolver.GetBindingRedirects(assemblies, domain);

            if (frameworkMultiTargeting != null)
            {
                // filter out assemblies that already exist in the target framework (CodePlex issue #3072)
                FrameworkName targetFrameworkName = project.GetTargetFrameworkName();
                redirects = redirects.Where(p => !FrameworkAssemblyResolver.IsHigherAssemblyVersionInFramework(p.Name, p.AssemblyNewVersion, targetFrameworkName, fileSystemProvider));
            }

            // Create a binding redirect manager over the configuration
            var manager = new BindingRedirectManager(fileSystem, project.GetConfigurationFile());

            // Add the redirects
            manager.AddBindingRedirects(redirects);

            return(redirects);
        }
Example #2
0
        public void GetBindingRedirectsOnlyRedirectsStrongNamedAssemblies()
        {
            // A, B2
            // A -> B1
            var A = new MockAssembly
            {
                Name           = "A",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = ""
            };

            var B1 = new MockAssembly
            {
                Name           = "B",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = null
            };

            var B2 = new MockAssembly
            {
                Name    = "B",
                Version = new Version("2.0.0.0")
            };

            var assemblies = new[] { A, B2 };

            A.References.Add(B1);

            // Act
            var redirectAssemblies = BindingRedirectResolver.GetBindingRedirects(assemblies).ToList();

            // Assert
            Assert.Equal(0, redirectAssemblies.Count);
        }
        public void GetBindingRedirectsTest()
        {
            // A, B, C2, G
            // A -> C1
            // B -> G
            // G -> C2
            var A = new MockAssembly {
                Name           = "A",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "a34a755ec277222f"
            };

            var B = new MockAssembly {
                Name           = "B",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "b34a755ec277222f"
            };

            var C1 = new MockAssembly {
                Name           = "C",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "c34a755ec277222f"
            };

            var C2 = new MockAssembly {
                Name           = "C",
                Version        = new Version("2.0.0.0"),
                PublicKeyToken = "c34a755ec277222f"
            };

            var D = new MockAssembly {
                Name           = "D",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "f34a755ec277222f"
            };

            var G = new MockAssembly {
                Name           = "G",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "d34a755ec277222f"
            };

            var assemblies = new[] { A, B, C2, D, G };

            B.References.Add(G);
            D.References.Add(C2);
            G.References.Add(C1);
            A.References.Add(C2);

            // Act
            var redirectAssemblies = BindingRedirectResolver.GetBindingRedirects(assemblies).ToList();

            // Assert
            Assert.AreEqual(1, redirectAssemblies.Count);
            Assert.AreEqual("C", redirectAssemblies[0].Name);
            Assert.AreEqual("2.0.0.0", redirectAssemblies[0].NewVersion);
        }
Example #4
0
        private void AddBindingRedirects()
        {
            IEnumerable <AssemblyBinding> bindingRedirects = BindingRedirectResolver.GetBindingRedirects(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain);

            if (bindingRedirects.Any())
            {
                BindingRedirectManager bindingRedirectManager = new BindingRedirectManager(mProjectManager.Project, mConfigurationFileName);
                bindingRedirectManager.AddBindingRedirects(bindingRedirects);
            }
        }
Example #5
0
        private void AddBindingRedirects(AppDomain appDomain)
        {
            if (DoNotAddBindingRedirects)
            {
                return;
            }
            // We can't use HttpRuntime.BinDirectory since there is no runtime when installing via WebMatrix.
            var binDirectory     = Path.Combine(_siteRoot, "bin");
            var assemblies       = RemoteAssembly.GetAssembliesForBindingRedirect(appDomain, binDirectory);
            var bindingRedirects = BindingRedirectResolver.GetBindingRedirects(assemblies);

            if (bindingRedirects.Any())
            {
                // NuGet ends up reading our web.config file regardless of if any bindingRedirects are needed.
                var bindingRedirectManager = new BindingRedirectManager(_projectManager.Project, "web.config");
                bindingRedirectManager.AddBindingRedirects(bindingRedirects);
            }
        }
Example #6
0
        private void RemoveBindingRedirects()
        {
            IEnumerable <AssemblyBinding> bindingRedirects = BindingRedirectResolver.GetBindingRedirects(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain);
            string    configurationFilePath = Path.Combine(mProjectManager.Project.Root, mConfigurationFileName);
            XDocument document       = XDocument.Load(configurationFilePath);
            XElement  runtimeElement = document.Root.Element("runtime");
            IEnumerable <XElement> assemblyBindingElements = Enumerable.Empty <XElement>();

            if (runtimeElement != null)
            {
                assemblyBindingElements = runtimeElement.Elements(XName.Get("assemblyBinding", "urn:schemas-microsoft-com:asm.v1")).Elements(XName.Get("dependentAssembly", "urn:schemas-microsoft-com:asm.v1"));
            }
            IEnumerable <AssemblyBinding> configurationBindingRedirects = assemblyBindingElements.Select(x => AssemblyBinding.Parse(x));
            IEnumerable <AssemblyBinding> candidates = configurationBindingRedirects.Except(bindingRedirects);

            if (candidates.Any())
            {
                BindingRedirectManager bindingRedirectManager = new BindingRedirectManager(mProjectManager.Project, mConfigurationFileName);
                bindingRedirectManager.RemoveBindingRedirects(candidates);
            }
        }
        public void GetBindingRedirectsOnlyRedirectsStrongNamedAssemblies()
        {
            // A, B, C2
            // A -> C2
            // B -> C1
            var A = new MockAssembly {
                Name           = "A",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "a34a755ec277222f"
            };

            var B = new MockAssembly {
                Name           = "B",
                Version        = new Version("1.0.0.0"),
                PublicKeyToken = "b34a755ec277222f"
            };

            var C1 = new MockAssembly {
                Name    = "C",
                Version = new Version("1.0.0.0")
            };

            var C2 = new MockAssembly {
                Name           = "C",
                Version        = new Version("2.0.0.0"),
                PublicKeyToken = "c34a755ec277222f"
            };

            var assemblies = new[] { A, B, C2 };

            A.References.Add(C2);
            B.References.Add(C1);

            // Act
            var redirectAssemblies = BindingRedirectResolver.GetBindingRedirects(assemblies).ToList();

            // Assert
            Assert.AreEqual(0, redirectAssemblies.Count);
        }
Example #8
0
        private static IEnumerable <AssemblyBinding> AddBindingRedirects(Project project, IFileSystemProvider fileSystemProvider, AppDomain domain, IDictionary <string, HashSet <string> > projectAssembliesCache, bool checkProjectType = true)
        {
            var redirects = Enumerable.Empty <AssemblyBinding>();

            // Only add binding redirects to projects that aren't class libraries
            if (!checkProjectType || project.SupportsConfig())
            {
                // Create a project system
                IFileSystem fileSystem = VsProjectSystemFactory.CreateProjectSystem(project, fileSystemProvider);

                // Run this on the UI thread since it enumerates all references
                IEnumerable <string> assemblies = ThreadHelper.Generic.Invoke(() => project.GetAssemblyClosure(fileSystemProvider, projectAssembliesCache));

                redirects = BindingRedirectResolver.GetBindingRedirects(assemblies, domain);

                // Create a binding redirect manager over the configuration
                var manager = new BindingRedirectManager(fileSystem, project.GetConfigurationFile());

                // Add the redirects
                manager.AddBindingRedirects(redirects);
            }

            return(redirects);
        }