2012-12-10 20 views
3

我正在創建2個wcf服務 - ADService & DBService。我使用稱爲EmployeeDTO和CustomerDTO的DTO來在端點之間交換數據。 我不能在我的解決方案中添加任何服務,服務引用到其他項目的,當我運行WCF主機和嘗試訪問ADService.svc或DBService.svc我得到如下:類型'MyDTO'無法序列化 - Wcf服務

 

    Type 'DTOs.CustomerDTO' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. 

 

    Type 'DTOs.EmployeeDTO' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. 

我的文件看起來如下:

類CustomerDTO

 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 

    namespace DTOs 
    { 
     public class CustomerDTO 
     { 
      public int customerID; 
      public string name; 
      public string surname; 
      public string street; 
      public string post_code; 
      public string city; 
      public string country; 
      public string personal_code; 
      public string phone_number; 
      public string group_type; 
      public string employee; 

      public CustomerDTO(int _customerID, string _name, string _surname, string _street, string _post_code, string _city, string _country, string _personal_code, string _phone_number, string _group_type, string _employee) 
      { 
       this.customerID = _customerID; 
       this.name = _name; 
       this.surname = _surname; 
       this.street = _street; 
       this.post_code = _post_code; 
       this.city = _city; 
       this.country = _country; 
       this.personal_code = _personal_code; 
       this.phone_number = _phone_number; 
       this.group_type = _group_type; 
       this.employee = _employee; 
      } 
     } 
    } 

類EmployeeDTO:

 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 

    namespace DTOs 
    { 
     public class EmployeeDTO 
     { 
      public string givenName; 
      public string sn; 
      public string telephoneNumber; 
      public string sAMAccountName; 
      public string title; 
      public string Department; 
      public string distinguishedName; 
      public string OU; 
      public bool enable; 

      public EmployeeDTO(string _givenName, string _sn, string _telephoneNumber, string _sAMAccountName, string _title, string _Department, string _distinguishedName, string _OU, bool _enable) 
      { 
       this.Department = _Department; 
       this.distinguishedName = _distinguishedName; 
       this.givenName = _givenName; 
       this.sAMAccountName = _sAMAccountName; 
       this.sn = _sn; 
       this.telephoneNumber = _telephoneNumber; 
       this.title = _title; 
       this.OU = _OU; 
       this.enable = _enable; 
      } 
     } 
    } 

的Web.config

<?xml version="1.0"?> 
<configuration> 
    <connectionStrings> 
    <add name="ki_dbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=WIN-D3T41W1E5EB\SQLEXPRESS;Initial Catalog=ki_db;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/> 
    </connectionStrings> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="WcfHost.ADService"> 
     <endpoint address="" binding="basicHttpBinding" contract="WcfHost.IADService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/WcfServiceLibrary/ADService/" /> 
      </baseAddresses> 
     </host> 
     </service> 
     <service name="WcfHost.DBService"> 
     <endpoint address="" binding="basicHttpBinding" contract="WcfHost.IDBService"> 
      <identity> 
      <dns value="localhost" /> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8733/WcfServiceLibrary/DBService/" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

IADService.cs

 

    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Runtime.Serialization; 
    using System.ServiceModel; 
    using System.Text; 
    using System.Threading.Tasks; 
    using DTOs; 
    using System.Security.Principal; 

    namespace WcfHost 
    { 
     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IADService" in both code and config file together. 
     [ServiceContract] 
     public interface IADService 
     { 
      [OperationContract] 
      bool setConnection(); 
      . 
      . 
      . 
      [OperationContract] 
      Task changeAccountStatus(EmployeeDTO _employee); 

     } 
    } 

IDBService.cs模擬方式定義。

我似乎無法找到這個錯誤的原因,特別是我的一個朋友有一個類似的實現,它的工作,而我的沒有。 任何幫助將深表謝意。

+1

你想序列化與'DataMemberAttribute'屬性你考慮使用'DataContractAttribute'屬性標記它,標誌着其所有成員? – Matthew

回答

8

要在您需要使用DataContract和DataMember屬性來標記它(因爲它說)線序列化的類。我相信你也需要將這些字段轉換爲屬性。因此,你需要:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace DTOs 
{ 
    [DataContract] 
    public class CustomerDTO 
    { 
     [DataMember] 
     public int customerID { get; set; } 

     [DataMember] 
     public string name { get; set; } 

     [DataMember] 
     public string surname { get; set; } 

     [DataMember] 
     public string street { get; set; } 

     [DataMember] 
     public string post_code { get; set; } 

     [DataMember] 
     public string city { get; set; } 

     [DataMember] 
     public string country { get; set; } 

     [DataMember] 
     public string personal_code { get; set; } 

     [DataMember] 
     public string phone_number { get; set; } 

     [DataMember] 
     public string group_type { get; set; } 

     [DataMember] 
     public string employee { get; set; } 

     public CustomerDTO(int _customerID, string _name, string _surname, string _street, string _post_code, string _city, string _country, string _personal_code, string _phone_number, string _group_type, string _employee) 
     { 
      this.customerID = _customerID; 
      this.name = _name; 
      this.surname = _surname; 
      this.street = _street; 
      this.post_code = _post_code; 
      this.city = _city; 
      this.country = _country; 
      this.personal_code = _personal_code; 
      this.phone_number = _phone_number; 
      this.group_type = _group_type; 
      this.employee = _employee; 
     } 
    } 
} 

因爲你的操作合同使用EmployeeDTO類型作爲輸入參數,這意味着客戶需要能夠創建該類型並把它通過線路到服務器。所以它必須歸因於[DataContract]

+0

感謝您的明確回答:) –

2

由於在錯誤消息稱,將DataContract屬性的類以上:

[DataContract] 
public class CustomerDTO 
{ 
... 

您還想要放置DataMember屬性上面的任何屬性,你會希望通過服務,使可用,以及作爲OperationContract以上的任何方法你想提供。

多一點信息,請參見本教程: http://www.wcftutorial.net/Data-Contract.aspx

0

我最近有這個錯誤,當我試圖運行我的應用程序並在瀏覽器中顯示WSDL。這種情況很奇怪,因爲黃色WSDL錯誤頁面標識的對象實際上是我的WCF服務。它讓我撓了撓頭。

它結束了,我的錯誤是添加的WCF服務類的ServiceKnownTypeAttribute,一個接口作爲[ServiceKnownType(typeof運算(類名))],但深夜......

所以,這是您可能會收到此錯誤消息的另一個原因。

乾杯

相關問題