Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        public static T Wrap <T>(Type interfacetype) where T : class
        {
            Assembly assembly = interfacetype.Assembly;
            Module   module   = interfacetype.Module;

            AssemblyName assemblyName = new AssemblyName(assembly.FullName);

            assemblyName.Name = assemblyName.Name + "$001";

            AssemblyBuilder assemblyBuilder = AssemblyBuilder.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
            ModuleBuilder   moduleBuilder   = assemblyBuilder.DefineDynamicModule(module.Name);

            // Console.WriteLine ("11111111");
            // Console.WriteLine ("11111111");
            // Console.WriteLine ("11111111");
            // Console.WriteLine (interfacetype.FullName + "$1");
            // Console.WriteLine (interfacetype.Namespace+"."+interfacetype.Name + "$1");
            // Console.WriteLine ("11111111");
            // Console.WriteLine ("11111111");
            // Console.WriteLine ("11111111");

            TypeBuilder typeBuilder = moduleBuilder.DefineType(interfacetype.Namespace + "." + interfacetype.Name + "$1"
                                                               , TypeAttributes.Class | TypeAttributes.Public, typeof(WrapBase), new Type[] { interfacetype });

            ConstructorBuilder constructorBuilder = typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);

            //constructorBuilder.GetILGenerator().Emit(OpCodes.Ret);

            constructorBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
            ILGenerator ctor0IL = constructorBuilder.GetILGenerator();

            ctor0IL.Emit(OpCodes.Ret);

            MethodInfo[] interfacemethods = interfacetype.GetMethods();
            MethodInfo   interfacemethodInfo;

            InterfaceWrapContext interfaceWrapContext = InterfaceWrapContext.GetContext(interfacetype);

            for (int i = 0; i < interfacemethods.Length; i++)
            {
                interfacemethodInfo = interfacemethods[i];
                MethodFactory.GenerateMethod(interfacetype, interfacemethodInfo, typeBuilder);
            }

            Type newtype = typeBuilder.CreateType();

            T             t             = (T)Activator.CreateInstance(newtype);
            InterfaceItem interfaceItem = new InterfaceItem(t, interfacetype, interfaceWrapContext);

            Lark.Core.Lark.InterfaceWrapCache[interfacetype] = interfaceItem;

            return(t);
        }
Exemplo n.º 3
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
 }
Exemplo n.º 4
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
     interfaceWrapContext.ContentType = HttpContentTypes.xml;
 }
Exemplo n.º 5
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
     interfaceWrapContext.HeaderAttributes.Add(this);
 }
Exemplo n.º 6
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
     base.SaveToInterfaceContext(interfaceWrapContext);
     interfaceWrapContext.SerializeType = SerializeTypes.xml;
 }
Exemplo n.º 7
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
     interfaceWrapContext.HttpMethod = this.Method.ToUpper();
 }
Exemplo n.º 8
0
 internal override void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
     interfaceWrapContext.Path = this.Path;
 }
Exemplo n.º 9
0
 internal virtual void SaveToInterfaceContext(InterfaceWrapContext interfaceWrapContext)
 {
 }
Exemplo n.º 10
0
 internal InterfaceItem(object wrapInstance, Type interfaceType, InterfaceWrapContext context)
 {
     WrapInstance       = wrapInstance;
     WrapContext        = context;
     this.interfaceType = interfaceType;
 }