I am getting this error and I am unable to figure out why:
System.Xml.XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Stack Trace:
[XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.]
System.Web.Http.WebHost.HttpControllerHandler.EndProcessRequest(IAsyncResult result) +27904
System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +10
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8971341
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
This is the controller which I made request against:
public class CategoryController : ApiController {
public IQueryable<Category> Get() {
var dbContext = new ListItCheckItContext();
return dbContext.Categories.AsQueryable();
}
}
I got the same thing too. Trying to return IEnumerable list of POCO generated by EF Poco T4. Couldn't find any documentation about this error until this post popped up.
You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Description:
An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'. Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.]
System.Web.Http.WebHost.HttpControllerHandler.EndProcessRequest(IAsyncResult result) +27554
System.Web.Http.WebHost.HttpControllerHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +10
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184
I ran into this a minute ago and the issue was the virtual keyword on a collection within my class, like so:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Thing> Things { get; set; }
}
Get the rid of the virtual and everything is good. Without seeing how the model is built up, can't so for sure if this is your problem, but take look. Side note, you don't need to call AsQueryable() in your Get() method. Returning dbContext.Categories will
work just fine.
Ok I solved my particular issue. My generic repository was built up from ObjectContext instead of the context created by the entity framework designer. As soon as I specified the exact context it all started working. Not sure this is cool though.
Code was:
publicabstractclassBaseRepository<T>
: Repository<T>
where
T : class
{
protectedObjectSet<T>
_objectSet;
protectedObjectContext
_objectContext;
public
BaseRepository(String
stringconn)
{
this._objectContext =
newObjectContext(stringconn);
//changed here to specific context
One way to solve this problem is to let the serializer to know of the subclasses of the base classes. For example, if you declare the class Fruit with its "known" subclasses, it should work out well:
[System.Runtime.Serialization.KnownType(typeof(Apple))]
public class Fruit {
// ...
}
public class Apple : Fruit {
// ...
}
We're working to solve this problem in the RC release of the framework.
and the MetaData<Cotntroller> can even recognize and serialize the Subclass (Apple) 's metadata too.
Just one more question, in the RC, is it possible to serialize even those sub-classes that installed in another package? for example, after my application shipped, and installing "plugins" that extends the Fruit type, will it be able to recognize?
The same error raised with another reason, this time, on the EntityFramework Code First.
I have a one-to-many model defined, so, in the parent model, it is something like this:
public class Parent{
public virtual ICollection<Child> Children;
}
If I return the IQueryable<Parent>, the "You must write an attribute...." is coming up again, while if I return the IQueryable<Child> it is fine. If I removed the
virtual, then parent can be returned, but EF cannot fill in the children (i.e., return [] for children property). Is that problem with the WebAPI as well?
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Feb 23, 2012 08:18 PM|LINK
I am getting this error and I am unable to figure out why:
This is the controller which I made request against:
public class CategoryController : ApiController { public IQueryable<Category> Get() { var dbContext = new ListItCheckItContext(); return dbContext.Categories.AsQueryable(); } }tweets as @tourismgeek
patterncoder
Member
4 Points
2 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 24, 2012 12:04 AM|LINK
I got the same thing too. Trying to return IEnumerable list of POCO generated by EF Poco T4. Couldn't find any documentation about this error until this post popped up.
You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Xml.XmlException: You must write an attribute 'type'='object' after writing the attribute with local name '__type'.
Source Error:
Stack Trace:
Jesse Willia...
Member
2 Points
1 Post
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 24, 2012 02:16 AM|LINK
I ran into this a minute ago and the issue was the virtual keyword on a collection within my class, like so:
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Thing> Things { get; set; }
}
Get the rid of the virtual and everything is good. Without seeing how the model is built up, can't so for sure if this is your problem, but take look. Side note, you don't need to call AsQueryable() in your Get() method. Returning dbContext.Categories will work just fine.
patterncoder
Member
4 Points
2 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 24, 2012 02:32 AM|LINK
Ok I solved my particular issue. My generic repository was built up from ObjectContext instead of the context created by the entity framework designer. As soon as I specified the exact context it all started working. Not sure this is cool though.
Code was:
public abstract class BaseRepository<T> : Repository<T> where T : class
{
protected ObjectSet<T> _objectSet;
protected ObjectContext _objectContext;
public BaseRepository(String stringconn)
{
this._objectContext = new ObjectContext(stringconn); //changed here to specific context
_objectSet =this._objectContext.CreateObjectSet<T>();
}
benaw
Member
211 Points
95 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 24, 2012 11:53 AM|LINK
or specify
context.Configuration.ProxyCreationEnabled = false;tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 25, 2012 09:54 PM|LINK
didn't work.
tweets as @tourismgeek
gueei_com
Member
6 Points
3 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 26, 2012 05:54 AM|LINK
I ran into this problem as well, and found to be releated to the subclass of the return types, suppose my Models are:
class Fruit
class Apple : Fruit
In my Db, if I put DbContext.Fruits.add(new Apple), and then I get it from DataController, that error message arise,
but if I put DbContext.Fruits.add(new Fruit), then no problem. Do I need any speical config with the subclassing in the data model?
CarlosFiguei...
Member
99 Points
19 Posts
Microsoft
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 26, 2012 10:17 PM|LINK
One way to solve this problem is to let the serializer to know of the subclasses of the base classes. For example, if you declare the class Fruit with its "known" subclasses, it should work out well:
[System.Runtime.Serialization.KnownType(typeof(Apple))] public class Fruit { // ... } public class Apple : Fruit { // ... }We're working to solve this problem in the RC release of the framework.
gueei_com
Member
6 Points
3 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Feb 28, 2012 02:07 AM|LINK
Thanks, this works perfectly
and the MetaData<Cotntroller> can even recognize and serialize the Subclass (Apple) 's metadata too.
Just one more question, in the RC, is it possible to serialize even those sub-classes that installed in another package? for example, after my application shipped, and installing "plugins" that extends the Fruit type, will it be able to recognize?
gueei_com
Member
6 Points
3 Posts
Re: You must write an attribute 'type'='object' after writing the attribute with local name '__ty...
Mar 02, 2012 05:13 AM|LINK
I am so frustrated..
The same error raised with another reason, this time, on the EntityFramework Code First.
I have a one-to-many model defined, so, in the parent model, it is something like this:
public class Parent{
public virtual ICollection<Child> Children;
}
If I return the IQueryable<Parent>, the "You must write an attribute...." is coming up again, while if I return the IQueryable<Child> it is fine. If I removed the virtual, then parent can be returned, but EF cannot fill in the children (i.e., return [] for children property). Is that problem with the WebAPI as well?