none
find out CompositeTypeand find inside any CompositeType class is there are not through WSDL refelction in .net? RRS feed

  • General discussion

  • namespace WcfService1
    {
        [ServiceContract]
        public interface IService1
        {
            [OperationContract]
            string GetTestString(string value);
            [OperationContract]
            CompositeType GetTestDataUsingDataContract(int i, CompositeType composite, string str);//, EmployeeIn obj);
        }
        // Use a data contract as illustrated in the sample below to add composite types to service operations.
        [DataContract]
        public class Employee
        {
            [DataMember]
            public int EmpNo { get; set; }
            [DataMember]
            public string EmpName { get; set; }
        }
        [DataContract]
        public class CompositeType
        {
            bool boolValue = true;
            string stringValue = "Hello ";
            
            [DataMember]
            public bool BoolValue
            {
                get { return boolValue; }
                set { boolValue = value; }
            }
            [DataMember]
            public string StringValue
            {
                get { return stringValue; }
                set { stringValue = value; }
            }
            [DataMember]
            public Employee EmployeeValue
            {
                get { return EmployeeValue; }
                set { EmployeeValue = value; }
            }
        }
        [DataContract]
        public class EmployeeIn
        {
            bool UserIsOnline = true;
            string UserName = "Karthik";
            [DataMember]
            public bool BoolValue
            {
                get { return UserIsOnline; }
                set { UserIsOnline = value; }
            }
            [DataMember]
            public string StringValue
            {
                get { return UserName; }
                set { UserName = value; }
            }
        }

    protected void btnCompostiteDataTypeStringValue_Click(object sender, EventArgs e)
            {
                CompilerResults compilerResults = null;
               
                object proxyInstance = GetProxyInstance(ref compilerResults);
                string operationName = "GetTestDataUsingDataContract";
                var methodInfo = proxyInstance.GetType().GetMethod(operationName);
                
                ParameterInfo[] paramInfos = methodInfo.GetParameters();
                IOrderedDictionary arguments = new OrderedDictionary();
                
                CompositeType Obj = new CompositeType();
                Obj.BoolValue = true;
                Obj.StringValue = "Test1";
                arguments.Add("i", 100);
                arguments.Add("composite", Obj);
                arguments.Add("str", "test2");
                Employee e2 = new Employee();
                e2.EmpNo = 123;
                e2.EmpName = "Chandan";
                Obj.EmployeeValue = e2;
             
                OrderedDictionary valueList = new OrderedDictionary();
                Object[] ObjValues = new Object[paramInfos.Length];
                for (int paramIndex = 0; paramIndex < paramInfos.Length; paramIndex++)
                {
                    foreach (DictionaryEntry valueEntry in arguments)
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(paramInfos[paramIndex].ParameterType);
                        if (paramInfos[paramIndex].Name.Equals(valueEntry.Key.ToString()))
                        {
                            if (PrimitiveTypes.Test(valueEntry.Value.GetType()) || valueEntry.Value.GetType().IsPrimitive || valueEntry.Value.GetType().IsValueType
                                || valueEntry.Value.GetType().Module.ScopeName == "CommonLanguageRuntimeLibrary")
                            {
                                ObjValues[paramIndex] = valueEntry.Value;
                            }
                           
                            else if (!PrimitiveTypes.Test(valueEntry.Value.GetType()) || valueEntry.Value.GetType().IsByRef)
                            {
                                var parameterType1 = paramInfos[paramIndex].ParameterType;
                                var parameter2 = compilerResults.CompiledAssembly.CreateInstance(paramInfos[paramIndex].ParameterType.FullName, false, BindingFlags.CreateInstance, null, null, null, null);
                                CopyObjectData(arguments[paramIndex], parameter2, "", BindingFlags.Public | BindingFlags.Instance);
                                valueList.Add(valueEntry.Key, parameter2);
                                ObjValues[paramIndex] = parameter2;
                            }
                        }
                    }
                }
             
               
                var result = methodInfo.Invoke(proxyInstance, BindingFlags.InvokeMethod, null, ObjValues, null);
                var resultType = methodInfo.Invoke(proxyInstance, BindingFlags.InvokeMethod, null, ObjValues, null).GetType();
               
            }
      
            public static object GetProxyInstance(ref CompilerResults compilerResults)
            {
                object proxyInstance = null;
               
                Uri address = new Uri("WSDL PAth");
                MetadataExchangeClientMode mexMode = MetadataExchangeClientMode.HttpGet;
                string contractName = "IService1";
                MetadataExchangeClient metadataExchangeClient = new MetadataExchangeClient(address, mexMode);
                metadataExchangeClient.ResolveMetadataReferences = true;
                MetadataSet metadataSet = metadataExchangeClient.GetMetadata();
                WsdlImporter wsdlImporter = new WsdlImporter(metadataSet);
                Collection<ContractDescription> contracts = wsdlImporter.ImportAllContracts();
                ServiceEndpointCollection allEndpoints = wsdlImporter.ImportAllEndpoints();
                ServiceContractGenerator serviceContractGenerator = new ServiceContractGenerator();
                var endpointsForContracts = new Dictionary<string, IEnumerable<ServiceEndpoint>>();
                foreach (ContractDescription contract in contracts)
                {
                    serviceContractGenerator.GenerateServiceContractType(contract);
                    // Keep a list of each contract's endpoints.
                    endpointsForContracts[contract.Name] = allEndpoints.Where(ep => ep.Contract.Name == contract.Name).ToList();
                }
                CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions();
                codeGeneratorOptions.BracingStyle = "C";
                CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("C#");
                CompilerParameters compilerParameters = new CompilerParameters(new string[] { "System.dll", "System.ServiceModel.dll", "System.Runtime.Serialization.dll" });
                compilerParameters.GenerateInMemory = true;
                compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, serviceContractGenerator.TargetCompileUnit);
                if (compilerResults.Errors.Count <= 0)
                {
                    Type proxyType = compilerResults.CompiledAssembly.GetTypes().First(t => t.IsClass && t.GetInterface(contractName) != null &&
                        t.GetInterface(typeof(ICommunicationObject).Name) != null);
                    // Now we get the first service endpoint for the particular contract.
                    ServiceEndpoint serviceEndpoint = endpointsForContracts[contractName].First();
                    // Create an instance of the proxy by passing the endpoint binding and address as parameters.
                    proxyInstance = compilerResults.CompiledAssembly.CreateInstance(proxyType.Name, false, System.Reflection.BindingFlags.CreateInstance, null,
                        new object[] { serviceEndpoint.Binding, serviceEndpoint.Address }, CultureInfo.CurrentCulture, null);
                }
                return proxyInstance;
            }

    Friday, November 29, 2013 5:35 AM

All replies

  • Hi,

    Could please tell me more about what do you want to do? I am sorry I am not clear about the question.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, December 3, 2013 7:20 AM
    Moderator