I have a flag attribute enumeration that is behind a web service as follows:
[Serializable,Flags]
public enum AccessLevels
{
None = 0,
Read = 1,
Write = 2,
Full = Read | Write
}
My problem is the consumer of my web service does not have the original constant values of the enum. The resulting proxy class client side has something that amounts to this:
{
None = 1,
Read = 2,
Write = 4,
Full = 8
}
And thus when the consumer is checking for "Read" access this will be false even when "testItem" is "Full"
((testItem & Svc.Read) == Svc.Read)
How can I properly provide flags over a web service?
EDIT:
According to this article it may not be possible to do what I am looking to do. Ivan Krivyakov states
Imperfect Transparency of Enums
It turns out that enums are not as transparent as we'd like them to be. There are three sticky issues:
- If server-side code declares an enum and assigns specific numeric values to its members, these values will not be visible to the client.
- If server-side code declares a [Flags] enum with "compound" mask values (as in White = Red|Green|Blue), it is not properly reflected on the client side.
- If server or client transmits an "illegal" value which is outside of the scope of the enum, it causes an exception in XML de-serializer on the other side.
So I wonder if this is just a limitation and is not possible.
One option would be to provide a more verbose class instead of the enum e.g.
The other option I can see is providing a method in what ever language they are using to successfully interoperate the integer you are sending. The flag operator means that c# does bit masking to find if a individual flag is marked
so to check for any permission you should see if that bit is set
Please note this second solution is more fragile, if you change the definition of accessLevels you client will silently miss behave.
I had a similar problem and got round it by adding another web service to return the currect flag values first.
Those then became the values I used in the compare.
Might not be the cleanest solution but it works.
Edit:
My original answer suggested that the values are passed across as a separate web service rather than within an enum.
However, poking around, it appears that an enumeration of 0,1,2 being mapped to 1,2,4 across a web service (even that the [Flags] attribute is set) is a common problem.
The solution suggested by a number of people is to modify the original enumeration definition and start from 1 rather than 0.
I have done extensive research on this and found that it is not possible to serialize enumeration constants through a web service. Note that to accomplish your goal you don't need the enumerations None or Full. These two enumerations can be implied with read / write combination:
You could assume full access if your AccessLevels = Read | Write and none if your AccessLevels = 0 [nothing]
Your enumerations would look like this:
Flags should be multiples of two, and in your case your flags are (0,1,2,3). Try change your definition of the struct to:
And see if it works better.
(I hope Im not making a fool of myself, its late and Im on my way to the bed.. )