c# - How to pass an object parameter to a WCF service? - Stack Overflow

I'm having this error:Operation 'Login' in contract 'Medicall' has a query var

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?

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?

Share Improve this question edited Jul 28, 2013 at 21:47 amarruffo asked Jul 28, 2013 at 21:37 amarruffoamarruffo 4182 gold badges5 silver badges15 bronze badges 2
  • 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 Commented Jul 28, 2013 at 21:45
  • See this post stackoverflow./questions/9334643/… – Kambiz Shahim Commented Jul 28, 2013 at 22:30
Add a ment  | 

1 Answer 1

Reset to default 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 plex 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 acplish 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744737053a4590812.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信