예제 #1
0
        private async Task <object> RunMethod(Attribute[] attributes, Type retType, ParameterInfo[] parameterInfos, Parameters parameters)
        {
            RequestAttribute ra = attributes.FirstOrDefault(s => s is RequestAttribute) as RequestAttribute;

            if (ra == null)
            {
                FileTransferAttribute sa = attributes.FirstOrDefault(s => s is FileTransferAttribute) as FileTransferAttribute;
                if (sa == null)
                {
                    throw new MessageException("特性不存在");
                }
                return(SendStream(sa, parameters.ParametersInfo));
            }
            HttpResponseMessage rd = await Request(ra, parameterInfos, parameters.ParametersInfo);

            if (rd.IsSuccessStatusCode)
            {
                Type respType = retType;
                if (retType.IsTask())
                {
                    var args = retType.GenericTypeArguments;
                    if (args.Length > 0)
                    {
                        respType = args[0];
                    }
                }
                if (respType == typeof(HttpResponseMessage))
                {
                    return(rd);
                }
                if (respType == typeof(Stream))
                {
                    return(await rd.Content.ReadAsStreamAsync());
                }
                if (respType == typeof(byte[]))
                {
                    return(await rd.Content.ReadAsByteArrayAsync());
                }
                if (respType == typeof(void))
                {
                    return(Task.CompletedTask);
                }
                string value = await rd.Content.ReadAsStringAsync();

                if (respType == typeof(string))
                {
                    return(value);
                }
                if (!string.IsNullOrWhiteSpace(value))
                {
                    var v = value.ToObject(respType);
                    return(v);
                }
            }
            throw new MessageException(rd.ToString());
        }
예제 #2
0
        private Task <HttpResponseMessage> SendStream(FileTransferAttribute sa, params object[] objs)
        {
            string uri = HttpWebHelper.PathCombine(Uri, sa.Uri);

            return(HttpWebNetwork.SendStream(uri, sa.ContentName, sa.ContentType, objs));
        }