WCF - 枚举类型

通常情况下,我们无需做任何设置就可以在 WCF 中使用枚举类型。

public enum DataType
{
  A,
  B,
  C
}

[ServiceContract]
public interface IMyService
{
  [OperationContract]
  void Test(DataType d);
}


客户端代理

//------------------------------------------------------------------------------
// <auto-generated>
//   此代码由工具生成。
//   运行库版本:2.0.50727.42
//
//   对此文件的更改可能会导致不正确的行为,并且如果
//   重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConsoleApplication1.localhost
{
  [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
  [DataContractAttribute(Namespace = "...")]
  public enum DataType : int
  {
    [EnumMemberAttribute()]
    A = 0,

    [numMemberAttribute()]
    B = 1,

    [EnumMemberAttribute()]
    C = 2,
  }

  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [ServiceContractAttribute(ConfigurationName = "ConsoleApplication1.localhost.IMyService")]
  public interface IMyService
  {
    [OperationContractAttribute(Action = "http://tempuri.org/IMyService/Test", ReplyAction = "...")]
    void Test(DataType d);
  }
}


如果我们只想使用部分枚举值时,可以使用 EnumMemberAttribute。

[DataContract]
public enum DataType
{
  [EnumMember]A,
  B,
  [EnumMember]C
}


代理文件

//------------------------------------------------------------------------------
// <auto-generated>
//   此代码由工具生成。
//   运行库版本:2.0.50727.42
//
//   对此文件的更改可能会导致不正确的行为,并且如果
//   重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConsoleApplication1.localhost
{
  [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
  [DataContractAttribute(Namespace = "...")]
  public enum DataType : int
  {
    [EnumMemberAttribute()]
    A = 0,

    [EnumMemberAttribute()]
    C = 2,
  }
}


我们还可以使用 EnumMemberAttribute.Value 为枚举值设置一个 "固定" 的名字,这和前面我们提到的 ServiceContract / DataContract 版本问题类似。

[DataContract]
public enum DataType
{
  [EnumMember(Value="A")]Axxx,
  B,
  [EnumMember]C
}


最后要注意的是,如果我们要使用自定值的枚举类型,一定要象下面这样。

1. 在服务契约上添加 ServiceKnownType(typeof(Enum))。
2. 为枚举值添加 [EnumMember]。

[DataContract]
[Flags]
public enum DataType
{
  [EnumMember]A = 1,
  [EnumMember]B = 2,
  [EnumMember]C = 4,
  [EnumMember]D = 8
}

[ServiceContract]
[ServiceKnownType(typeof(DataType))]
public interface IMyService
{
  [OperationContract]
  void Test(DataType d);
}
//------------------------------------------------------------------------------
// <auto-generated>
//   此代码由工具生成。
//   运行库版本:2.0.50727.42
//
//   对此文件的更改可能会导致不正确的行为,并且如果
//   重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConsoleApplication1.localhost
{
  [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
  [FlagsAttribute()]
  [DataContractAttribute(Namespace = "...")]
  public enum DataType : int
  {
    [EnumMemberAttribute()]
    A = 1,

    [EnumMemberAttribute()]
    B = 2,

    [EnumMemberAttribute()]
    C = 4,

    [EnumMemberAttribute()]
    D = 8,
  }

  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [ServiceContractAttribute(ConfigurationName = "ConsoleApplication1.localhost.IMyService")]
  public interface IMyService
  {
    [OperationContractAttribute(Action = "http://tempuri.org/IMyService/Test", ReplyAction = "...")]
    void Test(DataType d);
  }

  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  public interface IMyServiceChannel : IMyService, IClientChannel
  {
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  public partial class MyServiceClient : ClientBase<IMyService>, IMyService
  {
    public void Test(DataType d)
    {
      base.Channel.Test(d);
    }
  }
}


否则,生成的客户端代理就是下面这个样子了。晕不? [lol]

//------------------------------------------------------------------------------
// <auto-generated>
//   此代码由工具生成。
//   运行库版本:2.0.50727.42
//
//   对此文件的更改可能会导致不正确的行为,并且如果
//   重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------

namespace ConsoleApplication1.localhost
{
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [ServiceContractAttribute(ConfigurationName = "ConsoleApplication1.localhost.IMyService")]
  public interface IMyService
  {
    // CODEGEN: Generating message contract since element name d from namespace
    // http://tempuri.org/ is not marked nillable

    [OperationContractAttribute(Action = "http://tempuri.org/IMyService/Test", ReplyAction = "...")]
    TestResponse Test(TestRequest request);
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [MessageContractAttribute(IsWrapped = false)]
  public partial class TestRequest
  {
    [MessageBodyMemberAttribute(Name = "Test", Namespace = "http://...", Order = 0)]
    public TestRequestBody Body;

    public TestRequest()
    {
    }

    public TestRequest(TestRequestBody Body)
    {
      this.Body = Body;
    }
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [DataContractAttribute(Namespace = "http://tempuri.org/")]
  public partial class TestRequestBody
  {
    [DataMemberAttribute(EmitDefaultValue = false, Order = 0)]
    public string d;

    public TestRequestBody()
    {
    }

    public TestRequestBody(string d)
    {
      this.d = d;
    }
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [MessageContractAttribute(IsWrapped = false)]
  public partial class TestResponse
  {
    [MessageBodyMemberAttribute(Name = "TestResponse", Namespace = "http://...", Order = 0)]
    public TestResponseBody Body;

    public TestResponse()
    {
    }

    public TestResponse(TestResponseBody Body)
    {
      this.Body = Body;
    }
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  [DataContractAttribute()]
  public partial class TestResponseBody
  {
    public TestResponseBody()
    {
    }
  }

  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  public interface IMyServiceChannel : IMyService, IClientChannel
  {
  }

  [DebuggerStepThroughAttribute()]
  [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
  public partial class MyServiceClient : ClientBase<IMyService>, IMyService
  {
    TestResponse IMyService.Test(TestRequest request)
    {
      return base.Channel.Test(request);
    }

    public void Test(string d)
    {
      TestRequest inValue = new TestRequest();
      inValue.Body = new TestRequestBody();
      inValue.Body.d = d;
      TestResponse retVal = ((IMyService)(this)).Test(inValue);
    }
  }
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值