Unity under .NET 4.0 TypeLoadException

Oct 19, 2009 at 5:35 AM

When the current Unity build is compiled under .NET 4.0 Beta 1 using Visual Studio 10 Beta 1 the code below throws a TypeLoadException in UnityContainer.DoBuild with the following details.

Additional information: Inheritance security rules violated while overriding member: 'Microsoft.Practices.Unity.ResolutionFailedException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

Any thoughts and work arounds?

public interface IRepository
{

void Test();

}

public class Repository : IRepository
{

public void Test(){}

}

public void DoTest()

{

IUnityContainer container = new UnityContainer();

container.RegisterType<IRepository, Repository>();

IRepository repository = container.Resolve<IRepository>();

if (repository == null)
{
     Response.Write("Repository failed to initialize.");
}
else
{
   
Response.Write(
"Repository initialized successfully.");
}

}

Oct 19, 2009 at 6:14 AM

Did some follow-up testing. Downloaded the current build again and attempted to build out of the box with Visual Studio 2010 Beta 1. Crashed the compiler multiple times. Tried again with the current free WebDeveloper version and built for 3.5. Same code as above it works. So, definitely a .NET 4.0/Visual Studio 2010 Beta 1 issue.

Oct 19, 2009 at 6:21 AM

We've been waiting for Beta 2 to release before we do any work on cross-compiling for .NET 4.0. We're not planning on shipping 4.0 only binaries (the 3.5 version should work just fine in a 4.0 process), but we'd like it to compile and work with 4.0 if people want to do that themselves.

 

Oct 21, 2009 at 1:29 AM

I've gotten a little farther and have realized that if I use ResolveType<From,To> I get the error but if use RegisterInstance<From>(instance) I don't get the error, regardless of what version of Unity, .NET Framework, or compiler I am using. I've gone all the way back to Unity 1.2 under .NET 2.0. Am I missing something simple?

Oct 21, 2009 at 6:05 AM

Well, there is no ResolveType<From, To> call. Do you mean RegisterType<From, To>?

And I use that all the time, so there must be something in your app that we're not seeing. The error has to do with serialization on .NET 4.0; you definitely should not be seeing it on .NET 2.0.

 

 

Oct 21, 2009 at 8:15 PM

Yes, I meant RegisterType. Should have reviewed my post more carefully. I have used Unity previously with no problems. The primary difference for me now, but I'm not sure how it would be relevant, is I used the installer before but am compiling the source myself this time on a machine that the installer was not run on.

 

Oct 23, 2009 at 12:58 AM
Edited Oct 23, 2009 at 1:09 AM

Resolved the problem and it sort of wasn't Unity. I had two constructors, the second taking a connection string which was blowing up Resolve. I changed the second constructor to take a parameter of IConnectionString as declared below, then initialized an instance of an IConnectionString implementer and registered it with the container. Resolve then works beautifully under Visual Studio 10 Beta 1 and .NET 4.0 Beta 1.

public interface IConnectionString
{
  
string Value { get; }
}

public class ConnectionString : IConnectionString
{
   public ConnectionString(string connectionString)
   {
      if (string.IsNullOrEmpty(connectionString))
      {
         throw new ArgumentNullException();
      }
      this.Value = connectionString;
   }

   public string Value
   {
      get;
      private set;
   }
}

In retrospect, including in the error message where the class/method/constructor in the requested resolution where the buildup error occurred would have made it much easier to resolve my mistake. Took me about 10 hours of time to figure this simple issue out. Uggh, do I feel dumb.