I have a c# web service with the following definition:
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "BCryptLogin")]
LoginResponse BCryptLogin(LoginData logindata);
And the actual function is
public LoginResponse BCryptLogin(LoginData logindata)
{
string login = logindata.login;
string password = logindata.password;
// code be here
}
When trying to access the “logindata.login” I get a NullReferenceException:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=LoginService
StackTrace:
at LoginService.LoginService.BCryptLogin(LoginData logindata) in C:\services\LoginService\LoginService\LoginService.svc.cs:line 74
at SyncInvokeBCryptLogin(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
This is my first attempt at POST web service and I am fairly clueless :S What is going wrong?
My testing request looks like this:
{"login":"username","password":"password"}
(also tried)
{"LoginData":{"login":"username","password":"password"}}
And the LoginData object is defined like this:
[DataContract]
public class LoginData
{
[DataMember]
public string login { get; set; }
[DataMember]
public string password { get; set; }
}
Your function is expecting an object, but your passing two values.
You need to create an object in JavaScript and pass it to your function.
Then pass this object to your method in your JSON request, using the following syntax.
NOTE: The parameter name on your method “logindata” must be the same in your JSON call, it’s case sensitive.
I suggest you use JSON.org lib to convert your object into a valid JSON request.