2

I hope that someone can help me with this problem that I've been having with XmlSerializer.

I've already looked through this thread: http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/551cee76-fd80-48f8-ac6b-5c22c234fecf/

The error I am getting is:

System.InvalidOperationException: Unable to generate a temporary class (result=1). error CS0012: The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I've made sure that my unit test has a reference to System.Data.Entity, so it is able to compile at least. I've also in the app.config made an assembly binding to System.Data.Entity.

Here's my rough class structure

[Serializable] 
[XmlRoot(Namespace = XmlSupport.MyNamespace, ElementName = XmlSupport.WantToSerialize)] 
[XmlInclude(typeof(WantToSerializeBaseClass)]
[XmlInclude(typeof(EntityObject)]
[XmlInclude(typeof(MyEntityObjectSubClass)]
public class WantToSerialize : WantToSerializeBaseClass, IXmlSerializable  (I've tried putting this on the baseclass and the current class)
{
  // methods and classes 
  // I've included XmlIncludes for all the classes that this class has a reference too
  //  even though in the WriteXml it just uses .NET base classes
}

The WantToSerializeBaseClass makes use of some generics, but I've decorated it with XmlIncludes for (EntityObject, and any other classes it makes reference to as well).

the calling code:

var serializerWrite = new XmlSerializer(typeof (WantToSerialize), XmlSupport.ITNNamespace); 

fails

However if I do:

var serializerWrite = new XmlSerializer(typeof (WantToSerialize), new Type[] {typeof(EntityObject)}); 

it is succesfull.

Any thoughts would be most helpful.

UPDATED I've tracked the problem down to a method in the WantToSerializeBaseClass

public abstract void ConvertFromEntity<TtoCopy>(TtoCopy toCopy) where TtoCopy : MyEntityObjectSubClass;

Where MyEntityObjectSubClass is a subclass of EntityObject, that adds a few methods that I want on my entity objects. The MyEntityObjectSubClass looks like this:

[Serializable]
[XmlInclude(typeof(EntityObject))]
public abstract class MyEntityObjectSubClass : EntityObject, IMyEntityObjectSubClass 

Again any thoughts would be great

John Saunders
  • 160,644
  • 26
  • 247
  • 397
MisterHux
  • 140
  • 12

4 Answers4

0

I realize this is an older question but for posterity's sake set the CopyLocal parameter on the .dll reference to True.

kozmi
  • 25
  • 2
  • 8
0

If you don't have any code that requires a reference at compile time then that reference won't be included in the built assembly. You can use a tool like Reflector to check whether the reference is making it into your assembly.

One thing you can try is adding a static method to WantToSerialize that creates the XmlSerializer. The assembly containing WantToSerialize must already have a good reference to EntityObject, so this should solve the problem.

Dave
  • 4,420
  • 1
  • 34
  • 39
  • I've used reflector to open the DLL and made sure that it has a reference to System.Data.Entity (and its the right version). I don't think that adding a static method that creates XmlSerializer is an option, since I want to eventually send it over the wire using WCF. – MisterHux Feb 12 '09 at 15:34
0

I ended up removing the generic code and it worked fine.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
MisterHux
  • 140
  • 12
0

I have this same problem too (in VB). what I found is that you can use the generic parameter, but it errors because the type MyEntityObjectSubClass is in another assembly. If you remove the type restriction on the generic parameter it will work fine.

I believe this to be an error in the framework itself. I've submitted a feedback ticket to microsoft. I attached a VB.net

Kratz
  • 4,280
  • 3
  • 32
  • 55