如何传递一个对象参数WCF服务? [英] How to pass an object parameter to a WCF service?

查看:180
本文介绍了如何传递一个对象参数WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此错误:

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'.

我想一个参数传递给我的WCF服务,但该服务甚至没有显示。

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.'); }
    });

要调用服务,它与:收到1字符串参数的服务工作过。 我怎么能接受这个对象?

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

推荐答案

现在的问题是,你的登录函数标记与属性的 WebGet [WebGet(ResponseFormat = WebMessageFormat.Json) 。而应该声明的方法, WebInvoke

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默认情况下使用是无法转换您的复杂类型QueryStringConverter类。有一个办法得到这个为你工作,如果你真的需要使用WebGet,检查出的讨论<一href="http://stackoverflow.com/questions/6783264/passing-a-class-as-parameter-in-restful-wcf-service">here对于你将如何实现一个很好的解释。

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.

看看这篇文章 WebGet的VS解释WebInvoke 。基础知识是WebGet应使用HTTP GET和WebInvoke应与其他动词如POST使用。

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.

这篇关于如何传递一个对象参数WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆