Ejemplo n.º 1
0
        /// <summary>
        /// 引发 <see cref="BeforeRequestGetPreferedResponseType" /> 事件
        /// </summary>
        /// <param name="sender">引发此事件的源对象</param>
        /// <param name="ea">包含此事件的参数</param>
        public static void OnBeforeRequestGetPreferedResponseType(object sender, GetPreferedResponseTypeEventArgs ea)
        {
            var handler = BeforeRequestGetPreferedResponseType;

            if (handler != null)
            {
                handler(sender, ea);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 包装内容
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public HttpResponseContent GetResponseContent <T>(T data, GetPreferedResponseTypeEventArgs <T> ea)
        {
            if (data == null)
            {
                return(null);
            }

            var contextItem = GetBuilderContextItem(data.GetType());

            return(contextItem?.CachedGetResponseContentMethod?.Invoke(data, ea));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获得对应的数据处理类
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="streamInvoker"></param>
        /// <param name="result"></param>
        /// <param name="targetStream">目标流</param>
        /// <param name="saveToFilePath">希望保存到的目标文件路径</param>
        /// <param name="extraRequestInfo">额外的请求数据信息</param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public virtual HttpResponseContent GetPreferedResponseType <T>(HttpContext ctx, EventHandler <ResponseStreamContent.RequireProcessStreamEventArgs> streamInvoker = null, T result = default(T), Stream targetStream = null, string saveToFilePath = null, ExtraRequestInfo extraRequestInfo = null)
        {
            var resultType = typeof(T);
            var ea         = new GetPreferedResponseTypeEventArgs <T>(this, ctx, result, saveToFilePath, streamInvoker, targetStream, extraRequestInfo);

            ea.ResponseContent = Setting.ContentPayloadBuilder.GetResponseContent(result, ea);

            GlobalEvents.OnBeforeRequestGetPreferedResponseType(this, ea);
            OnBeforeRequestGetPreferedResponseType(this, ea);
            var content = ea.ResponseContent;

            if (content == null)
            {
                if (!saveToFilePath.IsNullOrEmpty())
                {
                    content = new ResponseFileContent(ctx, this, saveToFilePath);
                }
                else if (resultType != typeof(object))
                {
                    if (resultType == typeof(string))
                    {
                        content = new ResponseStringContent(ctx, this);
                    }
                    else if (resultType == typeof(byte[]))
                    {
                        content = new ResponseBinaryContent(ctx, this);
                    }
                    else if (resultType == typeof(Image))
                    {
                        content = new ResponseImageContent(ctx, this);
                    }
                    else if (resultType == typeof(XmlDocument))
                    {
                        content = new ResponseXmlContent(ctx, this, (XmlDocument)(object)result);
                    }
                    else if (resultType == typeof(EventHandler <ResponseStreamContent.RequireProcessStreamEventArgs>))
                    {
                        var r = new ResponseStreamContent(ctx, this);
                        if (streamInvoker != null)
                        {
                            r.RequireProcessStream += streamInvoker;
                        }
                        content = r;
                    }
                    else if (resultType == typeof(Stream))
                    {
                        content = new ResponseCopyStreamContent(ctx, this, targetStream ?? new MemoryStream());
                    }
                    else
                    {
                        content = new ResponseObjectContent <T>(ctx, this)
                        {
                            ObjectInternal = result
                        }
                    };
                }
                else
                {
                    content = null;                     //为null,等待请求自动判断
                }
                ea.ResponseContent = content;
            }

            //Global events
            GlobalEvents.OnRequestGetPreferedResponseType(this, ea);

            //instance events
            OnRequestGetPreferedResponseType(this, ea);

            //http handler
            HttpHandler.GetPreferedResponseType <T>(this, ctx, content, streamInvoker, result, targetStream);

            return(content);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 引发 <see cref="BeforeRequestGetPreferedResponseType" /> 事件
        /// </summary>
        /// <param name="sender">引发此事件的源对象</param>
        /// <param name="ea">包含此事件的参数</param>
        public static void OnBeforeRequestGetPreferedResponseType(object sender, GetPreferedResponseTypeEventArgs ea)
        {
            var handler = BeforeRequestGetPreferedResponseType;

            handler?.Invoke(sender, ea);
        }