Quantcast
Channel: Enum accepting any string
Viewing all articles
Browse latest Browse all 6

Enum accepting any string

$
0
0

I have an asynchronous WCF service and was getting the following exception:

An error occurred while receiving the HTTP response to http://localhost:34840/La5108Service.svc. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

Upon investigation, I discovered that my enum was being assigned a wrong value.  However, the exception didn't happen when parsing the string to an enum, it happened when the object was deserialized by our MVC 4 application.

This struck me as very curious, so I went to the line in my repository where the crash SHOULD have occurred.

ActionType = dataReader.GetEnum<ActionType?>("ActionTypeDescription")

GetEnum is an extension method that looks like this:

        public static T GetEnum<T>(this DbDataReader dbDataReader, String columnName)
        {
            Object currentValue = ReadColumn(dbDataReader, columnName);;
            if (IsNull(currentValue))
                return default(T);
            Type enumType = typeof(T);
            // get generic arguments
            Type[] arguments = enumType.GetGenericArguments();
            // if it is a nullable type, reassign enumType to the enum type
            if (arguments != null && arguments.Length > 0)
                enumType = arguments[0];
            return (T)Enum.Parse(enumType, currentValue.ToString());
        }

Then I created a console application and ran this exact code and it crashed, as it should have (when trying to assign a value to the enum that doesn't exist).  But yet, works in WCF (crashes when it gets deserialized by the client).  This has made me come to the conclusion that WCF doesn't actually work with real types, instead, it works with serialized version of that type under the hood.  Can anyone confirm or deny this and provide proof of what/why WCF is doing?


Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>