开发者

Using enum in WCF service

开发者 https://www.devze.com 2023-03-03 20:35 出处:网络
I have a WCF web service written in c# I have an enum [DataContract(Namespace = \"NameSpace\")] public enum State

I have a WCF web service written in c# I have an enum

[DataContract(Namespace = "NameSpace")]
public enum State
{
        [EnumMember]
        NotAvailable = 1,
        [EnumMember]
        RunningTests = 3,
        [EnumMember]
        ReadyForTests = 4,
        [EnumMember]
        HungOrBroken = 5
}

Then I run following commands to generate .h, .cpp files I can use in my MFC application.

svcutil.exe /t:metadata <webservice dll>
WSUtil.exe /prefix:Ws *.wsdl *.xsd

Above commands generate files that have:

typedef enum WsState
{
    WsStateHungOrBroken = 0,
    WsStateNotAvailable = 1,
    WsStateReadyForTests = 2,
    WsStateRunningTests = 3,
}

I understand why the enum values don't match exactly (info). Everything works as expected except that when I pass WsStateHungOrBroken to web service, it cannot interpret it correctly and simply convert it to 0. It should really be 5 which is how it is defined in web service.

If anyone can explain to me开发者_运维百科 why it is like this or how i can debug further, that would be useful. As a note, I've tried SvcTraceViewer but no luck in finding out what data is being passed in between service and my application.

Edit: Found solution at http://thorarin.net/blog/post/2010/08/08/Controlling-WSDL-minOccurs-with-WCF.aspx#comment By default all WCF method parameters are IsRequired=False and when enum value is 0, WCF treats this as empty value and don't pass it to service at all and service use default value. Making all parameters required fixes the issue.


The very link you supplied tells you why this happens. The numeric values of the enum are not preserved. Since HungOrBroken is first in alphabetical order, it will have the value 0 on the client side.

This is exactly what you posted in your question.

0

精彩评论

暂无评论...
验证码 换一张
取 消