4

I'm having this error:

Operation 'Login' in contract 'Medicall' has a query variable named 'objLogin' of type      'Medicall_WCF.Medicall+clsLogin', but type 'Medicall_WCF.Medicall+clsLogin' is not convertible by 'QueryStringConverter'.  Variables for UriTemplate query values must have types that can be converted by 'QueryStringConverter'.

I'm trying to pass a parameter to my WCF service, but the service isn't even showing.

#region Methods
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json)]
    public Int32 Login(clsLogin objLogin)
    {
        try
        {
            // TODO: Database query.
            if (objLogin.username == "" & objLogin.password == "")
                return 1;
            else
                return 0;
        }
        catch (Exception e)
        {
            // TODO: Handle exception error codes.
            return -1;
        }
    }

    #endregion
    #region Classes
    [DataContract(), KnownType(typeof(clsLogin))]
    public class clsLogin
    {
        public string username;
        public string password;
    }
    #endregion

I'm using this:

$.ajax({
        url: "PATH_TO_SERVICE",
        dataType: "jsonp",
        type: 'post',
        data: { 'objLogin': null },
        crossDomain: true,
        success: function (data) {
            // TODO: Say hi to the user.
            // TODO: Make the menu visible.
            // TODO: Go to the home page.
            alert(JSON.stringify(data));
        },
        failure: function (data) { app.showNotification('Lo sentimos, ha ocurrido un error.'); }
    });

To call the service, it worked before with a service that recieved 1 string parameter. How can I recieve this object?

amarruffo
  • 418
  • 2
  • 5
  • 15
  • Yes, I had a similar service working but it only recieved 1 string parameter, for this one I need it to recieve an object. Here's the syntax I used to call it (edited the first post). – amarruffo Jul 28 '13 at 21:45
  • See this post http://stackoverflow.com/questions/9334643/wcf-rest-webget-for-user-defined-parameters – Kambiz Shahim Jul 28 '13 at 22:30

1 Answers1

3

The problem is that your Login function is marked with the attribute WebGet [WebGet(ResponseFormat = WebMessageFormat.Json)]. You should instead declare your method as WebInvoke:

[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
public Int32 Login(clsLogin objLogin)

WebGet by default uses a QueryStringConverter class which is unable to convert your complex type. There is a way to get this to work for you if you really need to use WebGet, check out the discussion here for a good explanation of how you would accomplish that.

Take a look at this article for an explanation of WebGet vs WebInvoke. The basics is WebGet should be used with HTTP GET and WebInvoke should be used with other verbs like POST.

Community
  • 1
  • 1
p e p
  • 6,593
  • 2
  • 23
  • 32
  • Hello I changed my code and now it works with a POST call. The problem now is that in my $.ajax call I'm using type: "POST" but it gets ignored and it uses "GET" I think that because of the "dataType: jsonp". I need that because its a cross-domain call. So now the problem is how to send an object parameter when using JSONP (GET). – amarruffo Jul 28 '13 at 23:00
  • I did not notice that you are using jsonp. You're right, jsonp calls can only use POST. Two suggestions: 1) Look at the link I provided and extend QueryStringConverter so that you can pass complex types using WebGet or 2) instead of using the clsLogin object, simply pass in two String parameters for "username" and "password", since the only problem is that you are trying to use a complex type. This would be simple and easy. Either way I think you can forget about WebInvoke since your primary necessity is jsonp due to cross-domain. If this works I can update the answer. – p e p Jul 28 '13 at 23:08
  • Yeah I'm gonna do that, thanks! If you find any other thing that can be done to make this more simple (to use up to 5+ parameters or something like that) it would be nice. – amarruffo Jul 28 '13 at 23:10
  • Yeah, sorry about that, this would be perfect if not for jsonp :) I think if you have lots of parameters and it would make things easier for you, I'd definitely go with extending QueryStringConverter. From the answer I linked, it looks really easy to do, but I've never tried it so I couldn't tell you for sure. – p e p Jul 28 '13 at 23:14