DataContractSerializer serialzing the same object more than once per request - wcf

I'm in the process of writing a WCF application which will be consumed by a Silverlight application. I have done most of the design work and I am now doing the implementation, which has made me come up with this question.
Here's an example of something that exists in my application:
[DataContract]
class Person
{
[DataMember]
private Towel mostRecentlyUsedTowel;
[DataMember]
private Gym gym; //the gym that this person attends
...
}
[DataContract]
class Gym
{
[DataMember]
private List<Towel> towels; //all the towels this gym owns
...
}
Here's what I'm getting at: In my application mostRecentlyUsedTowel will be pointing at something in the towels list for the person's gym. Some of my requests will serialize a Person object.
Is the DataContractSerializer smart enough to notice its being asked to serialize the exact same instance of an object twice? If so, how does it deal with it?
If it will just go about serializing the same instance twice, how should I deal with this so I'm not sending unnecessary data over the link?

The following code:
[TestMethod]
public void CanSerializePerson()
{
var towel1 = new Towel() { Id = 1 };
var towel2 = new Towel() { Id = 2 };
var towel3 = new Towel() { Id = 3 };
var gym = new Gym();
gym.towels.Add(towel1);
gym.towels.Add(towel2);
gym.towels.Add(towel3);
var person = new Person()
{
recentlyUsedTowel = towel1,
gym = gym
};
var sb = new StringBuilder();
using (var writer = XmlWriter.Create(sb))
{
var ser = new DataContractSerializer(typeof (Person));
ser.WriteObject(writer, person);
}
throw new Exception(sb.ToString());
}
public class Person
{
public Towel recentlyUsedTowel { get; set; }
public Gym gym { get; set; }
}
public class Gym
{
public Gym()
{
towels = new List<Towel>();
}
public List<Towel> towels { get; set; }
}
public class Towel
{
public int Id { get; set; }
}
will evaluate to:
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
<gym>
<towels>
<Towel><Id>1</Id></Towel>
<Towel><Id>2</Id></Towel>
<Towel><Id>3</Id></Towel>
</towels>
</gym>
<recentlyUsedTowel><Id>1</Id></recentlyUsedTowel>
</Person>
If you added the IsReference property to the DataContract attribute of the Towel class like this:
[DataContract(IsReference=true)]
public class Towel
{
// you have to specify a [DataMember] in this because you are
// explicitly adding DataContract
[DataMember]
public int Id { get; set; }
}
you would get an output like this:
<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org">
<gym>
<towels>
<Towel z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Id>1</Id>
</Towel>
<Towel z:Id="i2" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Id>2</Id>
</Towel>
<Towel z:Id="i3" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
<Id>3</Id>
</Towel>
</towels>
</gym>
<recentlyUsedTowel z:Ref="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/" />
</Person>
Hope this Helps.

Related

why is .net core 5 webapi xml serializer different that swaggers serializer?

I enable the xml serializer in my .net core 5 web api:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddXmlSerializerFormatters()
//.AddXmlDataContractSerializerFormatters()
.AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNamingPolicy = null;
o.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = $"Routing Evaluator Package Demo Service ({appSettings.Environment})", Version = "v1" });
});
}
My controller takes a parameter
[HttpPost]
public ActionResult<TestResponse> Post(TestRequest request)
{
...
}
And my model looks like this:
[Serializable]
//[DataContract]
public class TestRequest
{
/// <summary>
/// The message addressing information
/// </summary>
//[DataMember]
[XmlArray("Items")]
[XmlArrayItem("Item")]
public Items Items { get; set; }
}
[Serializable]
[XmlRoot("Items")]
//[CollectionDataContract(Name = "Items")]
public class Items : List<Item>
{
}
[Serializable]
//[DataContract]
[XmlRoot("Item")]
public class Item
{
public string key {get;set;}
public string value {get;set;}
}
If I manually serialize an instance of my object, I get results like this:
Object Instance:
var request = new TestRequest()
{
Items = new Items()
{
new Item() {key = "A", value="1"},
new Item() {key = "B", value="2"},
new Item() {key = "C", value="3"}
}
};
Serialization:
using (var str = new MemoryStream())
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(DemoService.ServiceModels.EvaluationRequest));
ser.Serialize(str, request);
str.Flush();
str.Position = 0;
_output.WriteLine(str.ReadAsString());
}
Results:
<?xml version="1.0" encoding="utf-8"?>
<TestRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
<Item>
<key>A</key>
<value>1</value>
</Item>
<Item>
<key>B</key>
<value>2</value>
</Item>
<Item>
<key>C</key>
<value>3</value>
</Item>
</Items>
</TestRequest>
If I browse to the swagger page, the example it shows is:
<?xml version="1.0" encoding="UTF-8"?>
<TestRequest>
<Items>
<key>A</key>
<value>1</value>
</Items>
</TestRequest>
The sample it supplied is incorrect. It appears that the serializer that swagger is using is not the same as the serializer that the web api is using as specfied by AddXmlSerializerFormatters. Do I need to configure swagger somehow to make them match?
Your model is wrong, it should be like below
[Serializable]
public class TestRequest
{
/// <summary>
/// The message addressing information
/// </summary>
//[DataMember]
[XmlArray("Items")]
[XmlArrayItem("Item")]
public List<Item> Items { get; set; }
}
[Serializable]
public class Item
{
public string key {get;set;}
public string value {get;set;}
}

How to get rid of prefix _x003C_ in soap serialization?

I perform serialization to soap envelop using this:
[Serializable]
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person("Tom", 29);
Person person2 = new Person("Bill", 25);
Person[] people = new Person[] { person, person2 };
SoapFormatter formatter = new SoapFormatter();
using (FileStream fs = new FileStream("people.soap", FileMode.OpenOrCreate))
{
formatter.Serialize(fs, people);
Console.WriteLine("Serialized");
}
using (FileStream fs = new FileStream("people.soap", FileMode.OpenOrCreate))
{
Person[] newPeople = (Person[])formatter.Deserialize(fs);
Console.WriteLine("Deserialized");
foreach (Person p in newPeople)
{
Console.WriteLine("Name: {0} --- Age: {1}", p.Name, p.Age);
}
}
Console.ReadLine();
}
}
I get serialized soap as below:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENC:Array SOAP-ENC:arrayType="a1:Person[2]" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/Serialization%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<item href="#ref-3"/>
<item href="#ref-4"/>
</SOAP-ENC:Array>
<a1:Person id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/Serialization%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_Name_x003E_k__BackingField id="ref-5">Tom</_x003C_Name_x003E_k__BackingField>
<_x003C_Age_x003E_k__BackingField>29</_x003C_Age_x003E_k__BackingField>
</a1:Person>
<a1:Person id="ref-4" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/Serialization%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<_x003C_Name_x003E_k__BackingField id="ref-6">Bill</_x003C_Name_x003E_k__BackingField>
<_x003C_Age_x003E_k__BackingField>25</_x003C_Age_x003E_k__BackingField>
</a1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
How can I get rid of prefixes "x003C" and suffixes "_x003E_k__BackingField"
in my fields "Name" and "Age"?
Thank you very much for your answers
you don't need to use [Serializable] attribute for Person class. remove it and test it again.
You almost certainly don't need it unless you are actually using BinaryFormatter or SoapFormatter.
for more info and knowing reason:
https://stackoverflow.com/a/31498951/7979199

Return Entity Framework objects over WCF

We have a problem concerning Entity Framework objects and sending them through WCF.
We have a database, and Entity Framework created classes from that database, a 'Wallet' class in this particular situation.
We try to transfer a Wallet using this code:
public Wallet getWallet()
{
Wallet w = new Wallet();
w.name = "myname";
w.walletID = 123;
return w;
}
We need to transfer that Wallet class, but it won't work, we always encounter the same exception:
"An error occurred while receiving the HTTP response to localhost:8860/ComplementaryCoins.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details."
We searched on the internet, and there is a possibility that the error is due to the need of serialization of Entity Framework-objects.
We have absolutely no idea if this could be the case, and if this is the case, how to solve it.
Our DataContract looks like this (very simple):
[DataContract]
public partial class Wallet
{
[DataMember]
public int getwalletID { get { return walletID; } }
[DataMember]
public string getname { get { return name; } }
}
Does anyone ever encountered this problem?
EDIT: Our Entity Framework created class looks like this:
namespace ComplementaryCoins
{
using System;
using System.Collections.Generic;
public partial class Wallet
{
public Wallet()
{
this.Transaction = new HashSet<Transaction>();
this.Transaction1 = new HashSet<Transaction>();
this.User_Wallet = new HashSet<User_Wallet>();
this.Wallet_Item = new HashSet<Wallet_Item>();
}
public int walletID { get; set; }
public string name { get; set; }
public virtual ICollection<Transaction> Transaction { get; set; }
public virtual ICollection<Transaction> Transaction1 { get; set; }
public virtual ICollection<User_Wallet> User_Wallet { get; set; }
public virtual ICollection<Wallet_Item> Wallet_Item { get; set; }
}
}
Thanks for helping us.
I had the same problem some time ago and the solution for this was:
The entity framework was returning a serialized class instead of normal class.
eg. Wallet_asfawfklnaewfklawlfkawlfjlwfejlkef instead of Wallet
To solve that you can add this code:
base.Configuration.ProxyCreationEnabled = false;
in your Context file.
Since the context file is auto generated you can add it in the Context.tt
In the Context.tt file it can be added around lines 55-65:
<#=Accessibility.ForType(container)#> partial class <#=code.Escape(container)#> : DbContext
{
public <#=code.Escape(container)#>()
: base("name=<#=container.Name#>")
{
base.Configuration.ProxyCreationEnabled = false;
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
this.Configuration.LazyLoadingEnabled = false;
<#
Try specifying a setter for the properties, something like this :
[DataContract]
public partial class Wallet
{
[DataMember]
public int getwalletID { get { return walletID; } set { } }
[DataMember]
public string getname { get { return name; } set { } }
}
If it still doesn't work, you may consider creating an intermediate POCO class for this purpose, and use mapper library like AutoMapper or ValueInjecter to transfer the data from the EF objects.
The POCO class should have same properties as your EF class :
[DataContract]
public class WalletDTO
{
[DataMember]
public int walletID { get; set; }
[DataMember]
public string name { get; set; }
}
And modify your method to return this class instead :
public WalletDTO getWallet()
{
Wallet w = new Wallet(); // or get it from db using EF
var dto = new WalletDTO();
//assuming we are using ValueInjecter, this code below will transfer all matched properties from w to dto
dto.InjectFrom(w);
return dto;
}
Are you trying to recieve a IEnumerable<Wallets>? If - yes, please modify your server class that returns the IEnumerable by adding .ToArray() method

How to upload related entities via MVC4 upshot

I get a simple DTO entity A loaded into my upshot viewmodel which is happily viewable via Knockoutjs.
My DTO A contains a List entities. So I can foreach over the elements inside A.
again:
class A
{
someprop;
List<B> childB;
}
Class B
{
somepropB;
}
So far so good. I can iterated over the data with no problem.
But if I change "someprop" inside an instance of A and SaveAll the server will not respond at all.
The updateData controlle method is not even invoked.
If I clear the childB.Clear() before transmitting it to the client, all is fine.
It seems the upshot is not able to update entities with collections?
There is a bit of work to do if you want such a scenario to work. Upshot only turns the parent entities in observable items. So only the javascript representation of class A is a knockout observable, the javascript representation of class B is not. Therefore Upshot is not aware of any changes in associated objects.
The solution is to map the entities manually. To make my life easier, I've used code from my 'DeliveryTracker' sample application in the code snippets below. In my blog article you can see an example of manual mapping: http://bartjolling.blogspot.com/2012/04/building-single-page-apps-with-aspnet.html so my examples below are working on the 'delivery' and 'customer' objects.
The server-side domain model
namespace StackOverflow.q9888839.UploadRelatedEntities.Models
{
public class Customer
{
[Key]
public int CustomerId { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
public virtual ICollection<Delivery> Deliveries { get; set; }
}
public class Delivery
{
[Key]
public int DeliveryId { get; set; }
public string Description { get; set; }
public bool IsDelivered { get; set; }
[IgnoreDataMember] //needed to break cyclic reference
public virtual Customer Customer { get; set; }
public virtual int CustomerId { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Delivery> Deliveries { get; set; }
}
}
The data service controller
The data service controller exposes the data conforming to OData standards on
"http://localhost:[yourport]/api/dataservice/GetCustomers". In order to be able to update both customers and deliveries you need to define an UpdateCustomer AND UpdateDelivery function
namespace StackOverflow.q9888839.UploadRelatedEntities.Controllers
{
public class DataServiceController : DbDataController<AppDbContext>
{
//Service interface for Customer
public IQueryable<Customer> GetCustomers()
{
return DbContext.Customers.Include("Deliveries").OrderBy(x => x.CustomerId);
}
public void InsertCustomer(Customer customer) { InsertEntity(customer); }
public void UpdateCustomer(Customer customer) { UpdateEntity(customer); }
public void DeleteCustomer(Customer customer) { DeleteEntity(customer); }
//Service interface for Deliveries
public void InsertDelivery(Delivery delivery) { InsertEntity(delivery); }
public void UpdateDelivery(Delivery delivery) { UpdateEntity(delivery); }
public void DeleteDelivery(Delivery delivery) { DeleteEntity(delivery); }
}
}
Client-side domain model
Add a new javascript file containing your client-side model. Here I'm explicitly turning every property into an knockout observable. The key for solving your problem is the line inside the constructor of the Customer object where I'm mapping the incoming deliveries into an observable array
/// <reference path="_references.js" />
(function (window, undefined) {
var deliveryTracker = window["deliveryTracker"] = {}; //clear namespace
deliveryTracker.DeliveriesViewModel = function () {
// Private
var self = this;
self.dataSource = upshot.dataSources.Customers;
self.dataSource.refresh();
self.customers = self.dataSource.getEntities();
};
deliveryTracker.Customer = function (data) {
var self = this;
self.CustomerId = ko.observable(data.CustomerId);
self.Name = ko.observable(data.Name);
self.Address = ko.observable(data.Address);
self.Latitude = ko.observable(data.Latitude);
self.Longitude = ko.observable(data.Longitude);
self.Deliveries = ko.observableArray(ko.utils.arrayMap(data.Deliveries, function (item) {
return new deliveryTracker.Delivery(item);
}));
upshot.addEntityProperties(self, "Customer:#StackOverflow.q9888839.UploadRelatedEntities.Models");
};
deliveryTracker.Delivery = function (data) {
var self = this;
self.DeliveryId = ko.observable(data.DeliveryId);
self.CustomerId = ko.observable(data.CustomerId);
self.Customer = ko.observable(data.Customer ? new deliveryTracker.Customer(data.Customer) : null);
self.Description = ko.observable(data.Description);
self.IsDelivered = ko.observable(data.IsDelivered);
upshot.addEntityProperties(self, "Delivery:#StackOverflow.q9888839.UploadRelatedEntities.Models");
};
//Expose deliveryTracker to global
window["deliveryTracker"] = deliveryTracker;
})(window);
The View
In the index.cshtml you initialize Upshot, specify custom client mapping and bind the viewmodel
#(Html.UpshotContext(bufferChanges: false)
.DataSource<StackOverflow.q9888839.UploadRelatedEntities.Controllers.DataServiceController>(x => x.GetCustomers())
.ClientMapping<StackOverflow.q9888839.UploadRelatedEntities.Models.Customer>("deliveryTracker.Customer")
.ClientMapping<StackOverflow.q9888839.UploadRelatedEntities.Models.Delivery>("deliveryTracker.Delivery")
)
<script type="text/javascript">
$(function () {
var model = new deliveryTracker.DeliveriesViewModel();
ko.applyBindings(model);
});
</script>
<section>
<h3>Customers</h3>
<ol data-bind="foreach: customers">
<input data-bind="value: Name" />
<ol data-bind="foreach: Deliveries">
<li>
<input type="checkbox" data-bind="checked: IsDelivered" >
<span data-bind="text: Description" />
</input>
</li>
</ol>
</ol>
</section>
The Results
When navigating to the index page, the list of customers and related deliveries will be loaded asynchronously. All the deliveries are grouped by customer and are pre-fixed with a checkbox that is bound to the 'IsDelivered' property of a delivery. The customer's name is editable too since it's bound to an INPUT element
I don't have enough reputation to post a screenshot so you will have to do without one
When checking or unchecking the IsDelivered checkbox now, Upshot will detect the change and post it to the DataService Controller
[{"Id":"0",
"Operation":2,
"Entity":{
"__type":"Delivery:#StackOverflow.q9888839.UploadRelatedEntities.Models",
"CustomerId":1,
"DeliveryId":1,
"Description":"NanoCircuit Analyzer",
"IsDelivered":true
},
"OriginalEntity":{
"__type":"Delivery:#StackOverflow.q9888839.UploadRelatedEntities.Models",
"CustomerId":1,
"DeliveryId":1,
"Description":"NanoCircuit Analyzer",
"IsDelivered":false
}
}]
When modifying the customer's name, Upshot will submit the changes when the input box loses focus
[{
"Id": "0",
"Operation": 2,
"Entity": {
"__type": "Customer:#StackOverflow.q9888839.UploadRelatedEntities.Models",
"Address": "Address 2",
"CustomerId": 2,
"Latitude": 51.229248,
"Longitude": 4.404831,
"Name": "Richie Rich"
},
"OriginalEntity": {
"__type": "Customer:#StackOverflow.q9888839.UploadRelatedEntities.Models",
"Address": "Address 2",
"CustomerId": 2,
"Latitude": 51.229248,
"Longitude": 4.404831,
"Name": "Rich Feynmann"
}
}]
So if you follow above procedure, Upshot will both update parent and child entities for you.

Using a datamember type of list

i'm new with wcf and i create a DataContract like this :
[DataContract]
public class directoriesUser
{
[DataMember]
public string NameDir { get; set; }
[DataMember]
public directoriesUser Parent { get; set; }
private ObservableCollection<directoriesUser> _userDirs = new ObservableCollection<directoriesUser>();
[DataMember]
public ObservableCollection<directoriesUser> UserDirectories
{
get { return _userDirs; }
set { _userDirs = value; }
}
}
But whe i use this class like above, the service throw an exception. can someone help me ?
public directoriesUser getHomeUser(int user_id)
{
directoriesUser dirhome = new directoriesUser();
// this doesn't works
dirhome.UserDirectories = new ObservableCollection<directoriesUser>{
new directoriesUser { NameDir = "dir1", Parent = dirhome},
new directoriesUser {NameDir = "dir2", Parent = dirhome}
};
// neither this one
dirhome.UserDirectories.Add(new directoriesUser {NameDir = "dir3", Parent = dirhome})
return dirhome;
}
Your directoriesUser class probably causes recursion when wcf serializes the instance. This is basically caused by the Parent property pointing back up the hierarchy.
Here's a more detailed article about this.