/// <summary> /// 包装接口。 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> //TODO 会有多线程访问的问题 System.Collections.Concurrent.ConcurrentDictionary public static T Wrap <T>(string url) where T : class { Type interfacetype = typeof(T); if (false == interfacetype.IsInterface) { throw new ArgumentException(string.Format("{0} should be a interface", nameof(T))); } T t; if (InterfaceWrapCache.ContainsKey(interfacetype)) { t = (T)InterfaceWrapCache[interfacetype].WrapInstance; } else { t = ClassFactory.Wrap <T>(interfacetype); } Type newType = t.GetType(); object newo = Activator.CreateInstance(newType); WrapBase wrapbase = (WrapBase)newo; T newt = (T)newo; wrapbase.Url = url; return(newt); }
public void Test2() { IStudentService student = Lark.Core.Lark.Wrap <IStudentService>("http://*****:*****@"/api/student/add?ID=1&Name=name&rank=23"); //-------------------------------------- student.Add2(new IStudentService.Student() { ID = 1, Name = "name", rank = 23 }, new IStudentService.StudentClass() { }, new IStudentService.Remark() { }); wrap = (WrapBase)student; httpRequestHeaders = wrap.MyHttpRequestMessagea.Headers; httpContent = wrap.MyHttpRequestMessagea.Content; System.Console.WriteLine(wrap.GetRequestCreURL()); //Assert.Catch( delegate {} ,"",null); }
internal static LarkResult Create(RequestCreContext requestCreContext) { WrapBase wrapBase = requestCreContext.WrapInstance; MethodWrapContext methodWrap = requestCreContext.MethodWrap; if (methodWrap == null) { throw new ArgumentNullException(nameof(methodWrap)); } InterfaceWrapContext interfaceWrap = requestCreContext.InfaceContext; HttpRequestMessage httpRequestMessage = requestCreContext.PreparaRequestMessage(); //todo is it ok? httpRequestMessage.RequestUri = new Uri(requestCreContext.GetRequestUrl()); System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient(); //TODO it's need to deal with the http status code Task <HttpResponseMessage> task; switch (requestCreContext.HttpMethod.Method) { case "GET": case "POST": httpRequestMessage.Method = new HttpMethod(requestCreContext.HttpMethod.Method); break; default: throw new NotSupportedException("Not supported Http Method!"); } if (InternalConfig.SaveRequest) { wrapBase.MyClient = httpClient; wrapBase.MyHttpRequestMessagea = httpRequestMessage; wrapBase.MyRequestCreContext = requestCreContext; } if (InternalConfig.NotRequest) { return(LarkResult.GetResult(null, requestCreContext.MethodWrap.ReturnContext));; } task = httpClient.SendAsync(httpRequestMessage); //todo need to try-catch ? task.Wait(); if (InternalConfig.SaveResponse) { requestCreContext.WrapInstance.OriginalResponseMessage = task.Result; } return(LarkResult.GetResult(task.Result, requestCreContext.MethodWrap.ReturnContext)); }
public void Test1() { object responseResult; IStudentService student = Lark.Core.Lark.Wrap <IStudentService>("http://localhost:6346"); responseResult = student.QueryName(12, "upper", "234"); System.Console.WriteLine(responseResult.ToString()); WrapBase wrap = (WrapBase)student; HttpRequestHeaders HttpRequestHeaders = wrap.MyHttpRequestMessagea.Headers; HttpContent httpContent = wrap.MyHttpRequestMessagea.Content; List <string> values = new List <string>(); IEnumerable <string> valuesEnumrable = null; //appcode:just one Assert.IsTrue(HttpRequestHeaders.Contains("appcode") || httpContent.Headers.Contains("appcode")); bool l = HttpRequestHeaders.TryGetValues("appcode", out valuesEnumrable) ? true : httpContent.Headers.TryGetValues("appcode", out valuesEnumrable); values = valuesEnumrable.ToList(); values.Sort(); string valuestr = string.Join(",", values); Assert.IsTrue("appcode111111111111" == valuestr); //supportversion:3 Assert.IsTrue(HttpRequestHeaders.Contains("supportversion") || httpContent.Headers.Contains("supportversion")); l = HttpRequestHeaders.TryGetValues("supportversion", out valuesEnumrable) ? true : httpContent.Headers.TryGetValues("supportversion", out valuesEnumrable); values = valuesEnumrable.ToList(); Assert.IsTrue(l && values.Count == 3); values.Sort(); valuestr = string.Join(",", values); Assert.IsTrue("1.0,2.0,3.0" == valuestr); //case:2 Assert.IsTrue(HttpRequestHeaders.Contains("case") || httpContent.Headers.Contains("case")); l = HttpRequestHeaders.TryGetValues("case", out valuesEnumrable) ? true : httpContent.Headers.TryGetValues("case", out valuesEnumrable); values = valuesEnumrable.ToList(); Assert.IsTrue(l && values.Count == 2); values.Sort(); valuestr = string.Join(",", values); Assert.IsTrue("lower,upper" == valuestr); //prefix:1 Assert.IsTrue(HttpRequestHeaders.Contains("prefix") || httpContent.Headers.Contains("prefix")); l = HttpRequestHeaders.TryGetValues("prefix", out valuesEnumrable) ? true : httpContent.Headers.TryGetValues("prefix", out valuesEnumrable); values = valuesEnumrable.ToList(); Assert.IsTrue(l && values.Count == 1); values.Sort(); valuestr = string.Join(",", values); Assert.IsTrue("234" == valuestr); }
public void Test() { IStudentService student = Lark.Core.Lark.Wrap <IStudentService>("http://*****:*****@"/api/student/111/nnnnname/98/del"); }
//[Test] public void WrapTest_One() { IStudentService student = Lark.Core.Lark.Wrap <IStudentService>(""); for (int i = 0; i < 1; i++) { student = Lark.Core.Lark.Wrap <IStudentService>(BaseUrl); WrapBase wrap = (WrapBase)student; // Assert.IsTrue("Hello!" == student.SayHello("myid","testheader",null)); HttpResponseMessage responseMessage = wrap.OriginalResponseMessage; ProbeInfo probeInfo = Util.GetProbe(responseMessage) ?? throw new Exception("probeinfo is null"); Assert.IsTrue(probeInfo.Url.EndsWith("/api/student/sayhello")); Assert.IsTrue(probeInfo.Method == "GET"); Assert.IsTrue(probeInfo.Headers.Exists(x => x.Key == "myheader" && x.Value == "hello")); } }
public static object Invoke(Type interfacetype, WrapBase wrapBase, MethodInfo methodInfo, List <Object> args) { // if (InternalConfig.EmitTestCode) // { // Console.WriteLine("InvokeProxy args:"); // args.ForEach(x=>{ // Console.WriteLine((x??new object()).ToString()); // }); // } try { if (false == Lark.InterfaceWrapCache.ContainsKey(interfacetype)) { throw new LarkException("RuntimeException:wrapcache is not exists!"); } InterfaceItem interfaceItem = Lark.InterfaceWrapCache[interfacetype]; if (false == interfaceItem.WrapContext.MethodCache.ContainsKey(methodInfo)) { throw new LarkException("RuntimeException:MethodCache is not exists!"); } MethodItem methodItem = interfaceItem.WrapContext.MethodCache[methodInfo]; //todo need a pool of RequestCreContext RequestCreContext requestCreContext = RequestCreContext.Create(interfaceItem.WrapContext, methodItem.WrapContext, wrapBase); requestCreContext.ParameterValues.Value = args; return(HttpCreater.Create(requestCreContext).DealResponse(methodInfo.ReturnType)); } catch (System.Exception ex) { System.Console.WriteLine(ex.ToString()); throw ex; } }
internal static RequestCreContext Create(InterfaceWrapContext interfaceWrap, MethodWrapContext methodWrap, WrapBase wrapinstance) { RequestCreContext instance = new RequestCreContext(interfaceWrap, methodWrap, wrapinstance); instance.CreateHeaderBind(); return(instance); }
private RequestCreContext(InterfaceWrapContext interfaceWrap, MethodWrapContext methodWrap, WrapBase wrapinstance) { this.InfaceContext = interfaceWrap; this.MethodWrap = methodWrap; this.WrapInstance = wrapinstance; }
public static string GetRequestCreURL(this WrapBase warpbase) { return(warpbase.MyRequestCreContext.httpRequestMessage.RequestUri.ToString()); }