WCF Rest client and Transfer Encoding Chunked: Is it supported? - wcf

I have a datacontract as defined below:
[DataContract(Namespace="",Name="community")]
public class Community {
[DataMember(Name="id")]
public int Id{get; set;}
[DataMember(Name="name")]
public string Name { get; set; }
[DataMember(Name="description")]
public string Description { get; set; }
}
and the service contract goes like this:
[OperationContract]
[WebGet(
BodyStyle = WebMessageBodyStyle.Bare,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "{id}"
)]
Community GetCommunity(string id);
When I make a rest call to the host, I get data but only Id and Name properties are populated. The Description property is null! I am creating the channel by inheriting from ClientBase.
Does anybody know why WCF serializes Id and Name but not Description? The Transfer Encoding is set to 'Chunked' on the response from the host and I would like to know if that has anything to do with it ?

I found out that some of the properties were not getting serialized because the response xml had the elements in a different order. The solution was to explicitly set serialization order on the datacontract. Here is the datacontract after I added order attribute:
[DataContract(Namespace="",Name="community")]
public class Community
{
[DataMember(Name = "name",Order=2)]
public string Name { get; set; }
[DataMember(Name="id",Order = 1)]
public int Id{get; set;}
[DataMember(Name="description",Order=3)]
public string Description { get; set; }
}

Related

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

What JSON Should I be Using For WCF Service

I am running a WCF service with these methods,
public string UploadInspections(Inspection[] inspections)
public string UploadInspection(Inspection inspection)
[DataContract]
public partial class Inspection
{
[DataMember]
public DateTime DateTime { get; set; }
[DataMember]
public int Id { get; set; }
[DataMember]
public string Comment { get; set; }
[DataMember]
public int Rating { get; set; }
}
From javascript I tried to call a POST on these methods using JSON. The JSON I had for the UploadInspection method was this,
{"Id":10,"Comment":"New One","Rating":3}
The UploadInspection method was called, but the inspection object was set to null.
I wasn't sure how to specify the Date field using JSON, and I thought that perhaps the parser didn't like JSON with no Date field. I removed the Date field from the Inspection object, but the same thing happened.
Also what should the JSON look like for the UploadInspections method which is an array? I had some JSON that I tried,
"inspections": [{"Id":10,"Comment":"New One","Rating":3}]
And also this,
[{"Id":10,"Comment":"New One","Rating":3}, {"Id":11,"Comment":"New Two","Rating":2}]
But I was getting this error,
OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'string'.
The problem is not what I thought it was, in my service definition it initially looked like this,
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
string UploadInspections(Inspection[] inspections);
Once I removed this,
BodyStyle = WebMessageBodyStyle.WrappedRequest
It worked!

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.....

How to get json response of a custom object using wcf REST services?

How can I serialize an object to return a custom type?
//The response is null.
http://localhost:50604/GameService/Getbyid?id=1
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public MyClass GetById(int id)
[DataContract]
[KnownType(typeof(User))]
public partial class MyClass
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public int? CreatedBy { get; set; }
[DataMember]
public virtual User CreatedByUser { get; set; } //How will I serialize this?
}
You are missing UriTemplate for your operation so your Id is probably never passed in and your method works with default value = 0.
Try this:
[WebGet(UriTemplate="Getbyid?id={id}", ResponseFormat = WebMessageFormat.Json)]
public MyClass GetById(int id)
CreatedByUser will be serialized automatically if filled and if User is data contract as well.

Send large JSON data to WCF Rest Service

I have a client web page that is sending a large json object to a proxy service on the same domain as the web page.
The proxy (an ashx handler) then forwards the request to a WCF Rest Service. Using a WebClient object (standard .net object for making a http request)
The JSON successfully arrives at the proxy via a jQuery POST on the client webpage.
However, when the proxy forwards this to the WCF service I get a Bad Request - Error 400
This doesn't happen when the size of the json data is small
The WCF service contract looks like this
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
CarConfiguration CreateConfiguration(CarConfiguration configuration);
And the DataContract like this
[DataContract(Namespace = "")]
public class CarConfiguration
{
[DataMember(Order = 1)]
public int CarConfigurationId { get; set; }
[DataMember(Order = 2)]
public int UserId { get; set; }
[DataMember(Order = 3)]
public string Model { get; set; }
[DataMember(Order = 4)]
public string Colour { get; set; }
[DataMember(Order = 5)]
public string Trim { get; set; }
[DataMember(Order = 6)]
public string ThumbnailByteData { get; set; }
[DataMember(Order = 6)]
public string Wheel { get; set; }
[DataMember(Order = 7)]
public DateTime Date { get; set; }
[DataMember(Order = 8)]
public List<string> Accessories { get; set; }
[DataMember(Order = 9)]
public string Vehicle { get; set; }
[DataMember(Order = 10)]
public Decimal Price { get; set; }
}
When the ThumbnailByteData field is small, all is OK. When it is large I get the 400 error
What are my options here?
I've tried increasing the MaxBytesRecived config setting but that is not enough
Any ideas?
I would also tweak maxStringContentLength and maxBytesPerRead. Also make sure that whatever binding configuration you set up, you're actually using it in all the right places.
This thread captures pretty well all the things that can go wrong when you are dealing with a large string in WCF message input, so I'd follow the guidance here: Sending large strings to a WCF web service