在ASMX Web服务之间共享枚举

本文关键字:共享 枚举 之间 服务 ASMX Web | 更新日期: 2023-09-27 18:12:18

我有一个包含多个web服务的web服务项目。其中两个web服务共享一个在BL类中定义的enum,如下所示:

public class HumanResourcesService
{
    public SomeLibrary.Employee GetEmployee(int employeeCode)
    {
       var employee = new SomeLibrary.Employee();
       employee.Type= SomeLibrary.EmployeeType.SomeType;
       return employee ;
    }
}
public class BankService
{
    public bool ProcessPayment(int employeeCode, EmployeeType employeeType)
    {
        bool processed = false;
        // Boring code
        return processed;
    }
}

这只是一个例子

两个web服务,当在web项目中引用时,生成不同的EmployeeType enum代理,因此我需要显式地强制转换以调用BankService中的ProcessPayment方法:

public void SomeMethod(int employeeCode)
{
     var hrService = new HumanResourcesService();
     var employee = hrService.GetEmployee(employeeCode);
     var bankService = new BankService();
     bankService.ProcessPayment(employee.Code, (MyProject.BankService.EmployeeType) employee.Type);
}

我理解。net为了创建WSDL必须这样做,但是我不能以某种方式使两个服务在代理类上引用相同的enum而不破坏任何东西吗?

在ASMX Web服务之间共享枚举

可使用wsdl.exe的sharetypes参数。详见http://msdn.microsoft.com/en-us/library/7h3ystb6.aspx。

如果您公开相同的enum,代理将正常工作:

public class BankService
{
    public bool ProcessPayment(int employeeCode, MyProject.BankService.EmployeeType employeeType)
    {
        bool processed = false;
        // Boring code
        return processed;
    }
}
public void SomeMethod(int employeeCode)
{
     var hrService = new HumanResourcesService();
     var employee = hrService.GetEmployee(employeeCode);
     var bankService = new BankService();
     bankService.ProcessPayment(employee.Code, employee.Type);
}