Answered Dynamically invoking webservice, return type dataset doesnot work

  • Tuesday, August 07, 2012 2:44 AM
     
      Has Code

    Hi all,

    I am dynamically invoking a webservice using the below code. The code works if the return type is of String, int... But i am having if my return type (custom datatype it doesnot work).

    I have a web service method that returns me a object instance of this AllData class (This is defined in the service)

    public class AllData : System.Data.DataSet
    
    {
    	.........
    }
    

    The code i use :

    [SecurityPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
            internal static object CallWebService(string webServiceAsmxUrl, string serviceClassName, string serviceMethodName, object[] parameters)
            {
                try
                {
                    System.Net.WebClient client = new System.Net.WebClient();
                    client.Credentials = CredentialCache.DefaultCredentials;
                    // Connect To the web service
    
                    System.IO.Stream stream = client.OpenRead(webServiceAsmxUrl + "?wsdl");
    
    
                    // Now read the WSDL file describing a service.
                    ServiceDescription description = ServiceDescription.Read(stream);
    
    
                    ///// LOAD THE DOM /////////
                    // Initialize a service description importer.
                    ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
                    importer.ProtocolName = "Soap12"; // Use SOAP 1.2.
                    importer.AddServiceDescription(description, null, null);
    
                    // Generate a proxy client.
    
                    importer.Style = ServiceDescriptionImportStyle.Client;
    
                    // Generate properties to represent primitive values.
    
                    importer.CodeGenerationOptions = System.Xml.Serialization.
                    CodeGenerationOptions.GenerateProperties;
    
                    // Initialize a Code-DOM tree into which we will import the service.
                    CodeNamespace nmspace = new CodeNamespace();
                    CodeCompileUnit unit1 = new CodeCompileUnit();
                    unit1.Namespaces.Add(nmspace);
    
                    // Import the service into the Code-DOM tree. This creates proxy code that uses the service.
                    ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit1);
                    if (warning == 0) // If zero then we are good to go
                    {
                        // Generate the proxy code
                        CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");
                        // Compile the assembly proxy with the appropriate references
                        string[] assemblyReferences = new string[5] 
                        {   "System.dll", 
                            "System.Web.Services.dll", 
                            "System.Web.dll", 
                            "System.Xml.dll", 
                            "System.Data.dll" 
                        };
                        CompilerParameters parms = new CompilerParameters(assemblyReferences);
                        CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
                        // Check For Errors
                        if (results.Errors.Count > 0)
                        {
                            return null;
                        }
    
                        object wsvcClass = results.CompiledAssembly.CreateInstance(serviceClassName);
                       
                        PropertyInfo pic = wsvcClass.GetType().GetProperty("Credentials");
                        pic.SetValue(wsvcClass, System.Net.CredentialCache.DefaultCredentials, null);
    
                        MethodInfo mi = wsvcClass.GetType().GetMethod(serviceMethodName);
    
                        // Finally, Invoke the web service method
                        var obj =  mi.Invoke(wsvcClass, parameters);
                        return obj;
                    }
                    else
                    {
                        return null;
                    }
                }
                catch (Exception)
                {
                    return null;
                }
            }

    When i look at the return object it gives me a XMLElement (Base type of XmlLinkedNode) of the schema. This doesnot contain any data.

    If I try to convert the XmlLinkedNode and then convert to dataset it creates the dataset but the dataset doesnot have any data.

    var linkedNode = (XmlLinkedNode)gt;
    
                DataSet ds = new DataSet();
                using (XmlNodeReader reader = new XmlNodeReader(linkedNode))
                {
                    ds.ReadXml(reader);
                }

    Any ideas how i can get the data .

    - Girija

All Replies

  • Tuesday, August 07, 2012 11:52 AM
     
     

    Its not ideal to use DataSet as DataMember in WCF - good reasons well described at http://msdn.microsoft.com/en-us/library/aa347876.aspx

    However serialize attribute on top of DataMember of type DataSet in DataContract should help

  • Tuesday, August 07, 2012 6:29 PM
     
     

    Hi,

    The webservice is already available and I am not the one who designed it. But I have to consume it dynamically in my client code.

    However serialize attribute on top of DataMember of type DataSet in DataContract should help

    I am sorry but i dodnot get how the serialize attribute would help me .

    - Girija

  • Wednesday, August 08, 2012 3:35 AM
    Moderator
     
     

    Hi Girjia,

    "When i look at the return object it gives me a XMLElement (Base type of XmlLinkedNode) of the schema. This doesnot contain any

    data."First, make sure that the data have been return properly. Please use fiddler to monitor the data.

    More, how about use Json.Net to format the type at server to a Json format string.(as you have mentioned that string is ok). And then deserialize at client side.

     

  • Wednesday, August 08, 2012 6:37 PM
     
     

    Hi,

    As i said i donot find any data returned. It just returns me the schema.

    If i use the Webreference and pass teh same parameters it gives me a dataset with values, but using dynamic proxy it gives me only teh schema in this line :

    var obj =  mi.Invoke(wsvcClass, parameters);

    - Girija


  • Friday, August 10, 2012 1:43 AM
    Moderator
     
     Answered

    >As i said i donot find any data returned. It just returns me the schema.

    Hi,

    Please check if error is caused from service side., use fiddler or WCF logging to capture the service response message.
    If possible, please let me know the service return object sample, and client/service side config file.

    I'll try to reproduce the problem at my side.

    Edit:

    >The code works if the return type is of String, int... But i am having if my return type (custom datatype it doesnot work).

    How about try to convert the custom datatype object to byte[], then return the byte[] to the client side.