Question

I have developed a sample WCF REST service that accepts that creates an "Order" object, the method implementation is as shown below:

[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);
        }
    }

I am trying to consume this service in a client using the "WCF Rest Starter Kit" assemblies. The client side code to consume the service is as below:

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);

The below line

var updateResponse = client.Post("Orders", content);

throws the following error:

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

I have a similar logic to create an order and its working fine.

I also tried by removing the following lines

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});

but still the same error.

Please help me solve this issue.

I have also tried serializing the Order object to XML and changing the RequestFormat of UpdateOrder method to XML. In this case I am getting the following error, if any related entities are populated.

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:

I want to "Update" an order along with the related "Products" through the "Order_X_Products" mapping table.

Was it helpful?

Solution

There is a post here http://smehrozalam.wordpress.com/2010/10/26/datacontractserializer-working-with-class-inheritence-and-circular-references/ that talks about how to deal with circular references when using the DataContractSerializer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top