我正在创建 2 个 wcf 服务 - ADService 和 DBService。我正在使用称为 EmployeeDTO 和 CustomerDTO 的 DTO 在端点之间交换数据。我无法将任何服务作为服务引用添加到我的解决方案中的其他项目,当我运行 WCF 主机并尝试访问 ADService.svc 或 DBService.svc 时,我得到以下信息:
    无法序列化类型“DTOs.CustomerDTO”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。如果该类型是一个集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。
和
    无法序列化类型“DTOs.EmployeeDTO”。考虑使用 DataContractAttribute 属性对其进行标记,并使用 DataMemberAttribute 属性标记您想要序列化的所有成员。如果该类型是一个集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。
我的文件如下所示:
类 CustomerDTO
    使用系统;
    使用 System.Collections.Generic;
    使用 System.Linq;
    使用 System.Text;
    命名空间 DTO
    {
        公共类 CustomerDTO
        {
            公共 int 客户 ID;
            公共字符串名称;
            公共字符串姓氏;
            公共串街;
            公共字符串 post_code;
            公共字符串城市;
            公共字符串国家;
            公共字符串personal_code;
            公共字符串电话号码;
            公共字符串组类型;
            公共字符串员工;
            公共 CustomerDTO(int _customerID,字符串 _name,字符串 _surname,字符串 _street,字符串 _post_code,字符串 _city,字符串 _country,字符串 _personal_code,字符串 _phone_number,字符串 _group_type,字符串 _employee)
            {
                this.customerID = _customerID;
                this.name = _name;
                this.surname = _surname;
                this.street = _street;
                this.post_code = _post_code;
                这个.city = _city;
                this.country = _country;
                this.personal_code = _personal_code;
                this.phone_number = _phone_number;
                this.group_type = _group_type;
                this.employee = _employee;
            }
        }
    }
EmployeeDTO 类:
    使用系统;
    使用 System.Collections.Generic;
    使用 System.Linq;
    使用 System.Text;
    命名空间 DTO
    {
        公共类EmployeeDTO
        {
            公共字符串给定名称;
            公共字符串 sn;
            公共字符串电话号码;
            公共字符串 sAMAccountName;
            公共字符串标题;
            公串部;
            公共字符串 distinctName;
            公共字符串 OU;
            公共布尔启用;
            公共EmployeeDTO(字符串_givenName,字符串_sn,字符串_telephoneNumber,字符串_sAMAccountName,字符串_title,字符串_Department,字符串_distinguishedName,字符串_OU,布尔_启用)
            {
                this.Department = _Department;
                this.distinguishedName = _distinguishedName;
                this.givenName = _givenName;
                this.sAMAccountName = _sAMAccountName;
                这个.sn = _sn;
                this.telephoneNumber = _telephoneNumber;
                this.title = _title;
                这个.OU = _OU;
                this.enable = _enable;
            }
        }
    }
网页配置
<?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="Data Source=WIN-D3T41W1E5EB\SQLEXPRESS;Initial Catalog=ki_db;Integrated Security=True;MultipleActiveResultSets=True"" 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
    使用系统;
    使用 System.Collections.Generic;
    使用 System.Linq;
    使用 System.Runtime.Serialization;
    使用 System.ServiceModel;
    使用 System.Text;
    使用 System.Threading.Tasks;
    使用 DTO;
    使用 System.Security.Principal;
    命名空间 WcfHost
    {
        // 注意:您可以使用“重构”菜单上的“重命名”命令在代码和配置文件中同时更改接口名称“IADService”。
        [服务合约]
        公共接口IADService
        {
            [运营合同]
            布尔集连接();
            .
            .
            .
            [运营合同]
            任务 changeAccountStatus(EmployeeDTO _employee);
        }
    }
IDBService.cs 是类比定义的。
我似乎找不到这个错误的原因,特别是我的一个朋友有一个类似的实现并且它可以工作而我的没有。任何帮助将不胜感激。