locked
WCF Service/Web Service Reflection. RRS feed

  • Question

  • User-302746784 posted

    Hi, I had a WCF Service like

       

    public class CalculatorService : ICalculator

        {

           

    public double Add(double n1, double n2)

            {

               

    return n1 + n2;

            }

           

    public double Subtract(double n1, double n2)

            {

               

    return n1 - n2;

            }

           

    public double Multiply(double n1, double n2)

            {

               

    return n1 * n2;

            }

           

    public double Divide(double n1, double n2)

            {

               

    return n1 / n2;

            }

           

    public EmployeeOutput ProcessEmployee(EmployeeInput obj,string str, int i)

            {

               

    EmployeeOutput objOut = new EmployeeOutput();

                objOut.EmpNo = obj.EmpNo;

                objOut.EmpName = obj.EmpName;

               

    return objOut;

            }

    I am trying to call the service dynamically using reflection,

               

    CompilerResults compilerResults = null;

               

    object proxyInstance = GetProxyInstance(ref compilerResults);

               

    string operationName = "ProcessEmployee";

     

    MethodInfo

    methodInfo = proxyInstance.GetType().GetMethod(operationName);

    ParameterInfo

    [] paramInfos = methodInfo.GetParameters();

     

                var parameterType = paramInfos[0].ParameterType;             var parameter = compilerResults.CompiledAssembly.CreateInstance(parameterType.FullName, false, BindingFlags.CreateInstance, null, null, null, null);

                parameterType.GetProperties()[1].SetValue(parameter, "100", null);             //parameterType.GetProperties()[2].SetValue(parameter, "100", null);             parameterType.GetProperties()[2].SetValue(parameter, "test", null);

                object[] operationParameters = new object[] { parameter };

                //var result = methodInfo.Invoke(proxyInstance, parameters).GetType();             var result = methodInfo.Invoke(proxyInstance, BindingFlags.InvokeMethod, null, operationParameters, null);

    1) Instead of parmInfos[0] , i want to automate to accept mutiple simple & comple types.

    2)

    compilerResults.CompiledAssembly.CreateInstance

    is creating instance if the parameter is complex type, for simple type throwing error.

    Instead of saying

                parameterType.GetProperties()[1].SetValue(parameter,

    "100", null);

    I want to automate this one in one line where I can retrieve the values from a collection and bind at runtime.

    Thanks In Advance

    Wednesday, November 13, 2013 8:08 PM

All replies

  • User-488622176 posted

    Sorry, I do not understand your problem. Creating complex types can be done with reflection by using Activator.CreateType(new Type("<mytypenamehere>");.

    Please post code using the dedicted option in the forum editor (before the help button), it's a lot easier to read ;-)

    Thursday, November 14, 2013 3:47 AM
  • User-302746784 posted
    My Service:-
    public class CalculatorService : ICalculator
        {
            public double Add(double n1, double n2)
            {
                return n1 + n2;
            }
            public EmployeeOutput ProcessEmployee(EmployeeInput obj,string str, int i)
            {
                EmployeeOutput objOut = new EmployeeOutput();
                objOut.EmpNo = obj.EmpNo;
                objOut.EmpName = obj.EmpName;
                return objOut;
            }
    [DataContract]
        public class EmployeeInput
        {
            [DataMember]
            public string EmpNo { get; set; }
    
            [DataMember]
            public string EmpName { get; set; }
        }
    From client, I am trying to do this, since I had complex Type(EmployeeInput and primitive type string, int) and in complex type I had properties and fields, issue I am facing is mapping the inputs to service parameters for complex types as well as primitive types.  My Code at client:
    static class ObjectExtensions
        {
            public static void SetPropertyValue<T>(this object obj, string propertyName, T propertyValue)
                where T : IConvertible
            {
                PropertyInfo pi = obj.GetType().GetProperty(propertyName);
    
                if (pi != null && pi.CanWrite)
                {
                    pi.SetValue
                    (
                        obj,
                        Convert.ChangeType(propertyValue, pi.PropertyType),
                        null
                    );
                }
            }
        }
    class Program
        {
            static void Main(string[] args)
            {
                CompilerResults compilerResults = null;
    
                object proxyInstance = GetProxyInstance(ref compilerResults);
                string operationName = "ProcessEmployee";
                MethodInfo methodInfo = proxyInstance.GetType().GetMethod(operationName);
                ParameterInfo[] paramInfos = methodInfo.GetParameters();
                OrderedDictionary valueList = new OrderedDictionary();
                IList<Object> parameter = new List<Object>();
                //Object[] parameter = new Object[10];
                for (int paramIndex = 0; paramIndex < paramInfos.Length; paramIndex++)
                {
                    int i = 1;
                    var parameterType = paramInfos[paramIndex].ParameterType;
                    bool IsPrimitive = parameterType.GetType().IsPrimitive;
                    Object parameter2 = new Object();
                    if (parameterType.Namespace.StartsWith("System"))
                    {
                        parameter2.SetPropertyValue("str", "Test");
                        //parameterType.GetProperties()[i].SetValue(parameter2, "Test", null);
                    }
                    else
                    {
                        parameter2 = compilerResults.CompiledAssembly.CreateInstance(parameterType.FullName, false, BindingFlags.CreateInstance, null, null, null, null);
                        EmployeeInput obj = new EmployeeInput();
                        obj.EmpNo = "123";
                        obj.EmpName = "Test";
                        Console.WriteLine(parameter2.GetType());
                        Console.WriteLine(obj.GetType());
                        parameter2.SetPropertyValue("EmpNo", 123);
                        parameter2.SetPropertyValue("EmpName", "Test");
                        //parameterType.GetProperties()[i].SetValue(parameter2, Convert.ChangeType(obj, parameter2.GetType()), null);
                        //parameterType.GetProperties()[i].SetValue(parameter2, obj, null);
                    }
                    parameter.Add(parameter2);
                    //parameter[paramIndex] = parameter2;
                    i++;
                }
     object[] operationParameters = new object[] { parameter };
    var result = methodInfo.Invoke(proxyInstance, BindingFlags.InvokeMethod, null, operationParameters, null);
    The above code is failing when mapping complex fields.  I mean from client, client send a collection, ex:- Dictionary collection with ComplexType, PrimitiveTypes, I should be able to invoke my operationContract at service Side, this is my requirement.
    

    Thursday, November 14, 2013 5:10 AM
  • User-302746784 posted

    Hi Illeris, thanks for your reply, My Req. is I want to call WCF Service Dynamically with Complex Types and simple types as parameters for operationContract. Client will just send WSDL file, OperationContract, input parameters as dictionary collection?

    Thursday, November 14, 2013 5:13 AM
  • User-302746784 posted

    HI All, Is there any other approach oher than reflection, to invoke the WCF service method dynamically with simple & complex types?

    Monday, November 18, 2013 8:20 PM
  • User-488622176 posted

    At first sight I'm not seeing why this should fail. What is the error you get?

    Tuesday, November 19, 2013 9:18 AM