开发者

"The EntityReference object could not be serialized" when serializing from an ASP.NET Web Site, but works OK on an ASP.NET Web Application

开发者 https://www.devze.com 2022-12-23 22:13 出处:网络
I have an Entity Framework data model.Part of the model is a Customer entity.The web service provides a method to get a cu开发者_JAVA技巧stomer, and to receive an updated version of this customer to b

I have an Entity Framework data model. Part of the model is a Customer entity. The web service provides a method to get a cu开发者_JAVA技巧stomer, and to receive an updated version of this customer to be persisted.

To test this, I created a new ASP.NET web Application, (Solution > Add > New Project > ASP.NET Web Application), then added a reference to my service reference using the standard Add Service Reference dialog. I then call the service using the following code:

var client = new CustomerServiceClient();
var customer = client.GetCustomerByID(18); // get it
customer.LimitDown = 100; // change it
client.SaveCustomer(customer); // persist it

Everything works as expected.

Now, I do exactly the same thing, but this time with as ASP.NET Web Site (Solution > Add > New Web Site > ASP.NET Web Site). I add the reference in a similar fashion and copy and paste the code above into the new site. But now I get the following exception thrown on the 4th line:

System.InvalidOperationException The EntityReference object could not be serialized. This type of object cannot be serialized when the RelationshipManager belongs to an entity object that does not implement IEntityWithRelationships.

Source Error:

Line 2474: Line 2475:

public SkyWalkerCustomerService.OperationResult SaveCustomer(SkyWalkerCustomerService.Customer customer) { Line 2476:

return base.Channel.SaveCustomer(customer); Line 2477: } Line 2478: }

Stack Trace:

System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +9475203

System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +345

SkyWalkerCustomerService.ICustomerService.SaveCustomer(Customer customer) +0

SkyWalkerCustomerService.CustomerServiceClient.SaveCustomer(Customer customer) in c:\Windows\Microsoft.NET\Framework\v4.0.30128\Temporary ASP.NET Files\testsite2\dd2bcf8d\f95604ff\App_WebReferences.fz4h7x7l.0.cs:2476 _Default.Page_Load(Object sender, EventArgs e) in c:\Users\Mike\Documents\Repositories\UWC\SkyWalker\TestSite2\Default.aspx.cs:17 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35

System.Web.UI.Control.OnLoad(EventArgs e) +91

System.Web.UI.Control.LoadRecursive() +61 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1966

Googling for this error returns very little. Why doesn't this work?

I guess one answer would be to use a web application instead of a web site, but I have an large existing site to which I need to add the service reference, and I'd rather not convert it.


Cause

The old version Entity Framework was using EntityObject, EntityReference, etc. WCF cannot serialize if related entities are returned together.

Say, if the WCF method returns a customer with the purchased items, it will fail.

Fix

When we add service reference to the WCF endpoint that uses old Entity Framework, in Data Type, do not select Reuse types in all referenced assemblies. But choose Reuse types in specified referenced assemblies and make sure System.Data.Entity is not selected.

"The EntityReference object could not be serialized" when serializing from an ASP.NET Web Site, but works OK on an ASP.NET Web Application


Temporary workaround - I changed the .NET Framework version of the web site to 3.5, and now it works correctly. This is fine for my project, but I'd like to know for future reference why the original problem occurred.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号