Class that manages the binding redirect config section
Пример #1
0
        public void AddingBindingRedirectToEmptyConfig()
        {
            // Arrange            
            var mockFileSystem = new MockFileSystem();
            mockFileSystem.AddFile("config", @"<configuration></configuration>");

            var bindingRedirectManager = new BindingRedirectManager(mockFileSystem, "config");
            AssemblyBinding assemblyBinding = GetAssemblyBinding("AssemblyName", "token", "3.0.0.0");

            // Act
            bindingRedirectManager.AddBindingRedirects(new[] { assemblyBinding });

            // Assert
            string outputContent = mockFileSystem.OpenFile("config").ReadToEnd();
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
      <dependentAssembly>
        <assemblyIdentity name=""AssemblyName"" publicKeyToken=""token"" culture=""neutral"" />
        <bindingRedirect oldVersion=""0.0.0.0-3.0.0.0"" newVersion=""3.0.0.0"" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>", outputContent);
        }
Пример #2
0
        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;
        }
        public void AddingBindingRedirectToEmptyConfig()
        {
            // Arrange            
            var mockFileSystem = new Mock<IFileSystem>();
            mockFileSystem.Setup(m => m.FileExists("config")).Returns(true);
            mockFileSystem.Setup(m => m.OpenFile("config")).Returns(@"<configuration></configuration>".AsStream());
            var ms = new MemoryStream();
            mockFileSystem.Setup(m => m.AddFile("config", It.IsAny<Stream>())).Callback<string, Stream>((path, stream) =>
            {
                stream.CopyTo(ms);
                ms.Seek(0, SeekOrigin.Begin);
            });

            var bindingRedirectManager = new BindingRedirectManager(mockFileSystem.Object, "config");
            AssemblyBinding assemblyBinding = GetAssemblyBinding("AssemblyName", "token", "3.0.0.0");

            // Act
            bindingRedirectManager.AddBindingRedirects(new[] { assemblyBinding });

            // Assert
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
      <dependentAssembly>
        <assemblyIdentity name=""AssemblyName"" publicKeyToken=""token"" culture=""neutral"" />
        <bindingRedirect oldVersion=""0.0.0.0-3.0.0.0"" newVersion=""3.0.0.0"" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>", ms.ReadToEnd());
        }
Пример #4
0
        public void AddingBindingRedirectWithMultipleAssemblyBindingSectionsAddsToFirstOne()
        {
            // Arrange            
            var mockFileSystem = new MockFileSystem();
            mockFileSystem.AddFile("config", @"
<configuration>
    <runtime>
        <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
            <dependentAssembly>
                <assemblyIdentity name=""System.Web.Mvc"" publicKeyToken=""31bf3856ad364e35"" />
                <bindingRedirect oldVersion=""1.0.0.0"" newVersion=""2.0.0.0"" />
            </dependentAssembly>
        </assemblyBinding>
        <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
            <dependentAssembly>
                <assemblyIdentity name=""A.Library"" publicKeyToken=""a34a755ec277222f"" />
                <bindingRedirect oldVersion=""1.0.0.0-2.0.0.0"" newVersion=""2.0.0.0"" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>");

            var bindingRedirectManager = new BindingRedirectManager(mockFileSystem, "config");
            AssemblyBinding assemblyBinding = GetAssemblyBinding("AssemblyName", "token", "3.0.0.0");

            // Act
            bindingRedirectManager.AddBindingRedirects(new[] { assemblyBinding });

            // Assert
            string outputContent = mockFileSystem.OpenFile("config").ReadToEnd();
            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
    <runtime>
        <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
            <dependentAssembly>
                <assemblyIdentity name=""System.Web.Mvc"" publicKeyToken=""31bf3856ad364e35"" />
                <bindingRedirect oldVersion=""1.0.0.0"" newVersion=""2.0.0.0"" />
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name=""AssemblyName"" publicKeyToken=""token"" culture=""neutral"" />
                <bindingRedirect oldVersion=""0.0.0.0-3.0.0.0"" newVersion=""3.0.0.0"" />
            </dependentAssembly>
        </assemblyBinding>
        <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
            <dependentAssembly>
                <assemblyIdentity name=""A.Library"" publicKeyToken=""a34a755ec277222f"" />
                <bindingRedirect oldVersion=""1.0.0.0-2.0.0.0"" newVersion=""2.0.0.0"" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>", outputContent);
        }
Пример #5
0
        private static IEnumerable<AssemblyBinding> AddBindingRedirects(Project project, AppDomain domain, IDictionary<string, HashSet<string>> projectAssembliesCache)
        {
            var redirects = Enumerable.Empty<AssemblyBinding>();
            // Only add binding redirects to projects that aren't class libraries
            if (project.SupportsConfig()) {
                // Create a project system
                IFileSystem fileSystem = VsProjectSystemFactory.CreateProjectSystem(project);

                // Run this on the UI thread since it enumerates all references
                IEnumerable<string> assemblies = ThreadHelper.Generic.Invoke(() => project.GetAssemblyClosure(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;
        }
Пример #6
0
        private static IEnumerable<AssemblyBinding> AddBindingRedirects(
            Project project, 
            IFileSystemProvider fileSystemProvider, 
            AppDomain domain, 
            IDictionary<string, HashSet<string>> projectAssembliesCache, 
            IVsFrameworkMultiTargeting frameworkMultiTargeting,
            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);

                if (frameworkMultiTargeting != null)
                {
                    // filter out assemblies that already exist in the target framework (CodePlex issue #3072)
                    string targetFramework = project.GetTargetFramework();
                    redirects = redirects.Where(p => !IsAssemblyAvaialbleInFramework(p.Name, targetFramework, frameworkMultiTargeting));
                }

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

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

            return redirects;
        }
Пример #7
0
        private void AddBindingRedirects(AppDomain appDomain)
        {
            if (DoNotAddBindingRedirects)
            {
                return;
            }
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(appDomain_AssemblyResolve);
            // 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);
            }
            AppDomain.CurrentDomain.AssemblyResolve -= appDomain_AssemblyResolve;
        }
Пример #8
0
        public void AddBindingRedirectUpdatesElementsPreservingCommentsAndWhitespace()
        {
            // Arrange
            var mockFileSystem = new MockFileSystem();
            mockFileSystem.AddFile("config", @"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
      <dependentAssembly><assemblyIdentity name=""AssemblyName"" publicKeyToken=""token"" culture=""neutral"" />
        <!-- This is a comment that should not be removed -->
        <bindingRedirect oldVersion=""2.0.0.0"" newVersion=""3.0.0.0"" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>");

            var bindingRedirectManager = new BindingRedirectManager(mockFileSystem, "config");
            AssemblyBinding assemblyBinding = GetAssemblyBinding("AssemblyName", "token", "4.0.0.0");

            // Act
            bindingRedirectManager.AddBindingRedirects(new[] { assemblyBinding });

            // Assert
            string outputContent = mockFileSystem.OpenFile("config").ReadToEnd();

            Assert.Equal(@"<?xml version=""1.0"" encoding=""utf-8""?>
<configuration>
  <runtime>
    <assemblyBinding xmlns=""urn:schemas-microsoft-com:asm.v1"">
      <dependentAssembly><assemblyIdentity name=""AssemblyName"" publicKeyToken=""token"" culture=""neutral"" />
        <!-- This is a comment that should not be removed -->
        <bindingRedirect oldVersion=""0.0.0.0-4.0.0.0"" newVersion=""4.0.0.0"" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>", outputContent);
        }