c# - 如何将对象参数传递给 WCF 服务?

标签 c# javascript .net 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 服务,但该服务甚至没有显示。

#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

我正在使用这个:

$.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 个字符串参数的服务。 我怎样才能收到这个对象?

最佳答案

问题是您的 Login 函数标有属性 WebGet [WebGet(ResponseFormat = WebMessageFormat.Json)]。您应该将您的方法声明为 WebInvoke :

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

WebGet 默认使用无法转换复杂类型的 QueryStringConverter 类。如果您真的需要使用 WebGet,有一种方法可以让它为您工作,请查看讨论 here以获得有关如何实现该目标的良好解释。

查看这篇文章以了解 WebGet vs WebInvoke 的解释.基础知识是 WebGet 应该与 HTTP GET 一起使用,WebInvoke 应该与其他动词(如 POST)一起使用。

关于c# - 如何将对象参数传递给 WCF 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17913145/

相关文章:

c# - .NET 4.0 中使用 C# 的 Task.Run 的替代方法是什么?

c# - 如何通过API通过blob zip文件动态部署azure功能

javascript - 设置 Canvas 文本的字体粗细

c# - Azure 服务总线消息是否阻止调用?

javascript - 将数据传递给动态创建的 Angular 模块和组件

javascript - 如何通过事件增加/减少对象属性值?

.net - 获取 DataGridView 中所示的字符串

c# - .NET 中的 "STRONG NAME"是什么?

c# - C#加载二进制文件

c# - 如何在 C# 中取消绑定(bind)套接字?