Unable to send image file as part Datacontract - wcf

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

Related

Consuming WCF Service with DataContract

I do have a simple WCF service in which If I put the method with simple Data Type then I can access that service in the MVC project which is in same Solution. But if I change the Data Type of the Service method even to array or list of string or any other simple Data Type, I cannot access the service. Do I need to make any config changes.
[DataContract]
public class Property
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string PropertyPost { get; set; }
[DataMember]
public string PropertyType { get; set; }
[DataMember]
public string DealType { get; set; }
[DataMember]
public string Department { get; set; }
[DataMember]
public string ProjectName { get; set; }
}
I actually want to return the List from the WCF service for which I have created the Datacontract, but it is not working even with simple List Type.
Do we need to specify anything in Service like WebInvoke?
Can any one help?

Send a list with appointments through WCF

I would like to send a list of Appointments through WCF. My Interface looks like this:
[ServiceContract]
public interface IServices
{
[OperationContract]
string addAppointments(List<Appointment> appointmentList);
}
If I call my WCF Service I'm always getting the following error:
Type 'Microsoft.Exchange.WebServices.Data.Appointment' cannot be
serialized. Consider marking it with the DataContractAttribute
attribute, and marking all of its members you want serialized with the
DataMemberAttribute attribute. See the Microsoft .NET Framework
documentation for other supported types.
My Service currently looks like this:
class Service : IServices
{
public string addAppointments(List<Appointment> appointmentList)
{
foreach (Appointment app in appointmentList)
{
Console.WriteLine(app.Organizer.Name);
}
return "true";
}
}
It's not your service that's at fault, it's the class your passing, Appointment.
Start by adding [DataContract] to your class. then [DataMember] to each of the properties you'd like to pass.
For example, if you started with:
public class Appointment{
public DateTime Date { get; set; }
public string Name { get; set; }
}
You can make it serializable by WCF's DataContractSerializer by adding those attributes:
[DataContract]
public class Appointment{
[DataMember]
public DateTime Date { get; set; }
[DataMember]
public string Name { get; set; }
}

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

WCF Datacontract, some fields do not deserialize

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"

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

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