最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论