在 WCF Rest 中发布相关实体

Posted

技术标签:

【中文标题】在 WCF Rest 中发布相关实体【英文标题】:Posting related entities in WCF Rest 【发布时间】:2012-02-25 04:23:18 【问题描述】:

我开发了一个示例 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);

下面一行

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:

我想通过“Order_X_Products”映射表“更新”一个订单以及相关的“产品”。

【问题讨论】:

【参考方案1】:

http://smehrozalam.wordpress.com/2010/10/26/datacontractserializer-working-with-class-inheritence-and-circular-references/ 这里有一个帖子,讲的是在使用 DataContractSerializer 时如何处理循环引用。

【讨论】:

我在我的 Order_X_Products 类上添加了 [DataContract(IsReference=true)] 属性。现在它能够序列化数据并将其发送到 REST 服务。 现在我面临一个新问题,我修改了我的 POCO 模板文件,使其为每个属性添加一个 DataMember 属性,并在部分类中添加了 DataContract(IsReference=true) Order_X_Product 类。现在它会引发以下错误:不期望输入带有数据合同名称“Order”的“Order”。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。我可以在追踪时找到这个

以上是关于在 WCF Rest 中发布相关实体的主要内容,如果未能解决你的问题,请参考以下文章

实体框架不加载相关对象

请求实体太大 WCF REST 服务

WCF实体相关错误:远程终结点不再能识别此序列。这很可能是由于远程终结点上发生中止

WCF 作为业务逻辑

如何将 Json.Net 设置为 WCF REST 服务的默认序列化程序

为 WCF REST 服务生成示例数据?