Beispiel #1
0
        public static Try <object> ParseObject(
            IWireSerialization serializer,
            Try <Type> maybeType,
            Stream data,
            bool canCreate,
            IServiceProvider locator,
            HttpContext context)
        {
            if (maybeType.IsFailure)
            {
                return(Try <object> .Error);
            }
            var type = maybeType.Result;

            if (type == null)
            {
                return(Try <object> .Empty);
            }
            if (data == null)
            {
                if (canCreate == false)
                {
                    return(Try.Fail <object>(@"{0} must be provided. Example: 
{1}".With(type.FullName, serializer.EmptyInstanceString(type, context)), context.Response));
                }
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception ex)
                {
                    return(Try.Fail <object>(@"Can't create instance of {0}. Data must be provided. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
                }
            }
            try
            {
                //TODO: deserialize async
                var sc = new StreamingContext(StreamingContextStates.All, locator);
                //TODO: objects deserialized here will have global scope access. Do OnDeserialized again later in scope
                return(serializer.Deserialize(data, type, context.Request.ContentType, sc));
            }
            catch (TargetInvocationException tie)
            {
                var ex = tie.InnerException ?? tie;
                return(Try.Fail <object>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
            }
            catch (Exception ex)
            {
                return(Try.Fail <object>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type, context)), context.Response));
            }
        }
Beispiel #2
0
        public static object ParseObject(
            IWireSerialization serializer,
            Type type,
            Stream data,
            bool canCreate,
            IServiceLocator locator)
        {
            if (data == null)
            {
                if (canCreate == false)
                {
                    ThrowError(@"{0} must be provided. Example: 
{1}".With(type.FullName, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
                }
                try
                {
                    return(Activator.CreateInstance(type));
                }
                catch (Exception ex)
                {
                    ThrowError(@"Can't create instance of {0}. Data must be provided. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
                }
            }
            try
            {
                var sc = new StreamingContext(StreamingContextStates.All, locator);
                //TODO: objects deserialized here will have global scope access. Do OnDeserialized again later in scope
                return(serializer.Deserialize(data, type, ThreadContext.Request.ContentType, sc));
            }
            catch (TargetInvocationException tie)
            {
                var ex = tie.InnerException ?? tie;
                throw new WebFaultException <string>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
            }
            catch (Exception ex)
            {
                throw new WebFaultException <string>(@"Can't deserialize {0}. Error: {1}. Example: 
{2}".With(type.FullName, ex.Message, serializer.EmptyInstanceString(type)), HttpStatusCode.BadRequest);
            }
        }