Esempio n. 1
0
        private void GenerateMethodProxy(TypeBuilder proxyBuilder, Type type, MethodInfo method, bool callByName, FieldBuilder callService, FieldBuilder serializer, FieldBuilder jsonMethodWriterField)
        {
            var compressRequest  = _methodCompressionPicker?.CompressRequest(method) ?? false;
            var compressResponse = _methodCompressionPicker?.CompressResponse(method) ?? false;

            var methodAttributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual | MethodAttributes.NewSlot;

            var methodBuilder = proxyBuilder.DefineMethod(method.Name, methodAttributes, method.ReturnType, method.GetParameters().Select(x => x.ParameterType).ToArray());

            var ilGenerator = methodBuilder.GetILGenerator();

            ilGenerator.DeclareLocal(typeof(byte[]));                                                    // LdLoc_0
            ilGenerator.DeclareLocal(compressRequest ? typeof(GZipMemoryStream) : typeof(MemoryStream)); // LdLoc_1
            ilGenerator.DeclareLocal(typeof(StreamWriter));                                              // LdLoc_2
            ilGenerator.DeclareLocal(typeof(JsonTextWriter));                                            // LdLoc_3

            if (compressRequest)
            {
                EmitGzipMemoryStream(ilGenerator);
            }
            else
            {
                EmitMemoryStream(ilGenerator);
            }

            EmitStreamWriter(ilGenerator);

            EmitJsonWriter(ilGenerator);

            EmitJsonOpenObject(ilGenerator, type, method, jsonMethodWriterField);

            if (callByName)
            {
                EmitParametersByName(ilGenerator, method, serializer);
            }
            else
            {
                EmitParametersByOrder(ilGenerator, method, serializer);
            }

            EmitIdAndJsonObjectClose(ilGenerator, jsonMethodWriterField);

            EmitJsonWriterFinallyDispose(ilGenerator);

            EmitStreamWriterDispose(ilGenerator);

            EmitCreateBytes(ilGenerator, compressRequest);

            EmitMemoryStreamFinallyDispose(ilGenerator);

            EmitMakeCall(ilGenerator, method, callService, compressRequest, compressResponse);

            proxyBuilder.DefineMethodOverride(methodBuilder, method);
        }
Esempio n. 2
0
        public void ProxyGenerator_Create_Interface_Compress_Response(IRpcProxyService proxyService,
                                                                      IMethodCompressionPicker compressionPicker,
                                                                      ProxyGenerator proxyGenerator,
                                                                      Fixture fixture)
        {
            byte[] bytes = new byte[0];

            compressionPicker.CompressResponse(Arg.Any <MethodInfo>()).Returns(true);

            proxyService.MakeCallWithReturn <int>(typeof(IIntMathService).Namespace, typeof(IIntMathService).Name, nameof(IIntMathService.Add), Arg.Any <byte[]>(), false, true)
            .Returns(c =>
            {
                bytes = c.Arg <byte[]>();

                return(15);
            });

            var proxyType = proxyGenerator.GenerateProxyType(typeof(IIntMathService), true);

            var instance = (IIntMathService)fixture.Locate(proxyType);

            var value = instance.Add(5, 10);

            Assert.Equal(15, value);

            var request = bytes.Deserialize <RpcRequestMessage>();

            Assert.NotNull(request);
            Assert.Equal("2.0", request.Version);
            Assert.Equal("Add", request.Method);
            Assert.False(string.IsNullOrEmpty(request.Id));

            //var objectDictionary = request.Parameters as Dictionary<string, object>;

            //Assert.NotNull(objectDictionary);
            //Assert.Equal(2, objectDictionary.Count);
            //Assert.Equal(5, Convert.ToInt32(objectDictionary["a"]));
            //Assert.Equal(10, Convert.ToInt32(objectDictionary["b"]));
        }