I’ve got a WCF-hosted service right now which is self-hosted and defined like this:
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, Method = "PUT", UriTemplate = "/device")]
void updateDeviceLevel(ZDevice device);
The ZDevice class looks like this:
public class ZDevice {
public bool? newPowerState { get; set; }
public int nodeId {get; set;}
}
I have a simple Mac client which consumes the service by using an http post. It posts {"newLevel":27,"nodeId":6} to the \devices url and .NET magically stuffs the values into a ZDevice object for me. All is well here.
Now however, I need to add some basic security to the mix. I’ve done this by adding a new parameter and some “RequestWrapping” to the method call:
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest, Method = "PUT", UriTemplate = "/device")]
void updateDeviceLevel(string password, ZDevice device);
What I’m trying to do now is figure out what syntax the server is expecting from the consuming clients. I’d hoped that posting in {"password":"somepwd", "newLevel":27,"nodeId":6} would work, but .NET is no longer able to “deserialize” that into the ZDevice object like it did before.
Anyone got some suggestions for me?
Thanks
It should look like this:
Each property on the JSON object has a value; and in the case of
deviceit’s just a new object.Note that in your
ZDeviceclass you called itnewPowerState, but in JSON you are calling itnewLevel. In your class it’s also a bool, but in JSON you are assigning it an int. Something isn’t matching up.Based on your C#, I’d expect it to look like this:
The property names in your JSON object should match the parameter / property names in C#.