I'm trying to post to my WCF Service from an iPhone app. I have the app posting to the service and trying to send data in the form of a service data contract in XML like below:
NSData *myPostData = [[NSString stringWithFormat:@"<AddMediaItem xmlns='http://www.example.com'><Item xmlns:a='http://www.example.com/MediaItem'><a:MediaType>iPhone</a:MediaType><a:Description>Description</a:Description><a:Name>Test</a:Name><a:ImageType>JPEG</a:ImageType></Item></AddMediaItem>"] dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *myMutablePostData = [NSMutableData dataWithData:myPostData];
[request setPostBody:myMutablePostData];
[request setRequestMethod:@"POST"];
[request a开发者_如何转开发ddRequestHeader:@"Content-Type" value:@"application/xml"];
[request setDidFinishSelector:@selector(uploadFinished:)];
[request setDidFailSelector:@selector(uploadFailed:)];
[request setDelegate:self];
[request startAsynchronous];
For testing purposes, I've setup my service to return the data contract that I send to the service. However, when it returns, some of the values seem to be NULLs although from the code above, I am giving them values.
What also seems strange is that the Name
data member always has a value, but the other 3 I'm sending return NULL or with the correct values based on the order they appear in the XML I post to the service.
Below is the code for my service contract:
[ServiceContract(Namespace = "http://www.example.com")]
public interface IImageDiaryService
{
[OperationContract]
[WebInvoke(UriTemplate = "AddMediaItem", Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
MediaItem AddMediaItem(MediaItem Item);
}
Below is my code for my MediaItem data contract:
[DataContract(Namespace = "http://www.example.com/MediaItem")]
public class MediaItem
{
[DataMember]
public Int32 Id { get; set; }
[DataMember]
public String Name { get; set; }
[DataMember]
public String Description { get; set; }
[DataMember]
public String ImageData { get; set; }
[DataMember]
public String ImageType { get; set; }
[DataMember]
public String MediaType { get; set; }
}
Do let me know if you need any further code.
Any help is much appreciated.
The order is important in data contracts - by default the data members are ordered alphabetically, but you can override it by using the Order property of [DataMember]. Try reordering the fields and you'll get the values populated correctly
<AddMediaItem xmlns='http://www.example.com'>
<Item xmlns:a='http://www.example.com/MediaItem'>
<a:Description>Description</a:Description>
<a:ImageType>JPEG</a:ImageType>
<a:MediaType>iPhone</a:MediaType>
<a:Name>Test</a:Name>
</Item>
</AddMediaItem>
The DataContractSerializer only deserializes in the known order. As carlosfigueira says, default is alphabetical but you can specify an order. Yours aren't alphabetical and so I would expect that you are having server-side serialization issues.
I'd suggest you log what's happening server side. Is the request deserializing correctly inside your service? This will tell you where the data is going missing.
精彩评论