在ASMX Web服务之间共享枚举

时间:2011-10-25 22:34:24

标签: c# asp.net .net web-services webservice-client

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

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枚举代理,因此我需要显式转换以调用ProcessPayment中的BankService方法:

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,但我不能以某种方式使两个服务在代理类上引用相同的枚举而不会破坏任何内容吗?

2 个答案:

答案 0 :(得分:3)

您可以使用wsdl.exe的sharetypes参数。有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/7h3ystb6.aspx

答案 1 :(得分:0)

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

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