tags:

views:

579

answers:

3

Pulling my hair out trying to debug this one. Earlier this morning, this code was working fine, and I can't see what I've changed to break it. Now, whenever I try to open an nHibernate session, I'm getting the following error:

Test method BCMS.Tests.Repositories.BlogBlogRepositoryTests.can_get_recent_blog_posts threw exception: System.TypeInitializationException: The type initializer for 'NHibernate.Cfg.Environment' threw an exception. ---> System.Runtime.Serialization.SerializationException: Type is not resolved for member 'Castle.DynamicProxy.Serialization.ProxyObjectReference,Rhino.Mocks, Version=3.5.0.1337, Culture=neutral, PublicKeyToken=0b3305902db7183f'..

Any thoughts on how to debug what's going on here?

A: 

Some more info... it seems to be related to switching the Thread.CurrentPrincipal to a mocked IPrincipal implementation. I do all my security checks in my domain model inside the entities. The entity's methods check Thread.CurrentPrincipal.IsInRole() before modifying properties on the entity.

So, in order to test the entity's methods, I have to set different users (contributor user, moderator user, etc.) before I make my entity method calls.

I haven't figured out why this was working fine yesterday.

Here's an example of my Mocked IPrincipal:

        private static IPrincipal _blogContributorUser = null;
    public static IPrincipal BlogContributorUser
    {
        get
        {
            if (null == _blogContributorUser)
            {
                var identity = MockRepository.GenerateStub<IIdentity>(); 
                identity.Stub(p => p.Name).Return("BlogContributor").Repeat.Any(); 
                var principal = MockRepository.GenerateStub<IPrincipal>(); 
                principal.Stub(p => p.Identity).Return(identity).Repeat.Any();
                principal.Stub(p => p.IsInRole(UserRoles.BlogContributor)).Return(true).Repeat.Any();
                principal.Stub(p => p.IsInRole(UserRoles.CommentContributor)).Return(true).Repeat.Any();
                principal.Stub(p => p.IsInRole(UserRoles.TagContributor)).Return(true).Repeat.Any();
                _blogContributorUser = principal;
            }
            return _blogContributorUser;
        }
    }
A: 

Errors like this usually indicate versioning issues.

What I suspect may be happening is that both RhinoMocks and NHibernate are making use of Castle.DynamicProxy type, but they are asking for different versions of that type.

Did you recently uprade RhinoMocks or NHibernate to a newer version?

If this isn't the issue, then more information would be helpful - do all tests fail, or just this particular one?

edit You may also wish to try adding these lines to your Properties\AssemblyInfo.cs file:

[assembly: InternalsVisibleTo("Rhino.Mocks")] 
[assembly: InternalsVisibleTo("Castle.DynamicProxy")]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
Judah Himango
A: 

I have the same issue. It looks like that it has trouble reading the config file, since CurrentPrincipal is changed. I have moved all that has to be initialized from the config file, before replacing the CurrentPrincipal (for example, opened NHibernate session, initialized Unity and that kind of stuff), and everything works after that. Of course, this is not a solution, just a workaround figured out by a desperate man.

vucetica