示例#1
0
        private HttpReponseConvertDataHanlder GetConvertResultHanlder()
        {
            var key = new HttpConvertDataHanlderKey(typeof(TResult), ContentType);
            HttpReponseConvertDataHanlder hanlder = null;

            if (_convertHanlders.ContainsKey(key))
            {
                hanlder = _convertHanlders[key];
            }

            if (hanlder == null)
            {
                var closestAncestor = typeof(TResult).FindClosestAncestor(_convertHanlders.Where(t => ContentType.Contains(t.Key.ContentType, StringComparison.OrdinalIgnoreCase)).Select(t => t.Key.Type));
                if (closestAncestor != null)
                {
                    key     = new HttpConvertDataHanlderKey(closestAncestor, ContentType);
                    hanlder = _convertHanlders[key];
                }
            }
            if (hanlder == null)
            {
                throw new Exception($"无法反序列化ContentType为{ContentType}的结果到{typeof(TResult).FullName},因为未注册Hanlder");
            }
            return(hanlder);
        }
示例#2
0
 public static void RegisterConverter(Type[] types, string[] contentTypes, HttpReponseConvertDataHanlder hanlder)
 {
     if (hanlder == null)
     {
         throw new Exception("hanlder不能为空");
     }
     foreach (var type in types)
     {
         foreach (var ct in contentTypes)
         {
             var key = new HttpConvertDataHanlderKey(type, ct);
             if (_convertHanlders.ContainsKey(key))
             {
                 _convertHanlders[key] = hanlder;
             }
             else
             {
                 _convertHanlders.Add(key, hanlder);
             }
         }
     }
 }