WCF Datacontract, some fields do not deserialize - wcf

Problem:
I have a WCF service setup to be an endpoint for a call from an external system. The call is sending plain xml. I am testing the system by sending calls into the service from Fiddler using the RequestBuilder.
The issue is that all of my fields are being deserialized with the exception of two fields. price_retail and price_wholesale.
What am I missing? All of the other fields deserialize without an issue - the service responds. It is just these fields.
XML Message:
<widget_conclusion>
<list_criteria_id>123</list_criteria_id>
<list_type>consumer</list_type>
<qty>500</qty>
<price_retail>50.00</price_retail>
<price_wholesale>40.00</price_wholesale>
<session_id>123456789</session_id>
</widget_conclusion>
Service Method:
public string WidgetConclusion(ConclusionMessage message)
{
var priceRetail = message.PriceRetail;
}
Message class:
[DataContract(Name = "widget_conclusion", Namespace = "")]
public class ConclusionMessage
{
[DataMember(Name = "list_criteria_id")]
public int CriteriaId { get; set;}
[DataMember(Name = "list_type")]
public string ListType { get; set; }
[DataMember(Name = "qty")]
public int ListQuantity { get; set; }
[DataMember(Name = "price_retail")]
public decimal PriceRetail { get; set; }
[DataMember(Name = "price_wholesale")]
public decimal PriceWholesale { get; set; }
[DataMember(Name = "session_id")]
public string SessionId { get; set; }
}

Fields are in the wrong order for your message. DataContracts default to Alphabetical ordering and not order of declaration; and expects XML elements to arrive in that order; Out of order elements are discarded usually.
Either fix your contract to specify the right order explicitly (using the Order property of the DataMemberAttribute) or make sure your client sends them in the right one.

You can try to use XmlSerializer instead of DataContractSerializer.
In my case, I need to change default engine in global.asax file:
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
xml.UseXmlSerializer = true;
Do this carefully because some XML can become not valid, for example - namespaces, with XmlSerializer should be determined like:
[XmlNamespaceDeclarations]
private XmlSerializerNamespaces xmlns
{
get {
var xns = new XmlSerializerNamespaces();
xns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");
return xns;
}
set { }
}
Or u can set XmlSerializerFormatAtrribute to You class (not work for me).
Look in url head "Manually Switching to the XmlSerializer"

Related

WCF and .NET 5.0: Issue with byte array deserialization

I'm trying to consume existing WCF services (basic binding) and I'm facing some issues related with the deserialization of the received message. Let me start by showing a snippet of the message:
<s:Body>
<ObtemUtilizadoresResponse xmlns="http://xxx. pt/Mercados"><ObtemUtilizadoresResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<util>
<Id>123</Id>
<ver>AAAAAACL5j4=</ver>
<im>-2</im>
<n>User 123</n>
</util>
....
</s:Body>
Initially, I've start by creating a new DTO for performing the deserialization which looks like this:
[DataContract(Namespace = "http://xxx.pt/Mercados", Name = "util")]
public class Utilizador {
[field: DataMember(Name = "Id")]
public int Id { get; set; }
[field: DataMember(Name = "ver")]
private byte[] Version { get; set; }
[field: DataMember(Name = "n")]
public string Nome { get; set; }
[field: DataMember(Name = "im")]
public int IdMercado { get; set; }
}
Even though the instance is created, it will only fill the Id and Version properties. If I remove the Version property, then the remaining properties are filled. In order to get all the properties filled, I had to move Version to a base class:
[DataContract(Namespace = "http://xxx. pt/Mercados", Name = "vb")]
public class Base {
[field: DataMember(Name = "ver")]
private byte[] Version { get; set; }
}
[DataContract(Namespace = "http://xxx.pt/Mercados", Name = "util")]
public class Utilizador:Base {
... //removed Version property
Does anyone know why this is happening?
Thanks.
After some digging, it seems like there's an explanation for this behavior. When you use the DataContractSerializer, the default serialization order inside the type is alphabetical, and if you're using a class hierarchy, the order is top down. If there's a mismatch in the serialization order, the members will be initialized to their default values. That's why everything worked out fine when I introduced the base class (because the service uses a base class for the type being serialized).
So, if you want to flatten the hierarchy on the client side, you'll need to resort to the Order property of the DataMemberAttribute. You can apply the correct order position to each property (so that it mimics what's being serialized on the server) or you can "group" them by giving the same value to the properties that belong to the each level of the hierarchy (and rely on the the default alphabetical order for the properties that have the same order value - which, btw, is base on the Name property, if you're also setting it):
[DataContract(Namespace = "http://xxx.pt/Mercados", Name = "util")]
public class Utilizador {
[field: DataMember(Name = "Id", Order = 1)]
public int Id { get; set; }
[field: DataMember(Name = "ver", Order = 1)]
private byte[] Version { get; set; }
[field: DataMember(Name = "n", Order = 2)]
public string Nome { get; set; }
[field: DataMember(Name = "im", Order = 2)]
public int IdMercado { get; set; }
}

Unable to send image file as part Datacontract

I am developing a Wcf Restful Service which contains data contract "User" shown below
[DataContract]
public class User
{
public User()
{
}
[DataMember(Name = "Name")]
public string Name { get; set; }
[DataMember(Name = "Mobile")]
public string Mobile { get; set; }
[DataMember(Name = "Email")]
public string Email { get; set; }
[DataMember(Name = "IsImageUpdated")]
public bool IsImageUpdated { get; set; }
}
Now i would like to add one mode data member of type Image,When i try to add Image with type Stream it showing exception
[DataMember(Name = "Iamge")]
public Stream Image { get; set; }
"The InnerException message was 'Type 'System.IO.FileStream' with data contract name 'FileStream:http://schemas.datacontract.org/2004/07/System.IO' 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.'. Please see InnerException for more details."
The service i am developing having many Data contract's,I read some posts which saying the issue can be resolved by changing the Datacontract to message contract,Does a service supports different contract types(like Data,Message).
i need a solution.
This is not possible when using a WebHttpBinding.
Combining streamed and buffered Content is only possible when the binding has a SOAP message Format and you use MessageContract instead of DataContract.
Using a byte[] or returning the stream directly is supported.
[DataMember(Name = "Iamge")]
public byte[] Image { get; set; }
or
[OperationContract]
[WebGet(UriTemplate = "/Image")]
Stream GetImage();
or when using NetTcpBinding, WsHttpBinding, BasicHttpBinding, ...
[MessageContract]
public class ImageData
{
[MessageBodyMember]
public Stream Image { get; set; }
[MessageHeader]
public string Name { get; set; }
}

this[propertyName] is not a function in breeze.debug.js

I am using Hot towel template and extended functionality of it by using breeze. I have used breeze.partial-entities.js file to conver breeze entities to proper dtos that can be used by knockout observables as shown below.
function dtoToEntityMapper(dto) {
var keyValue = dto[keyName];
var entity = manager.getEntityByKey(entityName, keyValue);
if (!entity) {
// We don't have it, so create it as a partial
extendWith = $.extend({ }, extendWith || defaultExtension);
extendWith[keyName] = keyValue;
entity = manager.createEntity(entityName, extendWith);
}
mapToEntity(entity, dto);
entity.entityAspect.setUnchanged();
return entity;
}
For few of the entities it is working properly and getting breeze data converted to entities but for one of the entity implementation is failing. Model for the same is given as below.
public class StandardResourceProperty
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public int StandardResourceId{ get; set; }
public int InputTypeId{ get; set; }
public int ListGroupId{ get; set; }
public string Format{ get; set; }
public string Calculation{ get; set; }
public bool Required{ get; set; }
public int MinSize{ get; set; }
public int MaxSize{ get; set; }
public string DefaultValue{ get; set; }
public string Comment { get; set; }
public virtual StandardResource AssociatedStandardResource { get; set; }
public virtual List AssociatedList { get; set; }
}
The error i am getting is
TypeError: this[propertyName] is not a function
[Break On This Error]
thispropertyName;
breeze.debug.js (line 13157)
]
with code
proto.setProperty = function(propertyName, value) {
this[propertyName](value);
// allow set property chaining.
return this;
};
Please let me know . What can be possible issue with the implementation also , it would be great if i can get more suggestion on how to debug and trace such issues.
Let's back up. I do not understand what you mean by "convert breeze entities to proper dtos that can be used by knockout observables". Breeze entities are already configured as KO observables (assuming you are using the default Breeze model library configuration). What are you trying to do?
I suspect you are following along with the Code Camper Jumpstart course where it does a getSessionPartials projection query. That query (like all projections) returns DTOs - not entities - and maps them with the dtoToEntityMapper method into Session entities.
The CCJS dtoToEntityMapper method cannot be used with entities. It is for converting from a DTO to an Entity and takes DTOs - not entities - as input.
Goodbye to dtoEntityMapper
The dtoToEntityMapper method pre-dates the ability of Breeze to automate projection-to-entity mapping by adding .toType('StandardResourceProperty') to your projection query.
Here is what the CCJS getSessionPartials query could look like now:
var query = EntityQuery
.from('Sessions')
.select('id, title, code, speakerId, trackId, timeSlotId, roomId, level, tags')
.orderBy(orderBy.session)
.toType('Session');
If you go this way, be sure to set the default state of the isPartial flag to true in the custom constructor (see model.js)
metadataStore.registerEntityTypeCtor(
'Session', function () { this.isPartial = true; }, sessionInitializer);
Note that this.isPartial = true is the reverse of the CCJS example where the default was false.
Make sure that you set isPartial(false) when you query or create a full entity. In CCJS there are two places to do that: in the success-callback of getSessionById AND in createSession which would become:
var createSession = function () {
return manager.createEntity(entityNames.session, {isPartial: false});
};

How to setup a DataContract to match the complex XML input in WCF 4.0 REST Service

I have a XML structure like this:
<Message>
<Messagehead>
<OSType>Android</OSType>
<RouteDest>SiteServerName</RouteDest>
<ActionType>Enroll</ActionType>
</Messagehead>
<MessageBody>
<Raw>
<![CDATA[OrienginalMessageContent]]>
</Raw>
</MessageBody>
</Message>
and I want upload this XML to WCF 4.0 my rest service:
public string Enroll(Message instance)
{
// TODO: Add the new instance of SampleItem to the collection
return "success";
}
the Message is a DataContract type, I setup it like below:
[DataContract(Namespace = "")]
public class Message
{
[DataMember]
public MessageHead MessageHead { get; set; }
[DataMember]
public MessageBody MessageBody { get; set; }
}
public class MessageHead
{
public OSType OSType { get; set; }
public string RouteDest { get; set; }
public Action Action { get; set; }
}
public class MessageBody
{
public string RawRequestContent { get; set; }
}
but when I get the Message instance from the server side, all the property is null, except the OSType, can anybody tell me why? How could I solve this problem?
Besides being a really bad name for a class (since it's already used in the WCF runtime), your Message class also has some flaws:
<Message>
<Messagehead>
....
</Messagehead>
Your <Messagehead> has a lower-case h in the middle - yet your class defines it to be upper case:
[DataContract(Namespace = "")]
public class Message
{
[DataMember]
public MessageHead MessageHead { get; set; }
This will not work - case is important and relevant in a WCF message! If your XML has a lower-case h, so must your DataContract class!
Your XML also requires a <Raw> tag inside your <MessageBody>
<MessageBody>
<Raw>
<![CDATA[OriginalMessageContent]]>
</Raw>
</MessageBody>
yet your data contract doesn't respect that:
public class MessageBody
{
public string RawRequestContent { get; set; }
}
Again - those don't line up! Names are important - and they must match between your XML representation of the message, and the C# class representing that message.....

Solve a circular reference in WCF from my POCOs

I have a couple of classes (for now) and I'm trying to clear up a circular reference between the two since it is killing WCF's serialization.
I am using EF with POCOs in a WCF REST service is that helps. I have simplified my problem down to bare bones for an easy example here:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Groups
{
[WebGet(UriTemplate = "")]
public Message GetCollection()
{
var message = new Message { Body = "Test message" };
var group = new Group { Title = "Title of group" };
message.Group = group;
group.Messages = new List<Message> { message };
return message;
}
}
public class Message
{
public string Body { get; set; }
public Group Group { get; set; }
}
[DataContract(IsReference = true)]
public class Group
{
public string Title { get; set; }
public ICollection<Message> Messages { get; set; }
}
I have added the [DataContract(IsReference = true)] to the Group class so that the circular reference is cleaned up however my returned results end up like this:
<Message xmlns="http://schemas.datacontract.org/2004/07/LmApi" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Body>Test message</Body>
<Group z:Id="i1" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"/>
</Message>
Where are the properties of the Group and how can I get them?
BritishDeveloper,
There are no properties associated with Group. That's why all you see is the ID of 1.
The reason is that as soon as you annotate the Group class with [DataContract(IsReference = true)], you are telling the DataContract serializer that it's no longer a POCO type. It's a DataContract type.
So, to serialize Group with properties, you now need to go ahead and annotate the Title and Message properties with DataMemberAttribute.
An alternative would be use the "preserveObjectReferences", which you can pass as a parameter to DataContractSerializer, DataContractSerializerOperationBehavior, and other classes.
Hope this helps!
I decided to make my own smaller classes that have a constructor that takes an entity and sets all of this lighterweight properties correctly.
Basically it is a very small copy of the class that has just the properties needed in the payload. (Obviously I have excluded the problem navigation properties)
This is similar to Circular References and WCF Here is my answer modified for this case
I had the same problem and resolved it by excluding the navigation property back to the parent from the DataContract
[DataContract]
public partial class Message
{
[DataMember]
public virtual string Body { get; set; }
[DataMember]
public virtual Group Group { get; set; }
}
[DataContract]
public partial class Group
{
[DataMember]
public virtual string Title { get; set; }
public virtual ICollection<Message> Messages {get; set;}
}