我使用Wcf Service
和JS
。
我通过AJAX
向我的服务发送信息。
我在服务中有一个接受以下对象的函数:
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public List<BusinessService> Services { get; set; }
}
BusinessService.cs:
[DataContract]
public class BusinessService
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public int LongTime { get; set; }
}
我将JS中的对象解析为JSON
并将它们发送给服务。
这里发送给wcf函数的json:
"{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}"
除Services
列表外,一切正常。
列表将wcf函数作为null。
我不知道为什么会这样,我会很感激帮助吗?
答案 0 :(得分:4)
您无法从JSON数据中实例化List<T>
。你可以做的是替换:
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public List<BusinessService> Services { get; set; }
}
与
[DataContract]
public class Business
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Desc { get; set; }
[DataMember]
public BusinessService[] Services { get; set; }
}
即,将List<T>
替换为Array of T
答案 1 :(得分:0)
您可以使用WCF Service Trace Viewer来帮助您查看和跟踪从服务中获得的错误。
答案 2 :(得分:0)
试试这个,
var data = "{"ManageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}";
在下面的ajax调用中添加
contentType: "application/json",
data: JSON.stringify({ business: data }),
以下代码接收数据
public ActionResult BusinessCall(Business business){}
答案 3 :(得分:0)
以下我可以说明确有效。我刚试过它。对不起它在vb.net,但你应该明白了。
在服务器端: 在IService1.vb中
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Namespace Webservices
<ServiceContract()>
Public Interface IService1
<OperationContract()> _
<WebInvoke(method:="POST", ResponseFormat:=WebMessageFormat.Json, BodyStyle:=WebMessageBodyStyle.WrappedRequest)> _
Function test(manageBusiness As ManagedBusiness) As string
End Interface
End Namespace
在Service1.svc.vb
中Imports System.Runtime.Serialization
Imports System.ServiceModel.Activation
Namespace Webservices
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)>
Public Class Service1
Implements IService1
Public function test(manageBusiness As ManagedBusiness) As string Implements IService1.test
Return "done"
End function
End Class
<CollectionDataContract()>
Public Class Services
Inherits List(Of Service)
End Class
<DataContract()>
Public Class Service
<DataMember()>Public Property Name As String
<DataMember()>Public Property Desc As String
<DataMember()>Public Property LongTime As Integer
End Class
<DataContract()>
Public Class ManagedBusiness
<DataMember()>Public Property Name As String
<DataMember()>Public Property Desc As String
<DataMember()>Public Property Services As Services
End Class
End NameSpace
我连接了webconfig设置。并将此jquery ajax调用放在客户端:
$("#btnTest").click(function () {
var services = [];
services.push({
Name: "aaaaaa",
Desc: "aaaaaa",
LongTime: 30
});
services.push({
Name: "aaaaaa1",
Desc: "aaaaaa",
LongTime: 30
});
var data = {};
data.manageBusiness = {
Name: "aaaaaa",
Desc: "aaaaaaaaaaaa",
Services: services
}
$.ajax({
type: "POST",
url: "http://localhost:64110/Webservices/Service1.svc/test",
dataType: "json",
contentType: "application/json; charset=utf-8",
async: true,
data: JSON.stringify(data),
success(msg) {
}
});
});
我可以确认请求的有效负载如下:
{"manageBusiness":{"Name":"aaaaaa","Desc":"aaaaaaaaaaaa","Services":[{"Name":"aaaaaa","Desc":"aaaaaa","LongTime":30},{"Name":"aaaaaa1","Desc":"aaaaaa","LongTime":30}]}}
WCF服务对所有内容进行了反序列化。我认为你缺少的是 [CollectionDataContract] 属性。
更新:我尝试删除[CollectionDataContract]属性,但它仍然有效。所以我猜测数据契约属性仅用于序列化,反序列化不需要(?)