Pages

Thursday, January 13, 2011

NHibernate With Remoting

NHibernate With Remoting


NHibernate is a cool ORM and it's a very fast one that has robust options, and with fluent plugin it's even better. Recently I had to do some dummy application in NHibernate + Fluent using Remoting (Server App) and I want to focus on some errors that had my banging my head against the wall for some time.

Let's do this from the more obvious ones to the strange ones.

Lists

The first problem was the one to many collections where I just ended using the most obvious method "AsList()" when mapping my objects, but this actually requires a valid index column in the Db but I didn't want to specify that so the proper way for me was to use "AsBag()" method.

Schema Generation

Schema Export is a cool feature that generates the Db for you, but in my case it failed to drop table when I did by the book way.

var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Context).Assembly);
new SchemaExport(cfg).Execute(false,true,false,false);

The solution was to split this call into two separate calls the first drops the current schema and then the second executes it (Call method uses Execute).

var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Context).Assembly);
new SchemaExport(cfg).Execute(false,true,false,false);

var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Context).Assembly);

SchemaExport se = new SchemaExport(cfg);
se.Drop(true, true); 
se.Create(false, true);

Remoting

This was a real deal breaker for me, as I had my DAOs set up and when testing and they worked for the most part but some of them complained about ReflectionPermission Exception, by doing some investigation this exception only occurred when I wanted to add sibling entities that had some siblings as well, the reason for this was that the serializer of the Remoting Service was by default not running in full trust mode so this has to be explicitly configured to allow full trust calls in the server side like so.

Dictionary<string, object> props = new Dictionary<string, object>();
props["port"] = 9998;
BinaryServerFormatterSinkProvider binaryFormatterSinkProvider = new BinaryServerFormatterSinkProvider();
binaryFormatterSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
TcpServerChannel chan = new TcpServerChannel(props, binaryFormatterSinkProvider);
            
ChannelServices.RegisterChannel(chan, false);

RemotingConfiguration.RegisterWellKnownServiceType(
                typeof(Service),
                "Service",
                WellKnownObjectMode.SingleCall);

(This can be done also in app.config)

Others

If you had any hard to solve problems with NHibernate or it's plugins that are hard to find and solve please share them here in comment's or provide some links with solutions as those kinds of problems are the most infuriating ones as they are essentially framework triva, either you know how to solve it or you don't and no creative problem solving knowledge is gained.

No comments:

 
ranktrackr.net