如何在WCF服务中设置一个类作为参数的方法?

时间:2013-09-24 13:23:46

标签: wcf wcf-binding wcf-security

我正在尝试创建一个以类作为参数的方法。但这是错误的投掷。经过一番搜索,我找到了implementation of QueryStringConverter。 我试图这样做,但我对它没有多少了解。

在我的服务类中,方法是:

[WebInvoke(UriTemplate="LogInForMobileWithDeviceNo", Method="POST", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped)]
string LogInForMobileWithDeviceNo(clsUserDeviceInfo userDeviceInfo);

clsUserDeviceInfo 类中,我将属性声明为:

    [DataContract]
    public class clsUserDeviceInfo
    {
        [DataMember]
        public string UserID{get;set;}
        [DataMember]
        public string DeviceName{get;set;}
        [DataMember]
        public string CordovaVersion{get;set;}
        [DataMember]
        public string DevicePlatformJs{get;set;}
        [DataMember]
        public string DeviceUID{get;set;}
        [DataMember]
        public string DeviceModel { get; set; }
        [DataMember]
        public string DeviceVersion { get; set; }
    }

但它不起作用。

使用Jquery我做了Ajax发布:

 var DeviceName = "samsung";
        var CordovaVersion = "2.1.1.1";
        var DevicePlatformJs = "windows 8";
        var DeviceUID = "23dswd-234dff-23-2334nhj";
        var DeviceModel = "grand duos";
        var DeviceVersion = "3.2";
         var DataArr = {DeviceName:DeviceName,CordovaVersion:CordovaVersion, DevicePlatformJs:DevicePlatformJs,DeviceUID:DeviceUID,DeviceModel:DeviceModel,DeviceVersion:DeviceVersion};
         $.ajax({
            type: "GET",               
            url: serverurl,
        data: JSON.stringify(DataArr),
            success: function (result) {
                alert(result);
            },
            accept: 'application/json'
        });

我做错了吗?

1 个答案:

答案 0 :(得分:0)

你有一个OperationContract,这应该有效!

     [OperationContract]
      [WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/LogInForMobileWithDeviceNo?userDeviceInfo={userDeviceInfo}")]
string LogInForMobileWithDeviceNo(clsUserDeviceInfo userDeviceInfo);