4

I recently asked a question about using Fluent NHibernate with .NET 4 - I solved that problem, but met a new one.

Summary
My main problem (at the moment) is configuring the database. I'm following this guide, but trying to work against SQL Server 2008 Express instead, as that's what I'll be using and thus what I need to learn.

The failing code:

public static ISessionFactory CreateSessionFactory()
{
    return Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c => c.FromConnectionStringWithKey("mssql")))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
        .ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
        .BuildSessionFactory();
}

When I try to run my application, I get the following exception on the last line (.BuildSessionFactory()):

Inheritance security rules violated while overriding member: 'FluentNHibernate.Cfg.FluentConfigurationException.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.

What is causing this?

Community
  • 1
  • 1
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • I have posted your question to Flune NHibernate mailing list. Hopefully they can help with it. http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/ae4013d711d2e4ad?hl=en – Meligy Dec 18 '09 at 00:56
  • just updated my answer, check out the new link – Mauricio Scheffer Dec 18 '09 at 01:56
  • Did you actually make it work? The solution below fixed the first problem, but then I got further problems: http://stackoverflow.com/questions/2697795/using-fluentnhibernate-with-net4 – stiank81 Apr 23 '10 at 10:30

1 Answers1

8

From the Microsoft Connect issue:

Security attributes need to be re-applied on types that derive from other types that also have security attributes.

Maybe FluentConfigurationException needs to apply a [SecurityPermission] attribute to its GetObjectData() method.

Else check out this blog post.

EDIT: The final solution was adding [SecurityCritical] to FluentConfigurationException.GetObjectData()

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • OK - but this is a class defined in the FluentNHibernate assembly. Should I take this as a bug in FluentNHibernate (that I can fix in my own copy of the source) or as a problem in my implementation of FNH? – Tomas Aschan Dec 17 '09 at 22:45
  • I looked at the source for System.Exception (see link below) and added the same attribute to FluentConfigurationException in my local copy, but it didn't help. Now that I read the error message again, it seems more like some other type is inheriting FluentConfigurationException, without the correct permissions, but I can't figure out where. Link to source: http://labs.developerfusion.co.uk/SourceViewer/browse.aspx?assembly=SSCLI&namespace=System&type=Exception – Tomas Aschan Dec 17 '09 at 23:03
  • Thanks for the update! I looked in the blog post and did what it suggested (added the attribute to `AssemblyInfo.cs`) but it didn't help. I'll keep an eye on that news group question =) – Tomas Aschan Dec 18 '09 at 14:52
  • Thanks for the info, see http://groups.google.com/group/fluent-nhibernate/browse_thread/thread/ae4013d711d2e4ad for the fix. Not sure when the code will be merged into the main line. – Chris Chilvers Dec 20 '09 at 17:28
  • Thanks a lot guys! I will try this out as soon as I can on my local copy of the FNH source and keep you posted. – Tomas Aschan Dec 21 '09 at 00:46