Ambiguous binding redirect

esben's Avatar

esben

16 Nov, 2017 08:24 AM

Hi
I have an issue where my unit test project uses an ModuleInitializerAttribute to set the logging backend.

The project was upgraded to the "new csproj" format, but still targeting framework 4.6.2. Now when compiling the project I get an ambiguous binding redirect error as follows:

PS0099   Unhandled exception (postsharp-net40-x64-srv.exe release | .NET Framework 4.7): PostSharp.Sdk.CodeModel.BindingException: Ambiguous assembly binding policies: Assembly 'system.runtime, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a' redirects both to 'system.runtime, version=4.1.2.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a' and 'system.runtime, version=4.0.20.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a'.
   at PostSharp.Sdk.CodeModel.AssemblyBindingPolicyManager.EvaluatePolicy(IAssemblyName assemblyName, Boolean evaluateSystemPolicies)
   at PostSharp.Sdk.CodeModel.Domain.GetAssembly(IAssemblyName assemblyName, BindingOptions bindingOptions, AssemblyLoadContext context)
   at PostSharp.Sdk.CodeModel.AssemblyRefDeclaration.GetAssemblyEnvelope(BindingOptions bindingOptions)
   at PostSharp.Sdk.CodeModel.TypeRefDeclaration.GetTypeDefinition(BindingOptions bindingOptions)
   at PostSharp.Sdk.Extensibility.Tasks.TypeHierarchyTask.^L+R5xVbP(TypeDefDeclaration _0)
   at PostSharp.Sdk.Extensibility.Tasks.AnnotationRepositoryTask.^wwGmHrz+(IAnnotationInstance _0)
   at PostSharp.Sdk.Extensibility.Tasks.AnnotationRepositoryTask.Execute()
   at PostSharp.Sdk.Extensibility.Project.ExecutePhase(String phase)
   at PostSharp.Sdk.Extensibility.Project.Execute()
   at PostSharp.Hosting.PostSharpObject.ExecuteProjects()
   at PostSharp.Hosting.PostSharpObject.InvokeProject(ProjectInvocation projectInvocation). Infosoft.S4.Framework.AutoFac.UnitTest

Any ideas on how to work around it? Its postsharp 5.0.37 bw.

  1. Support Staff 1 Posted by PostSharp Techn... on 20 Nov, 2017 12:16 AM

    PostSharp Technologies's Avatar

    Hello,

    Please try to apply the following workaround for this issue.

    Review the *.config files of your projects and find the binding redirect for the 'system.runtime'.

    <dependentAssembly>
      <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
    </dependentAssembly>
    

    If this binding redirect exists then try to change newVersion to 4.0.20.0 or remove the redirect altogether.

    Please let us know whether the workaround was applicable in your case and whether it worked.

    -alex

  2. 2 Posted by esben on 20 Nov, 2017 07:02 AM

    esben's Avatar

    The point is to NOT have binding redirects in the app.config since the "new" csproj format automatically generate these during compile time based on the dependencies. Ill try and add a redirect manually, and see what happens.

  3. 3 Posted by esben on 20 Nov, 2017 07:32 AM

    esben's Avatar

    Okay, so adding the binding redirect does nothing. Probably because visualstudio ignores what is written there and generates something else, namely:

        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Runtime" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
            <bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
          </dependentAssembly>
        </assemblyBinding>
    

    If i remove the binding redirect (as it was before) the auto-generated entry disappears. Regardless i get the same error.

  4. 4 Posted by esben on 21 Nov, 2017 10:05 AM

    esben's Avatar

    By coincidence i have another rather small library with a unit test project that IS working with the new project format AND postsharp.

    That project is using 5.0.32, i have now tried all versions from .33 to .38 and they all exhibit some sort of error. .33 and .34 (possibly also .35) complaints about not being able to find System.Collections.IList version 4.0.0.0 but it seems to just be a different error message having the same root cause (guessing here).

    Anyway, it got broken in 5.0.33 apparently.

  5. Support Staff 5 Posted by PostSharp Techn... on 23 Nov, 2017 01:55 PM

    PostSharp Technologies's Avatar

    Hello,

    we are currently investigating this issue and we will let you know as soon as we make any progress.

    Thank you for your patience.

    Best regards,
    -jakub

  6. 6 Posted by esben on 27 Nov, 2017 08:34 AM

    esben's Avatar

    A small update from my side. I tried another workaround, being a regular csproj, but without packages.config (i.e. using packagereference tags in the project file). This results in the same error.

    Using packages.config works just fine.

    As far as i know, the commonality between packagereference tags and the new format (which also uses packagereference tags) is that the binding redirect is automatically generated, and whatever it generates is probably not something postsharp likes.

  7. Support Staff 7 Posted by PostSharp Techn... on 27 Nov, 2017 10:54 AM

    PostSharp Technologies's Avatar

    Hello,

    I'm sorry for the delay, we were analyzing this and other similar issues and we would need more information. The problem comes from the fact that PostSharp uses the app.config to do redirections specified by the user (which is needed in old projects). However there is a conflict between what is generated by MSBuild and what PostSharp needs when executing code from the assembly. We were doing a lot of changes lately, but there are still issues that remain.

    It would be best if you could provide us with a failing reproduction, since there are a lot of ways how to achieve the faulty behavior and I understand that most likely you will not be able to share the actual code (even redacted csproj files would help). In order to closely replicate your case we would need to know:

    1) Which kinds of assemblies is project referencing and if they are yours how were they built (i.e. NuGet packages, NetCore SDK version, NetStandard version, etc.)?
    2) In which assemblies do you declare aspects (i.e. .Net Standard, .Net Framework library, directly in the failing assembly etc.)?
    3) Are you using IAspectProvider/IAdviceProvider in your aspects?
    4) Which version of .NET Framework is installed on your machine (specifically, do you have .NET 4.7.1 installed?)
    5) Do you have package references or normal references in your project?

    Thanks!

    All the best,
    Daniel

  8. 8 Posted by esben on 27 Nov, 2017 11:24 AM

    esben's Avatar

    I actually think I have a project that I can share. Let me have a look and then i will create a reproduction with "not shareables" removed.

    Regarding the question, I think they will be asnwered if I shared the reproduction. Except from 4. I am targeting 4.6.2 but I have a slew of frameworks installed.

    • .Net Core SDK 1.0.0 Preview 2
    • .Net Core SDK 1.0.1 Preview 2
    • .Net Core SDK 1.0.0
    • .Net Core SDK 1.0.1
    • .Net Core SDK 1.0.2
    • .Net Core SDK 1.0.3
    • .Net Core SDK 1.0.4
    • .Net Core SDK 1.1.0
    • .Net Core SDK 2.0.0
    • .Net Core SDK 2.0.2
    • .Net Core SDK 2.0.3
    • .NET 4.5.1 SDK
    • .NET 4.5.2 SDK
    • .NET 4.6 SDK
    • .NET 4.6.1 SDK
    • .NET 4.6.2 .NET SDK
    • .NET 4.7 SDK

    Note: No 4.7.1 installation

  9. 9 Posted by esben on 27 Nov, 2017 11:30 AM

    esben's Avatar

    The reproduction was easier than imagined.
    Nothing terribly secret here (removed the license key configuration file).

  10. Support Staff 10 Posted by PostSharp Techn... on 28 Nov, 2017 04:34 PM

    PostSharp Technologies's Avatar

    Hello,

    We have identified the cause, but, unfortunately, the fix will require some breaking changes, so we plan these changes for the next version of PostSharp (probably 5.1). This issue is filed as #15684 and we'll let you know as soon as a preview of PostSharp 5.1 with this bug fixed is available.

    In the meanwhile, the workaround is not to use the new .csproj with PackageReference.

    You can also try to create an empty *.config file in the directory of your project or solution, then set PostSharpHostConfigurationFile MSBuild property to the relative path of this file (http://doc.postsharp.net/configuration-msbuild).

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    </configuration>
    

    Does the second work-around work for you?

    Best regards,
    -tony

  11. 11 Posted by esben on 29 Nov, 2017 07:18 AM

    esben's Avatar

    So i took the small reproduction project i sent to you and tried the workaround. It seems that no matter what I attempt, postsharp does not really care and still gives me the same error.

    I tried an empty config and putting the PostSharpHostConfigurationFile property in the csproj, and using msbuild in the cli using /p:PostSharpHostConfigurationFile it finds the file, but seems to not care. I also tried adding a binding redirect to the file, but still the same error.

    Did you manage to get it working with my reproduction? If so the workaround should be good enough.

  12. Support Staff 12 Posted by PostSharp Techn... on 29 Nov, 2017 03:48 PM

    PostSharp Technologies's Avatar

    Hello,

    My apologies. Yes, we have managed to reproduce the issue with the project you've sent. The work-around with empty config file is related to another issue. I've sent this suggestion to you by mistake.

    So the only work-around here is to revert your project to the "old" .csproj format until we release the first preview of PostSharp 5.1.

    You have mentioned another issue System.Collections.IList version 4.0.0.0. Is it related to the same project or another? Would you mind to share a reproduction of that with us as well, so we can have a look at it?

    Best regards,
    -tony

  13. 13 Posted by esben on 30 Nov, 2017 06:22 AM

    esben's Avatar

    Okay. No worries.

    The IList thing was the same project but with a different version of postsharp, as mentioned p reviously i went through all the minor versions - it might have been caused by something else, I just figured it was a variant of the same error, and all i did was upgrading postsharp nuget package version for version.

  14. Support Staff 14 Posted by PostSharp Techn... on 30 Nov, 2017 07:40 AM

    PostSharp Technologies's Avatar

    OK, thank you.

    We're closing the ticket for now as the bug has been internally filed as issue #15684. We will contact you as soon as the preview of PostSharp 5.1 with the bug fix has been released.

    For more details on our support policies and prioritization of bug fixes, please visit https://www.postsharp.net/support/policies.

    Best regards,
    -tony

  15. PostSharp Technologies closed this discussion on 30 Nov, 2017 07:40 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac