服务引用C#上缺少已发布的类

本文关键字:引用 服务 | 更新日期: 2024-07-27 13:38:26

第一次来这里,从来没有必要问任何问题,因为在其他问题中总是很容易找到答案。

我面临的问题:

我刚刚添加了ServiceLayer,它将发布对应于BusinesLogicLayer的服务。

这是我的代码:

using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq; 
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
namespace ServiceLayer
{
[ServiceContract]
public interface IServiceEmployees
{
    [OperationContract]
    void AddEmployee(Employee emp);
    [OperationContract]
    void DeleteEmployee(int id);
    [OperationContract]
    void UpdateEmployee(Employee emp);
    [OperationContract]
    List<Employee> GetAllEmployees();
    [OperationContract]
    Employee GetEmployee(int id);
    [OperationContract]
    double CalcPartTimeEmployeeSalary(int idEmployee, int hours);
}
}

下一个文件:

using BusinessLogicLayer;
using Shared.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace ServiceLayer
{
public class ServiceEmployees : IServiceEmployees
{
    private static IBLEmployees blHandler;
    public ServiceEmployees()
    {
        blHandler = Program.blHandler;
    }
    public void AddEmployee(Employee emp)
    {
        blHandler.AddEmployee(emp);
    }
    public void DeleteEmployee(int id)
    {
        blHandler.DeleteEmployee(id);
    }
    public void UpdateEmployee(Employee emp)
    {
        blHandler.UpdateEmployee(emp);
    }
    public List<Employee> GetAllEmployees()
    {
        return blHandler.GetAllEmployees();
    }
    public Employee GetEmployee(int id)
    {
        return blHandler.GetEmployee(id);
    }
    public double CalcPartTimeEmployeeSalary(int idEmployee, int hours)
    {
        return blHandler.CalcPartTimeEmployeeSalary(idEmployee, hours);
    }
    }
    }

下一个文件:

     using System;
     using System.Collections.Generic;
     using System.Linq;
     using System.Text;
     using System.Threading.Tasks;
     using System.Runtime.Serialization;
     namespace Shared.Entities
     {
     [DataContract]
     [KnownType(typeof(FullTimeEmployee))]
     [KnownType(typeof(PartTimeEmployee))]
     public abstract class Employee
     {
    [DataMember]
    public int Id { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public DateTime StartDate { get; set; }
    }
    }

下一个文件:

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Runtime.Serialization;
  namespace Shared.Entities
  {
[DataContract]
public class FullTimeEmployee : Employee
{
    [DataMember]
    public int Salary { get; set; }
}
}

最后一个文件:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Runtime.Serialization;
    namespace Shared.Entities
    {
    [DataContract]
    public class PartTimeEmployee : Employee
    {
    [DataMember]
    public double HourlyRate { get; set; }
    }
    }

好吧,这些是我需要在wsdl中发布的内容,以便以后使用,而不是在PresentationLayer中访问BusinessLayer和DataLayer。

我已经成功地发布了这些OperationContracts,并且当我在PresentationLayerWinForm上添加url作为服务引用时,我得到了以下内容。

wsdl描述

我需要在该描述中看到Employee.cs、PartTimeEmployee、FullTimeEmployer类,但我没有得到它们,尽管它们是在wsdl中定义的。

wsdl_2

可能出了什么问题?

我已经清理了我的项目,重新编译了好几次,但我仍然得到了同样的东西,我需要这些分类发布,所以我错误地引用了PresentationLayerWinForm 中的共享实体

感谢4位帮助,

服务引用C#上缺少已发布的类

问题已解决;

问题是,我的服务器端DataContract类的名称与我在web服务客户端本地使用的类的名称相同,因此Visual Studio默认情况下会重新使用类型。右键单击Service Reference,单击Configure并关闭Type reuse。

无论如何,谢谢。