Reputation: 2358
I have a simple windows serviced based WCF REST client (WebServiceHost() based). I can receive data from a GET operation and I get a call to the right method on a PUT operation, however the passed json is not getting de-serialized into a .NET object. I always get null for the passed object into my object.
The receiving method takes two parameters, one is a key from the uri and the second is to be the received json data. The key comes into my method correctly.
I have logging turned on but there aren't any de-serialization errors. Fiddler is happy, it receives a 200 response. I don't see any funny stuff looking at what's going over the wire.
I've been looking at the other posts that revolved around this issue and from what I can see I have things set up correctly. Obviously that isn't true but I cannot see what's missing / wrong. This would be a lot easier if there were just a slight bit more diagnostic messages coming out. I have config file debugging set to verbose but I don't see much around the actual message de-serialization process.
Is there a way to tap into the actual de-serialization process to debug further?
Or is there something wrong with my setup detailed below?
Service contract, The Send()
method has the two parameters, a key
and a message
object:
[ServiceContract]
public interface ILoader
{
[OperationContract]
[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "Send/{key}")]
string Send(string key, MessageBodyWrapper message);
}
I have the data contract setup for the message wrapper object:
[DataContract(Name = "MessageWrapper")]
public class MessageBodyWrapper
{
private string _messageBody;
private string _state;
public MessageBodyWrapper()
{
_messageBody = "un-initialized";
}
[DataMember(Name="Message")]
public string Message
{
get { return _messageBody; }
set { _messageBody = value; }
}
public override string ToString()
{
JavaScriptSerializer js = new JavaScriptSerializer(); // Available in System.Web.Script.Serialization;
return js.Serialize(this);
}
}
The interface
public class Loader : ILoader
{
private string _state;
public Loader()
{
_state = "initialzied";
}
public string Send(string key, MessageBodyWrapper Message)
{
string ret = "Send here -- Message";
return (ret);
}
}
I'm using fiddler to send out the sample PUT
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8080
Content-Length: 48
{"Message":"sdsdfsdf"}
Upvotes: 1
Views: 1375
Reputation: 4569
I think JSON is in incorrect format. Try this:
Change the BodyStyle
in your OperationContract to BodyStyle = WebMessageBodyStyle.Bare
Try adding following when request is made:
Content-Type: application/json;charset=utf-8
If still it doesn't work, then try Overriding ToString()
method in your MessageBodyWrapper
class.
public override string ToString()
{
JavaScriptSerializer js = new JavaScriptSerializer(); // Available in System.Web.Script.Serialization;
return js.Serialize(this);
}
Now make an object of MessageBodyWrapper
and call ToString()
method and here you can see your required JSON format.
MessageBodyWrapper mbw = new MessageBodyWrapper();
string json = mbw.ToString(); // Hit break point here and "json" variable will contain required JSON format.
After you get required JSON format, then try sending JSON in that format.
Hope this will help! Thanks
Upvotes: 2