示例#1
0
        private object FetchData(IMethodCallMessage methodCall)
        {
            var uri         = BaseAddress + GetAction(methodCall.MethodBase);
            var relativeUri = ReplenishURL(methodCall);

            if (!string.IsNullOrWhiteSpace(relativeUri))
            {
                uri = string.Format("{0}?{1}", uri, relativeUri);
            }

            var absoluteUrl = string.Format("{0}#{1}", ClientProxy.BaseAddress, uri);
            var returnType  = (methodCall.MethodBase as MethodInfo).ReturnType;

            var httpMethod = GetHttpMethod(methodCall.MethodBase);

            if (httpMethod == "GET")
            {
                return(ClientProxy.GetAsync(uri).FetchValue(returnType, httpMethod, absoluteUrl));
            }
            else if (httpMethod == "DELETE")
            {
                return(ClientProxy.DeleteAsync(uri).FetchValue(returnType, httpMethod, absoluteUrl));
            }
            else
            {
                var body = FetchBody(methodCall);

                if (httpMethod == "POST")
                {
                    return(ClientProxy.PostAsJsonAsync(uri, body).FetchValue(returnType, httpMethod, absoluteUrl));
                }
                else if (httpMethod == "PUT")
                {
                    return(ClientProxy.PutAsJsonAsync(uri, body).FetchValue(returnType, httpMethod, absoluteUrl));
                }

                throw new NotSupportedException("不支持的请求方法:" + httpMethod);
            }
        }