2013-10-03 68 views
5

我使用Wcf ServiceJS將對象內的列表發送到Wcf服務

我通過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函數。

我不知道爲什麼會發生這種情況,我希望得到幫助嗎?

回答

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; } 
    } 

也就是說,Array of T

+0

它也沒有幫助。該列表繼續變爲空。 –

+0

那麼,你的JSON表示形式良好,所以我不明白什麼是錯的,你能發送更多的代碼嗎?例如處理調用的c#代碼和發送請求的javascript代碼? –

+0

它真的有用嗎?我也正在經歷同樣的情況!我需要這個代碼。 –

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){} 
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]屬性,它仍然工作。所以我猜測,數據協定屬性僅用於序列化,而不是反序列化所需的(?)