一个目的是WCF服务中发送清单 [英] Sending a list within an object to Wcf Service

查看:264
本文介绍了一个目的是WCF服务中发送清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 WCF服务 JS

我发信息给我的服务由 AJAX

我在接受下列对象的服务功能:

  [DataContract]
    公共类业务
    {
        [数据成员]
        公共字符串名称{;组; }
        [数据成员]
        公共字符串说明{获得;组; }
        [数据成员]
        公开名单<的BusinessService>服务{获得;组; }
    }
 

在BusinessService.cs:

  [DataContract]
    公共类的BusinessService
    {
        [数据成员]
        公共字符串名称{;组; }
        [数据成员]
        公共字符串说明{获得;组; }
        [数据成员]
        公众诠释长期{获得;组; }
    }
 

我分析我的JS对象,以 JSON ,并将其发送给服务。

这里发送到WCF功能的JSON:

<$p$p><$c$c>"{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}"

一切正常,因为它应该除了服务列表。

清单涉及到WCF功能为空。

我不知道为什么会这样,我就AP preciate帮助?

解决方案

您不能实例化一个名单,其中,从JSON数据; T&GT。你可以做的,就是将:

  [DataContract]
    公共类业务
    {
        [数据成员]
        公共字符串名称{;组; }
        [数据成员]
        公共字符串说明{获得;组; }
        [数据成员]
        公开名单&LT;的BusinessService&GT;服务{获得;组; }
    }
 

  [DataContract]
    公共类业务
    {
        [数据成员]
        公共字符串名称{;组; }
        [数据成员]
        公共字符串说明{获得;组; }
        [数据成员]
        公众的BusinessService [] {服务获得;组; }
    }
 

也就是说,替换名单,其中,T&GT; T的数组

I use Wcf Service and JS.

I send information to my service by AJAX.

I have a function in the service that accepts the following object:

  [DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public List<BusinessService> Services { get; set; }
    }

the BusinessService.cs:

   [DataContract]
    public class BusinessService
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }
        [DataMember]
        public int LongTime { get; set; }
    }

I parse the objects in my JS to JSON and send them to the service.

Here the json that send to the wcf function:

"{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}"

Everything works as it should except the Services list.

The list comes to the wcf function as null.

I have no idea why this is happening, I would appreciate help?

解决方案

You cannot instantiate a List<T> from JSON data. What you can do, is replace :

[DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public List<BusinessService> Services { get; set; }
    }

with

[DataContract]
    public class Business
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Desc { get; set; }        
        [DataMember]
        public BusinessService[] Services { get; set; }
    }

That is, replace List<T> with Array of T

这篇关于一个目的是WCF服务中发送清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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