Search code examples
.net.net-assemblyassembly-resolution

.NET assembly binding


I have an api, MyApi, which is consumed by an application, MyApplication.

MyApi has several dlls targeted to the .NET 4.0 Client Profile. For example:

  • MyApi.Core
  • MyApi.Domain
  • MyApi.Providers

(and some more)

For deployment simplicity, these are ILMerged into

  • MyApi.ClientProfile

All of the MyApis dlls are strong named.

All the projects in MyApplication reference MyApi.ClientProfile.

I'm now adding some web functionality into MyApi, so I've added a dll MyApi.Web, which is targeted to the .NET 4.0 Extended Profile. MyApi.Web references MyApi.Core and MyApi.Domain (as well as System.Web dlls).

There is now also a MyApplication.Web project. It references some other projects in MyApplication (which have references to the MyApi.ClientProfile dll). The MyApplication.Web project needs to reference MyApi.Web dll.

How can I do this?

If I add references to MyApi.Core and MyApi.Domain from MyApplication.Web, then there will be types that exists in multiple assemblies for MyApplication.Web.

If I don't add references to MyApi.Core and MyApi.Domain from MyApplication.Web, MyApi.Web will fail to load because it can't find MyApi.Core or MyApi.Domain (because only MyApi.ClientProfile is present).

I can't handle the assembly resolve event and redirect requests for MyApi.Core/MyApi.Domain to MyApi.ClientProfile because the MyApi assemblies are all strong named and that will make it fail.

I can't merge MyApi.Web into the MyApi.ClientProfile because MyApi.ClientProfile should be supported for the .NET 4.0 Client Profile (and MyApi.Web has references to System.Web, etc.).

If I change MyApi.Web to reference MyApi.ClientProfile, that should work for this one case, but isn't ideal, because it won't work for another application references MyApi.Core and MyApi.Domain directly.

I don't want to force MyApplication.Web to specify bypassTrustedAppStrongNames in its config (thought enabling this setting somehow from MyApi.Web directly might be an acceptable option....).

So, I'm stuck thinking of a suitable/elegant solution to this problem.

Any suggestions?

Thanks.


Solution

  • I came to a realization:

    If your assemblies have the same Public Key Token, you can do redirection and the name doesn't matter.

    So, because MyApi.Client profile is signed with the same key as MyApi.Core, if I handle AssemblyResolve in MyApi.Web, I actually CAN tell it to use MyApi.ClientProfile instead of MyApi.Core.

    I would have thought it would throw a strong name verification error. It does not....because the public keys are the same.