JSON deserialization throws circular reference ONLY in live build - json

I have a strange issue that I can't wrap my head around. I am trying to create an "export to csv" function for my MVC4 application where the relevant JSON is passed via an ajax call to my ActionResult. The ActionResult deserializes the stringify'd JSON (with JSON.Net), writes it to a file in csv format, then returns the server path to the new file. My success callback then receives the path and calls the url to download.
This works fine locally, but on my live test server I get the following exception:
A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.
The JSON (and subsequently the objects they are deserialized to) are slightly complex. They come from a grouped subset of a SlickGrid DataView. I was getting circular reference exceptions when I included the aggregate information for column totals (this is only relevant to those that are versed in SlickGrid, I do not believe the data being passed to the server is an issue), but I've removed them before passing the JSON to the server. Here is my JSON to C# class structure:
[Serializable]
public class Row
{
public int id { get; set; }
public DateTime DateCreated { get; set; }
public int RefNo { get; set; }
public string ClientName { get; set; }
public string Plate { get; set; }
public string Address1 { get; set; }
public int? ProductID { get; set; }
public string Product { get; set; }
public string S1 { get; set; }
public string S2 { get; set; }
}
[Serializable]
public class RootReportObject
{
public bool __group { get; set; }
public int level { get; set; }
public int count { get; set; }
public string value { get; set; }
public string title { get; set; }
public int collapsed { get; set; }
public List<Row> rows { get; set; }
public object groups { get; set; }
public string groupingKey { get; set; }
}
The only thing that I'm thinking is that, because of the way the data is structured, the List<> of rows in the root object may be throwing the circular references during deserializtion because a group does not necessarily have unique row references.
My question is why does it work fine locally?? I have no idea what I'm missing.

That's great that the [ScriptIgnore] attribute is helping. Also, something to be completely sure of is that all of your URL paths, including in your AJAX code, resolve correctly to the application root. When some of these are wrong, this is a notorious source of problems during the move from development to production.
It doesn't sound like it is necessarily the primary issue but I don't have any understanding of your app's design. It's definitely worth looking over.

Related

How to make a POST request with postman?

I have two models, Sever and Update. The ralationship between these two classes is 1...*N, meaning that each Server has multiple updates.
Here is my Server model:
public class Server
{
public Server()
{
UpdateList = new HashSet<Update>();
}
[Key]
[Required]
public int ID { get; set; }
[Required]
public string ComputerName { get; set; }
[Required]
public string Type { get; set; }
[Required]
public string Estado { get; set; }
[Required]
public string Phase { get; set; }
public virtual ICollection<Update> UpdateList { get; set; }
}
Here is my Update model:
public class Update
{
[Required]
public int ID { get; set; }
public string updateId { get; set; }
public string updateTitle { get; set; }
public string updateDescription { get; set; }
[System.ComponentModel.DataAnnotations.Schema.ForeignKey("Server")]
[Display(Name = "Server")]
public int? ServerFK { get; set; }
public virtual Server Server { get; set; }
}
Here is the POST request that I'm using in postman:
{
"computerName":"ServerTestUpdate",
"type":"ServidorAplicacional",
"estado":"Reboot Pending",
"phase":"0",
"updateList":
[{
"updateId":"idTest",
"updateTitle":"Update1",
"updateDescription":"TestDescription"
}]
}
However, when I do this, I get the following error:
System.Text.Json.JsonException: A possible object cycle was detected.
This can either be due to a cycle or if the object depth is larger
than the maximum allowed depth of 32. Consider using
ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.
Is there something wrong with the syntax or maybe is something in the controller?
Thank you in advance
Most likely this is a problem in serializing the object, since deserialization wouldn’t care about cycles. It’s probably the fact that your Server object has a collection of Updates, all of which have a Server property pointing back and this causes a cycle.
If you add JsonIgnore attribute to the property this should go away (from System.Text.Json.Serialization namespace)
[JsonIgnore]
public virtual Server Server { get; set; }

how to pass json data to controller in aspnetcore 2

I have a json data.
Aim: I want to pass json data from view to aspnetcore controller, something like
public IActionResult(PRICES[] things)
{
}
but no success!..
model is:
public class PRICES :IEntity
{
public int ID { get; set; }
public decimal? PRICE { get; set; }
public decimal? PRICE2 { get; set; }
public decimal? PRICE3 { get; set; }
public decimal? PRICE4 { get; set; }
}
JSON data is:
"{"things":[{"ID":1,"PRICE":1.23,"PRICE2":2.34,"PRICE3":3.45,"PRICE4":5.67},{"ID":2,"PRICE":4.53,"PRICE2":6.34,"PRICE3":6.45,"PRICE4":7.67},{"ID":3,"PRICE":100,"PRICE2":0,"PRICE3":0,"PRICE4":0},{"ID":4,"PRICE":200,"PRICE2":3434,"PRICE3":0,"PRICE4":0},{"ID":5,"PRICE":11,"PRICE2":0,"PRICE3":2,"PRICE4":33},{"ID":6,"PRICE":3,"PRICE2":0,"PRICE3":0,"PRICE4":0},{"ID":7,"PRICE":4,"PRICE2":33,"PRICE3":0,"PRICE4":0},{"ID":8,"PRICE":1,"PRICE2":0,"PRICE3":0,"PRICE4":0},{"ID":9,"PRICE":100,"PRICE2":118,"PRICE3":3,"PRICE4":4}]}"
enter image description here
it took 4-5 hours, no success. any response will be preciated.
Your action method accepts an array.PRICES[] things.But you are sending a Json Object.
Remove things part and send only [{},{}]. ie send a json array.
or else refactor the action method to accept an object like
public class Things
{
public IList<PRICES> Things { get; set; }
}
}

Sencha Touch and WCF REST post parameter always null

I'm building an application using sencha touch 1.1 in the client side and WCF REST services in the server side. I'm facing some problems when the sencha sore syncs data, the parameter sent to update method is always null. This is the c# definition of the parameter:
namespace HomeInventory.Application.Service.Dto{
[Serializable]
[DataContract(Namespace = "http://xxxxx.xxx.org/shopinglist")]
public class DtoShopingList{
[DataMember]
public long Id { get; set; }
[DataMember]
public string CreationDate { get; set; }
[DataMember]
public string ListName { get; set; }
[DataMember]
public DtoUser User { get; set; }
[DataMember]
public DtoListItem[] Items { get; set; }
[DataMember]
public bool IsDefault { get; set; }
}
This the json posted by sencha to the server:
{"records":"[{\"Id\":0,\"ListName\":\"Default\",\"CreationDate\":\"23/01/2012\",\"IsDefault\":true,\"items\":[]}]"}
It seems to me that WCF JSON serializer is unabled to parse the JSON to the corresponding class. Any help in how to fix it?
Thanks in advance.
Well, in case it serves someone else, I got rid of the backslashes with the following settings in the proxy.
writer: {
encode: false,
type: 'json'
}
Best regards.

Code First Object not properly instantiating

I have a class department inheriting from activeentity
public class ActiveEntity : Entity, IActive
{
public ActiveEntity()
{
IsActive = true;
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid Id { get; set; }
public bool IsActive { get; set; }
[Timestamp, ScaffoldColumn(false), DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Computed)]
public Byte[] Timestamp { get; set; }
[ScaffoldColumn(false)]
public string CreationUserId { get; set; }
[ScaffoldColumn(false)]
public string LastModifiedUserId { get; set; }
}
public class Department:ActiveEntity
{
public Department()
{
this.Address = new DepartmentAddress();
}
[StringLength(9),MinLength(9),MaxLength(9)]
public string Name { get; set; }
public Guid ManagerId { get; set; }
[UIHint("AjaxDropdown")]
public User Manager { get; set; }
public Guid? AddressId { get; set; }
public DepartmentAddress Address { get; set; }
public ICollection<OverheadRate> OverheadRates { get; set; }
}
I am just using annotations no Fluent API. The data saves to the data Sql Server 2008 just fine however the address object never gets instantiated, even though I have the context use the include
return c.Set<Department>().Include(d => d.Address).Include(d => d.Manager).Where(predicate);
The data is returned I run sql profiler and then run the query it returns the correct data.
Any thoughts or suggestions?
Remove instantiating the address (this.Address = new DepartmentAddress();) in the Department constructor. Instantiating navigation references in the default constructor is evil and has nasty side effects like these:
What would cause the Entity Framework to save an unloaded (but lazy loadable) reference over existing data?
EF 4.1 Code First: Why is EF not setting this navigation property?

OpenRasta Json returning nested classes

Is it possible to return nested classes in json with OpenRasta?
I'm using EF4.1 with code first (which in theory shouldn't make a difference, as they are just POCO classes).
Here is an example:
public class AppUser
{
[Key]
public int AppUserId { get; set; }
public string WinLogin { get; set; }
public string ScreenName { get; set; }
public string AgencyId { get; set; }
public virtual ICollection<UserAppVersion> UserAppVersion { get; set; }
}
public class UserAppVersion
{
[Key]
public int UaVersionId { get; set; }
public int AppUserId { get; set; }
public int AppVersionId { get; set; }
public DateTime DateCreated { get; set; }
public DateTime DateUpdated { get; set; }
public virtual AppUser User { get; set; }
public virtual AppVersion Version { get; set; }
}
I try and return an AppUser record in json using this:
ResourceSpace.Has.ResourcesOfType<AppUser>()
.AtUri("/user").HandledBy<UserHandler>().AsJsonDataContract();
But I get an error:
System.Runtime.Serialization.SerializationException: Type 'System.Data.Entity.DynamicProxies.UserAppVersion_FD8D86F0A3AE39A0C370918637C1A90AD8D3ACA3E149677EA82C0A8D10ED0F8D' with data contract name 'UserAppVersion_FD8D86F0A3AE39A0C370918637C1A90AD8D3ACA3E149677EA82C0A8D10ED0F8D:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
Unfortunately I don't know how to resolve this. Any suggestions?
That's a data contract issue, not an openrast one.
The DataContract serialzier is seeing a dynamic proxy probably generated by EF code first, and when seeing that cannot render the object.
I'd recommend either swapping for another serialziation codec, or disabling transparent lazy loading, or alternatively marking your property as an ignore for serialziation and have another property typed to a List so the serializer can function.
See DataContractSerializer Error using Entity Framework 4.0 with WCF 4.0