nhibernate set custom generator class in mapping file - c#

i try to implement a custom generator class like this:
Creating a custom id generator for nHibernate
This example is in fluent Nhibernate and not what I want exactly. I want it for nhibernate with mapping.
I have the following lines of Code:
namespace webportale_ger_webservice.Routinen
{
public class NextKey : TableGenerator
{
private const Int32 SeedValue = 1048576;
public override object Generate(ISessionImplementor sessionimpl, object obj)
{
var session = NHibernateHelper.GetSession();
int counter = Convert.ToInt32(base.Generate(sessionimpl, obj));
return counter + SeedValue + 1;
}
}
}
Now I want to give this generator class the ID-property of the mapping document, like this:
<hibernate-mapping assembly="webportale ger webservice" namespace="webportale_ger_webservice.Model" xmlns="urn:nhibernate-mapping-2.2">
<class name="SPTPL" table="SPTPL" lazy="false" >
<id name="AR" column="AR" generator="webportale_ger_webservice.Routinen.NextKey"/>
But it doesn't work with generator="webportale_ger_webservice.Routinen.NextKey", the error message is the following:
NHibernate.Id.IdentifierGenerationException: Could not interpret id generator strategy: webportale_ger_webservice.Routinen.NextKey
bei NHibernate.Id.IdentifierGeneratorFactory.GetIdentifierGeneratorClass(String strategy, Dialect dialect)
bei NHibernate.Id.IdentifierGeneratorFactory.Create(String strategy, IType type, IDictionary`2 parms, Dialect dialect)
bei NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
bei NHibernate.Cfg.Configuration.BuildSessionFactory()
bei webportale_ger_webservice.DatabaseInterface.NHibernateHelper..cctor() in C:\Quellen\VSWebNeoBackEnd\VSWebNeoBackEnd\VSWebNeoBackEnd\project india webservice\DatabaseInterface\NHibernateHelper.cs:Zeile 34.
--- Ende der internen Ausnahmestapelüberwachung ---
bei webportale_ger_webservice.DatabaseInterface.NHibernateHelper.GetSession()
bei webportale_ger_webservice.india_webservice.InsertSP_Leistungsort(String vornameStr, String nachnameStr, String strasseStr, String hnrzusatzStr, Int32 hausnrStr, String plzStr, String ortStr, String mailStr, String dateStr, String telStr, String argef, String bemerkungStr, String arstrasse, String arplz) in C:\Quellen\VSWebNeoBackEnd\VSWebNeoBackEnd\VSWebNeoBackEnd\project india webservice\webportale_ger_webservice.asmx.cs:Zeile 382.
Does anyone know how to define the class correctly in mapping documents?
thank you.

In case, we provide a type, it is better to use full type name - mostly including the assembly name. So this should work:
<class name="SPTPL" table="SPTPL" lazy="false" >
//<id name="AR" column="AR" generator="webportale_ger_webservice.Routinen.NextKey"/>
<id name="AR" column="AR" generator="webportale_ger_webservice.Routinen.NextKey,webportale_ger_webservice"/>
Expecting that the assembly name is webportale_ger_webservice

Related

XML deserialisation fails with nested classes (inner classes)

Edit
I have a problem at deserialising an XML file, containing inner classes (or nested classes).
I have following class diagram:
[XmlRoot(ElementName = "HM")]
public class OwnClass : BaseClass{
...
public OwnClass(){} // default constructor
...
}
I have the following _xmlMessageFormatter declaration (based on System.Messaging):
this._xmlMessageFormatter = new System.Messaging.XmlMessageFormatter();
System.Type[] OwnTypes = new System.Type[30];
OwnTypes[0] = typeof(Baseclasses.OwnClass); /* TR */
...
this._xmlMessageFormatter.TargetTypes = OwnTypes;
Edit: this is what the XML looks like:
<?xml version="1.0"?>
<HM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>124</ID>
<TC>TR</TC>
</HM>
All of this is working fine.
Now I add a new class inside the definition of OwnClass:
[XmlRoot(ElementName = "HM")]
public class OwnClass : BaseClass {
[XmlElement(ElementName = "INS")]
public ClassInside f_Inside;
...
public OwnClass(){} // default constructor
...
public class ClassInside{
...
public class ClassInside(){}
...} // end of ClassInside
} // end of OwnClass
I've also added the corresponding targettype:
OwnTypes [27] = typeof(BaseClasses.OwnClass.ClassInside); // the number of the array is correct.
Edit: the XML file looks now as follows:
<?xml version="1.0"?>
<HM xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>125</ID>
<TC>TR</TC>
<TR>
<ID>1</ID>
<CD>MOVE</CD>
</TR>
</HM>
The _xmlMessageFormatter cannot handle the deserialisation, as you can see here:
Source code:
Object temp;
temp = this._xmlMessageFormatter.Read(message);
For getting more information, I've typed ? temp = this._xmlMessageFormatter.Read(message); in the immediate window (I'm working with Visual Studio), this is what I get:
'temp = this._xmlMessageFormatter.Read(message)' threw an exception of type 'System.InvalidOperationException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233079
HelpLink: null
InnerException: {"There was an error reflecting field 'f_Inside'."}
Message: "There was an error reflecting type 'BaseClasses.AnotherClass'."
Source: "System.Xml"
StackTrace: " at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)\r\n at
System.Xml.Serialization.XmlReflectionImporter.ImportElement(TypeModel model, XmlRootAttribute root, String defaultNamespace, RecursionLimiter limiter)\r\n at
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type type, XmlRootAttribute root, String defaultNamespace)\r\n at
System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)\r\n at
System.Messaging.XmlMessageFormatter.CreateTargetSerializerTable()\r\n at
System.Messaging.XmlMessageFormatter.Read(Message message)"
TargetSite: {System.Xml.Serialization.TypeMapping ImportTypeMapping(System.Xml.Serialization.TypeModel,
System.String,
ImportContext,
System.String,
System.Xml.Serialization.XmlAttributes,
Boolean,
Boolean,
System.Xml.Serialization.RecursionLimiter)}
I have two issues with the error message:
It mentions f_Inside. This looks correct, but I have used f_Inside as a general fieldname for all my classes, and the reason I mention this:
It mentions AnotherClass while I have send a message of the form OwnClass.
=> I'm having serious doubts about the correctness of the error message. Is there anybody who knows what I can do now (or how the _xmlFormatter works?)
Edit: added background
All of this is part of a messaging service: one application is sending a message, the other one is receiving it (using the System.Messaging.MessageQueue objects). The serialisation/deserialisation is just a part of it.
Thanks in advance
The problem is solved and it had nothing to do with deserialisation of nested classes:
In one of my classes (AgainAnotherClass), I had following source code:
[XmlElement(ElementName = "SA")]
[XmlElement(ElementName = "SA")]
public string SomeAttribute { get; set; }
(a typical case of Copy/Paste)
The fact that I had two lines with XmlElement caused the problem.
The exception looked as follows:
InnerException: {"There was an error reflecting field 'f_Inside'."}
Message: "There was an error reflecting type '<NameSpace>.AgainOtherClass'."
The InnerException made me believe that there was a problem with the nested class, while the Message spoke about a completely other class. I decided to follow the InnerException.
That was wrong! So, in case of C# exceptions where InnerException and Message contradict each other, first check the Message, then (maybe) the InnerException.

NHibernate MySqlException

I am trying use NHibernate to my MySQL but I have still issue with connection on my localhost database.
Exeptions:
MySql.Data.MySqlClient.MySqlException
HResult=0x80004005
Message=Unable to connect to any of the specified MySQL hosts.
Source=MySql.Data
StackTrace:
at MySql.Data.MySqlClient.NativeDriver.Open()
at MySql.Data.MySqlClient.Driver.Open()
at MySql.Data.MySqlClient.Driver.Create(MySqlConnectionStringBuilder settings)
at MySql.Data.MySqlClient.MySqlPool.CreateNewPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.GetPooledConnection()
at MySql.Data.MySqlClient.MySqlPool.TryToGetDriver()
at MySql.Data.MySqlClient.MySqlPool.GetConnection()
at MySql.Data.MySqlClient.MySqlConnection.Open()
at NHibernate.Connection.DriverConnectionProvider.GetConnection()
at NHibernate.Tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.Prepare()
at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.GetReservedWords(Dialect dialect, IConnectionHelper connectionHelper)
at NHibernate.Tool.hbm2ddl.SchemaMetadataUpdater.Update(ISessionFactoryImplementor sessionFactory)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()
at TestE.Model.NHibernateHelper.get_Session() in C:\Users\hajek\source\repos\TestE\TestE\NHibernateHelper.cs:line 24
at TestE.Dao.DaoBase1..ctor() in C:\Users\hajek\source\repos\TestE\TestE\Dao\DaoBase.cs:line 20
at TestE.Dao.ItemDao..ctor() in C:\Users\hajek\source\repos\TestE\TestE\Dao\ItemDao.cs:line 13
at TestE.Program.Main(String[] args) in C:\Users\hajek\source\repos\TestE\TestE\Program.cs:line 16
Inner Exception 1:
WaitHandleCannotBeOpenedException: No handle of the given name exists.
Code:
namespace TestE.Model
{
public class NHibernateHelper
{
private static ISessionFactory factory;
private static MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
public static ISession Session
{
get
{
if (factory == null)
{
Configuration cfg = new Configuration();
factory = cfg.Configure("hibernate.cfg.xml").BuildSessionFactory();
}
return factory.OpenSession();
}
}
}
}
hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="TestE">
<property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.connection_string">
Database=todo_list;Data Source=localhost;User Id=root;Password=root;
Protocol=memory;Old Guids=True;
</property>
<property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
</session-factory>
</hibernate-configuration>
Configuration is located in ~/bin/debug and Nhibernate can see it but still can not connect to database.
----- Sorry for my English Language.------
By default, shared memory is not enabled on Windows. This may be why your connection is failing.
Remove Protocol=memory; from your connection string to use a regular TCP/IP connection.

NHibernate MappingException

I have been interested in learning NHibernate, so i search a course, and i found one in Pluralsight, but when i go to follow the examples i get this exception and i don't know why i get this exception... and it's kind of annoying because where i search i can't find more info about the exception or an up-to-date guide of NHibernate. So my question is:
a) for the exception itself, why it occurs
b) if you can recommend me a site or a course or anything up to date to learn NHibernate.
Thanks in advance.
I leave the code here:
Customer.cs:
namespace NHibernateDemo
{
public class Customer
{
public virtual int Id { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
}
}
Customer.hbm.xml:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:hibernate-mapping-2.2"
assembly="NHibernateDemo"
namespace="NHibernateDemo">
<class name="Customer">
<id name="Id">
<generator class="native"/>
</id>
<property name="FirstName"/>
<property name="LastName"/>
</class>
</hibernate-mapping>
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Driver;
namespace NHibernateDemo
{
class Program
{
static void Main(string[] args)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = "Server=localhost;Database=NHibernateDemo;Integrated Security=SSPI;";
x.Driver<SqlClientDriver>();
x.Dialect<MsSql2008Dialect>();
});
// Here is where i get the MappingException. It says that it can't compile the Customer.hbm.xml file.
cfg.AddAssembly(Assembly.GetExecutingAssembly());
var sessionFactory = cfg.BuildSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var tx = session.BeginTransaction())
{
var customers = session.CreateCriteria<Customer>()
.List<Customer>();
foreach (var customer in customers)
{
Console.WriteLine("{0} {1}", customer.FirstName, customer.LastName);
}
tx.Commit();
}
Console.WriteLine("Press <ENTER> to exit...");
Console.ReadLine();
}
}
}
}
Message of the exception:
"Could not compile the mapping document: NHibernateDemo.Customer.hbm.xml"
StackTrace:
NHibernate.MappingException: Could not compile the mapping document: NHibernateDemo.Customer.hbm.xml ---> System.InvalidOperationException: Error en el documento XML (1, 2). ---> System.InvalidOperationException: No se esperaba <hibernate-mapping xmlns='urn:hibernate-mapping-2.2'>.
en Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderHbmMapping.Read109_hibernatemapping()
--- Fin del seguimiento de la pila de la excepción interna ---
en System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
en System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
en NHibernate.Cfg.NamedXmlDocument..ctor(String name, XmlDocument document, XmlSerializer serializer)
en NHibernate.Cfg.NamedXmlDocument..ctor(String name, XmlDocument document)
en NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)
--- Fin del seguimiento de la pila de la excepción interna ---
en NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
en NHibernate.Cfg.Configuration.LoadMappingDocument(XmlReader hbmReader, String name)
en NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
en NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly)
en NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly)
en NHibernateDemo.Program.Main(String[] args) en d:\Sistema\Documents\Visual Studio 2015\Projects\NHibernateDemo\NHibernateDemo\Program.cs:línea 28
the problem is that it can't find the file to compile it, so :
try manually copying the file into your bin directory ( Debug or Release or .. )
make sure your NHibernateDemo.Customer.hbm.xml properties are Build Action : Embedded resource and Copy to Output Directory : Copy Always
if you still have the problem, try adding : cfg.AddFile("NHibernateDemo.Customer.hbm.xml") ( or the path to your hbm.xml file )
if you still have the problem, then try :
cfg.addFile(AssemblyLocation() + "NHibernateDemo.Customer.hbm.xml"); ( or the path to your hbm.xml file, where AssemblyLocation() is :
private string AssemblyLocation()
{
var codebase = new Uri(Assembly.GetExecutingAssembly().CodeBase);
return Path.GetDirectoryName(codebase.LocalPath);
}
cfg.AddAssembly isn't necessary so you might consider removing it if none of the above works :P
I found (thanks to Fréderic) that the problem was a line in the hbm.xml file. This line was the one that have make the problem occur: <hibernate-mapping xmlns="urn:hibernate-mapping-2.2". I forgot a 'n' in front of the 'hibernate' word and because of that it can't parse the file.
Thanks to Taki and Fréderic for the time.

Binding to Objective C library (VFR Reader)

I'm new to MonoTouch development and I would like to embed some PDF Viewing functionality in my app. I have found several resources for doing that, however, I also see enough remarks about all the additional implementations to make it stable and fast.
I now see that there is a good ObjectiveC library which already implements a lot of functionality (CATiledLayer, multi-threading, page scrolling, thumb nails, device rotation ...): https://github.com/vfr/Reader
The last days, after reading the monotoch binding documentation, I'm trying to bind this in MonoTouch, but without success.
I'm able to export it to a library (.a) file and I've created a binding API.
//#interface ReaderDocument : NSObject <NSObject, NSCoding>
[BaseType (typeof (NSObject))]
interface ReaderDocument {
//- (id)initWithFilePath:(NSString *)fullFilePath password:(NSString *)phrase;
[Export("initWithFilePath:password")]
IntPtr Constructor (string path, string phrase);
//Properties
[Export("guid")]
string Guid { get;}
[Export("fileDate")]
NSDate FileDate { get;}
[Export("lastOpen")]
NSDate LastOpen { get;set;}
[Export("fileSize")]
NSNumber FileSize{ get;}
[Export("pageCount")]
NSNumber PageCount { get;}
[Export("pageNumber")]
NSNumber PageNumber { get;set;}
[Export("bookmarks")]
NSMutableIndexSet Bookmarks { get;}
[Export("fileName")]
string FileName { get;}
[Export("password")]
string Password { get;}
[Export("fileURL")]
NSUrl FileURL { get;}
//Methods
//+ (ReaderDocument *)withDocumentFilePath:(NSString *)filename password:(NSString *)phrase;
[Static, Export("withDocumentFilePath:password")]
ReaderDocument WithDocumentFilePath(string filename, string phrase);
//+ (ReaderDocument *)unarchiveFromFileName:(NSString *)filename password:(NSString *)phrase;
[Static, Export("unarchiveFromFileName:password")]
ReaderDocument UnarchiveFromFileName(string filename, string phrase);
//- (void)saveReaderDocument;
[Export("saveReaderDocument")]
void SaveReaderDocument();
//- (void)updateProperties;
[Export("updateProperties")]
void updateProperties();
}
I'm very unsure about following line btw:
//#interface ReaderDocument : NSObject <NSObject, NSCoding>
[BaseType (typeof (NSObject))]
interface ReaderDocument
Not sure if I have to do something with the ""?
I can now create following code in MonoTouch
ReaderDocument doc = ReaderDocument.withDocumentFilePath("Tamarin.pdf","");
or
ReaderDocument doc = new ReaderDocument("Tamarin.pdf","yrt");
Both are resulting in "unrecognized selector" error
2012-11-04 22:15:05.731 PFDTest1[4149:1507] +[ReaderDocument withDocumentFilePath:password]: unrecognized selector sent to class 0x2f7738
[ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: +[ReaderDocument withDocumentFilePath:password]: unrecognized selector sent to class 0x2f7738
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:IntPtr_objc_msgSend_IntPtr_IntPtr (intptr,intptr,intptr,intptr)
at VFRBinding4.ReaderDocument.withDocumentFilePath (System.String filename, System.String phrase) [0x00000] in <filename unknown>:0
at PFDTest1.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00030] in /Users/matthiasvalcke/Projects/PFDTest1/PFDTest1/AppDelegate.cs:39
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at PFDTest1.Application.Main (System.String[] args) [0x00000] in /Users/matthiasvalcke/Projects/PFDTest1/PFDTest1/Main.cs:17
Any ideas?
There could be other issues but your bindings are wrong for the constructors, i.e.
//- (id)initWithFilePath:(NSString *)fullFilePath password:(NSString *)phrase;
[Export("initWithFilePath:password")]
void InitWithFilePath(string path, string password);
ObjectiveC init* selectors should be binded as C# constructors. E.g.
[Export("initWithFilePath:password")]
IntPtr Constructor (string path, string password);
and that should be what you use to create the instance, e.g.
ReaderDocument doc = new ReaderDocument ("sample.pdf", "");
// ...
I could be entirely wrong, but I think your selectors are wrong:
e.g. "withDocumentFilePath:password" should be "withDocumentFilePath:password:"

Intermittent errors while de-serializing object from XML

I have a program that takes objects stored as XML in a database (basicly a message queue) and de-serializes them. Intermittently, I will get one of the following errors:
System.Runtime.InteropServices.ExternalException: Cannot execute a program. The command being executed was "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /noconfig /fullpaths #"C:\Documents and Settings\useraccount\Local Settings\Temp\lh21vp3m.cmdline".
at System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(SafeUserTokenHandle userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at System.CodeDom.Compiler.Executor.ExecWaitWithCapture(SafeUserTokenHandle userToken, String cmd, String currentDir, TempFileCollection tempFiles, String& outputName, String& errorName, String trueCmdLine)
at Microsoft.CSharp.CSharpCodeGenerator.Compile(CompilerParameters options, String compilerDirectory, String compilerExe, String arguments, String& outputFile, Int32& nativeReturnValue, String trueArgs)
at Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(CompilerParameters options, String[] fileNames)
at Microsoft.CSharp.CSharpCodeGenerator.FromSourceBatch(CompilerParameters options, String[] sources)
at Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromSourceBatch(CompilerParameters options, String[] sources)
at System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromSource(CompilerParameters options, String[] sources)
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
.....
Or I'll get this one:
System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0016: Could not write to output file 'c:\Documents and Settings\useraccount\Local Settings\Temp\nciktsd7.dll' -- 'Could not execute CVTRES.EXE.'
at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
at System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence)
at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
....
The program process thousands of messages a day successfully, but I only get these errors maybe 2 or 3 times a day. They don't appear to be correlated to any specific kind of message, just completely random.
Any idea what causes those errors and how to fix it?
ETA - Here's the code that is causing the errors, in case that helps:
public class MessageContextBuilder<T> where T : MessageContextBase
{
private static IDictionary<string, XmlSerializer> SerializerCache { get; set; }
public ILog Logger { get; set; }
public MessageContextBuilder() {
if (SerializerCache == null) SerializerCache = new Dictionary<string, XmlSerializer>();
Logger = LogContextManager.Context.GetLogger<MessageContextBuilder<T>>();
}
public T BuildContextFromMessage(IEmailQueueMessage msg) {
XmlSerializer serializer = GetSerializer(typeof(T));
XmlReader r = XmlReader.Create(new StringReader(msg.MessageDetails));
if (serializer.CanDeserialize(r)) {
T rval = (T)serializer.Deserialize(r);
rval.EmailAddress = msg.EmailAddress;
rval.LocaleID = msg.LocaleID;
rval.StoreID = msg.StoreID;
rval.MessageID = msg.UniqueKey;
return rval;
} else {
throw new ArgumentException("Cannot deserialize XML in message details for message #" + msg.UniqueKey);
}
}
public XmlSerializer GetSerializer(Type t) {
if (!SerializerCache.ContainsKey(t.FullName)) {
SerializerCache.Add(t.FullName, new XmlSerializer(t)); // Error occurs here, in XmlSerializer constructor, intermittently
}
return SerializerCache[t.FullName];
}
}
You can pre-create serializers: http://msdn.microsoft.com/en-us/library/bk3w6240%28v=VS.100%29.aspx Just give it a try. The next canonical candidate for such problems is your virus scanner. Your tool is writing to disc while creating serializers. I have seen virus scanner producing all kind of strange errors in such situations.
XmlSerializer is supposed to be thread safe.
Even if that's the case, you can notice the behavior you are getting is in both cases failing at: XmlSerializer..ctor(Type type)
Given that, it seriously look like a multi-threading limitation trying to create serializers.
I suggest to take this code you have:
public XmlSerializer GetSerializer(Type t) {
if (!SerializerCache.ContainsKey(t.FullName)) {
SerializerCache.Add(t.FullName, new XmlSerializer(t)); // Error occurs here, intermittently
}
return SerializerCache[t.FullName];
}
And implement a lock on the Add. This way you are only creating 1 serializer at a time. The hit is small if you aren't processing tons of different types.
Note that you need the lock anyway, as the way it is you could get duplicate exceptions when 2 types try to be added at the same time.
static object serializerCacheLock = new object();
public XmlSerializer GetSerializer(Type t) {
if (!SerializerCache.ContainsKey(t.FullName))
lock(serializerCacheLock)
if (!SerializerCache.ContainsKey(t.FullName)) {
SerializerCache.Add(t.FullName, new XmlSerializer(t));
}
return SerializerCache[t.FullName];
}
If the above still isn't enough, I'd try with a read/write lock on serializer constructor vs. serializers usage. Line of thought being that maybe the multi-threading issue is worth than 2 ctors running at the same time.
All above is a Huge guess, but if it were me I'd definitely confirm is not that.
For the first error (cannot execute a program), you might be running into the same XmlSerializer bug that we ran into. It turns out XmlSerlializer throws that exception when Directory.CurrentDirectory is set to a folder that no longer exists.
Our specific situation is different than yours, but I'll give the details in case it helps shed light on what might be happening for you, or it helps anyone else. In our case, a small number of our customers would get that error after launching our WinForms application directly from the installer, i.e. they chose the "run now" option after installing or upgrading. (Unclear why it happened to some but not others). What we suspect is happening is that our installer (InstallAware) occasionally starts our application with the current directory set to a folder that no longer exists, or is about to be deleted. To test this theory, I wrote a test app which simulates launching from the installer:
string dir = #"C:\Users\me\Documents\Temp\WillBeDeleted";
Directory.CreateDirectory(dir);
Directory.SetCurrentDirectory(dir);
Process.Start(#"C:\Program Files (x86)\...\our.exe");
Directory.SetCurrentDirectory(#"C:\"); // otherwise, won't be able to delete
Directory.Delete(dir);
Sure enough, as soon as the launched application created a new instance of XmlSerializer, the exception would be thrown. I put in trace statements to show the result of GetCurrentDirectory(), and indeed it was set to the WillBeDeleted folder. The fix was to SetCurrentDirectory to a valid location during application initialization, before any serialization took place.
This is a sign that you are not caching your serialisers which is not good at all => it leads to memory leak and I suspect you will experience this.
Remember that .NET generates code and compiles them into assemblies every time you create a serialiser.
Always create your serialisers and then cache them.
Here is a sample:
public class SerialiserCache
{
private static readonly SerialiserCache _current = new SerialiserCache();
private Dictionary<Type, XmlSerializer> _cache = new Dictionary<Type, XmlSerializer>();
private SerialiserCache()
{
}
public static SerialiserCache Current
{
get { return _current; }
}
public XmlSerializer this[Type t]
{
get
{
LoadIfNecessary(t);
return _cache[t];
}
}
private void LoadIfNecessary(Type t)
{
// double if to prevent race conditions
if (!_cache.ContainsKey(t))
{
lock (_cache)
{
if (!_cache.ContainsKey(t))
{
_cache[t] = new XmlSerializer(typeof(T));
}
}
}
}
}

Categories

Resources