在WCF Rest中过帐相关实体

在WCF Rest中过帐相关实体,wcf,rest,.net-4.0,wcf-rest,wcf-rest-starter-kit,Wcf,Rest,.net 4.0,Wcf Rest,Wcf Rest Starter Kit,我开发了一个示例WCF REST服务,它接受创建“Order”对象的命令,方法实现如下所示: [Description("Updates an existing order")] [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "/Orders")] [OperationContract] public void UpdateOrder(Order order) {

我开发了一个示例WCF REST服务,它接受创建“Order”对象的命令,方法实现如下所示:

[Description("Updates an existing order")]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "/Orders")]
[OperationContract]
public void UpdateOrder(Order order)
    {
        try
        {
            using (var context = new ProductsDBEntities())
            {
                context.Orders.Attach(order);
                context.ObjectStateManager.ChangeObjectState(order, System.Data.EntityState.Modified);
                context.SaveChanges();
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.OK;
                WebOperationContext.Current.OutgoingResponse.StatusDescription = "Order updated successfully.";
            }
        }
        catch (Exception ex)
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.InternalServerError;
            WebOperationContext.Current.OutgoingResponse.StatusDescription = ex.Message + " " + ((ex.InnerException != null) ? ex.InnerException.Message : string.Empty);
        }
    }
我正在尝试使用“WCF Rest Starter Kit”程序集在客户机中使用此服务。使用该服务的客户端代码如下所示:

var order = new Order(){
              OrderId = Convert.ToInt32(ddlCategories.SelectedItem.Value)
};

order.Order_X_Products.Add(new Order_X_Products { ProductId = 1, Quantity = 10});
order.Order_X_Products.Add(new Order_X_Products { ProductId = 2, Quantity = 10});
var content = HttpContentExtensions.CreateJsonDataContract<Order>(order);
var updateResponse = client.Post("Orders", content);
引发以下错误:

Server Error in '/' Application.

Specified argument was out of the range of valid values.
Parameter name: value

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.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: value
我有一个类似的逻辑来创建一个订单,并且它工作得很好

我还尝试删除以下行

order.Order_X_Products.Add(new Order_X_Products { ProductId = 1, Quantity = 10});
order.Order_X_Products.Add(new Order_X_Products { ProductId = 2, Quantity = 10});
但还是一样的错误

请帮我解决这个问题

我还尝试将Order对象序列化为XML,并将UpdateOrder方法的RequestFormat更改为XML。在本例中,如果填充了任何相关实体,我将得到以下错误

Server Error in '/' Application.

Object graph for type 'WcfRestSample.Models.Common.Order_X_Products' contains cycles and  cannot be serialized if reference tracking is disabled.

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.Runtime.Serialization.SerializationException: Object graph for type 'WcfRestSample.Models.Common.Order_X_Products' contains cycles and cannot be serialized if reference tracking is disabled.

Source Error: 


Line 102:
Line 103: var content = HttpContentExtensions.CreateDataContract<Order>  (order);
Line 104: var updateResponse = client.Post("Orders", content);
Line 105:
Line 106:
“/”应用程序中出现服务器错误。 “WcfRestSample.Models.Common.Order_X_Products”类型的对象图包含循环,如果禁用引用跟踪,则无法序列化。 描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源。 异常详细信息:System.Runtime.Serialization.SerializationException:类型“WcfRestSample.Models.Common.Order_X_Products”的对象图包含周期,如果禁用引用跟踪,则无法序列化。 源错误: 第102行: 第103行:var content=HttpContentExtensions.CreateDataContract(订单); 第104行:var updateResponse=client.Post(“订单”,内容); 第105行: 第106行:
我想通过“order_X_Products”映射表“更新”订单以及相关的“Products”。

这里有一篇文章讨论如何在使用DataContractSerializer时处理循环引用。

我在我的order_X_Products类上添加了[DataContract(IsReference=true)]属性。现在它能够序列化数据并将其发送到REST服务。现在我面临一个新问题,我修改了我的POCO模板文件,使其为每个属性添加DataMember属性,并在一个分部类中为Order_X_Product类添加了DataContract(IsReference=true)。现在它抛出以下错误:不应使用数据协定名为“Order”的类型“Order”。考虑使用DATACONTRORTCORDEVER或将未知类型的任何类型添加到已知类型的列表中,例如,使用NoNyType属性或将它们添加到传递给DATACONTROTTRORIGLASER的已知类型列表中。我可以在追踪的时候找到这个
Server Error in '/' Application.

Object graph for type 'WcfRestSample.Models.Common.Order_X_Products' contains cycles and  cannot be serialized if reference tracking is disabled.

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.Runtime.Serialization.SerializationException: Object graph for type 'WcfRestSample.Models.Common.Order_X_Products' contains cycles and cannot be serialized if reference tracking is disabled.

Source Error: 


Line 102:
Line 103: var content = HttpContentExtensions.CreateDataContract<Order>  (order);
Line 104: var updateResponse = client.Post("Orders", content);
Line 105:
Line 106: