Example #1
0
        public static async Task <DigitalPlatform.HTTP.HttpResponse> WebCall(DigitalPlatform.HTTP.HttpRequest request,
                                                                             string transferEncoding,
                                                                             TimeSpan timeout)
        {
            // 从 request.Url 中解析出 remoteUserName
            string remoteUserName = request.Url;

            if (string.IsNullOrEmpty(remoteUserName) == false &&
                remoteUserName[0] == '/')
            {
                remoteUserName = remoteUserName.Substring(1);
            }

            {
                // 只取第一级作为对方用户名
                List <string> parts = StringUtil.ParseTwoPart(remoteUserName, "/");
                remoteUserName = parts[0];
            }

            WebData data = MessageUtility.BuildWebData(request, transferEncoding);

            string         id    = Guid.NewGuid().ToString();
            WebCallRequest param = new WebCallRequest(id,
                                                      transferEncoding,
                                                      data,
                                                      true,
                                                      true);

            // CancellationToken cancel_token = new CancellationToken();

            try
            {
                // Console.WriteLine("Begin WebCall");

#if NO
                MessageConnection connection = _channels.GetConnectionTaskAsync(
                    Url,
                    remoteUserName).Result;
                WebCallResult result = connection.WebCallTaskAsync(
                    remoteUserName,
                    param,
                    new TimeSpan(0, 1, 10), // 10 秒
                    _cancel.Token).Result;
#endif
                MessageConnection connection = await _messageChannels.GetConnectionTaskAsync(
                    Url,
                    remoteUserName, // 每个图书馆一根独立通道
                    true,
                    true);          // 采用 incUseCount。使用后要归还

                WebCallResult result = null;
                try
                {
                    result = await connection.WebCallTaskAsync(
                        remoteUserName,
                        param,
                        timeout == TimeSpan.MinValue?new TimeSpan(0, 1, 10) : timeout,   // 1分10 秒
                        _cancel.Token);
                }
                finally
                {
                    _messageChannels.ReturnConnection(connection);
                }
                // Console.WriteLine("End WebCall result=" + result.Dump());

                if (result.Value == -1)
                {
                    // 构造一个 500 错误响应包
                    return(new DigitalPlatform.HTTP.HttpResponse("500",
                                                                 "Router Error: " + WebUtility.UrlEncode(result.ErrorInfo),
                                                                 result.ErrorInfo));
                }

                return(MessageUtility.BuildHttpResponse(result.WebData, transferEncoding));
            }
            catch (AggregateException ex)
            {
                string code = "500";

                WriteErrorLog("1(测试): " + ex.ToString());   // 2016/10/29

                string error = ExceptionUtil.GetExceptionText(ex);

                Console.WriteLine("Exception: " + error);

                MessageException ex1 = ExceptionUtil.FindInnerException(ex, typeof(MessageException)) as MessageException;
                if (ex1 != null && ex1.ErrorCode.ToLower() == "unauthorized")
                {
                    error = "dp2Router 针对 dp2MServer 的账户 '" + ex1.UserName + "' 登录失败";
                    code  = "401";
                }

                WriteErrorLog("1: " + error);

                // 构造一个错误代码响应包
                return(new DigitalPlatform.HTTP.HttpResponse(
                           code,
                           "Router Error: " + WebUtility.UrlEncode(error),
                           error));
            }
            catch (Exception ex)
            {
                WriteErrorLog("2: " + ExceptionUtil.GetExceptionText(ex));

                // 构造一个 500 错误响应包
                return(new DigitalPlatform.HTTP.HttpResponse(
                           "500",
                           "Router Error: " + WebUtility.UrlEncode(ex.Message),
                           ExceptionUtil.GetExceptionText(ex)));
            }
        }