I am having troubles when I retrive data from Soap with C# ASP.NET

Bilbito Bolson 1 Reputation point
2022-05-19T13:16:15.137+00:00

Hi guys, wish you well,

I am sending from the controller an IdCostumer to the Soap Client and what I expect is to receive information of that costumer (If i send Idcostumer = 1, the soap brings me name, lastname, age, etc). For this I am creating a Dictionary, with TKeys with same names of the XML, and TValues as objects o empty (the error is the same). Then I want to insert those received values in a variable.

My problem happens when i want to send the dictionary to the client, I have the following error on last line:

System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'ServiceReference1.Family'.'

this is my code:

 FamilyClient client = new FamilyClient(binding, address);
IDictionary<string, object> params = new Dictionary<string, object>();

params.Add("Customerid", Customer.Customerid); 

params.Add("personalData", new Dictionary<string, object>()
{
     {
         "item", new Dictionary<string, object>()
              {
                   {"name", object.name1},
                   {"lastname", object.name2},
                   {"age", object.name3},
               }
                },
            });

            params.Add("FamilyData", new Dictionary<string, object>()
            {
                {
                    "item", new Dictionary<string, object>()
                    {
                        {"mother", object.name1},
                        {"father", object.name2},
                        {"sister", object.name3}
                    }
                },
            });

var response = client.FamilyClient((Family)params); //This is last line 

Is there other way to send the Custermer Id without a Dictionary? I am with these problem since last week and I am unable to resolve it

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,166 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,011 Reputation points Microsoft Vendor
    2022-05-20T02:11:02.857+00:00

    Hi @Bilbito Bolson ,

    System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'ServiceReference1.Family'.'

     IDictionary<string, object> params = new Dictionary<string, object>();    
    
        ...  
      var response = client.FamilyClient((Family)params); //This is last line   
    

    The error message is clear, from the above code, we can see that the params is a Dictionary<string, object>() type, you can't cast it to the Family type.

    Is there other way to send the Custermer Id without a Dictionary? I am with these problem since last week and I am unable to resolve it.

    It depends on the Soap service, if it has expose this method that only need the customer id, you can call it and provide the Custermer Id as the parameter, instead of the Dictionary. But from your code and description, it seems that at present the SOAP service method required a Family as the parameter. So, you need to convert the Dictionary to the Family object.

    Based on your code, to convert the Dictionary to the Family object, you can try to use the JsonSerializer.Serialize() method to convert the Dictionary to a Json string, then use the JsonSerializer.Deserialize() method to convert the Json string to the Family object.

    [Note] In this sample, I'm using the System.Text.Json.JsonSerializer class, you can also use the Newtonsoft.Json.JsonConvert class and the JsonConvert.SerializeObject() method and JsonConvert.DeserializeObject<T>() method.

    The code like this:

    Dictionary<string, object> paramsdata = new Dictionary<string, object>();  
    
    paramsdata.Add("Customerid", "1001");  
    
    paramsdata.Add("personalData", new Dictionary<string, object>()  
     {  
          {  
              "item", new Dictionary<string, object>()  
                   {  
                        {"name", "AA"},  
                        {"lastname", "A2"},  
                        {"age", 22},  
                    }  
          },  
    });  
    paramsdata.Add("FamilyData", new Dictionary<string, object>()  
                 {  
                     {  
                        "item", new Dictionary<string, object>()  
                        {  
                            {"name", "AA"},  
                            {"lastname", "A2"},  
                            {"age", 22},  
                        }  
                     },  
                 });  
    
    var jsonstring = System.Text.Json.JsonSerializer.Serialize(paramsdata);  
    //jsonstirn: {"Customerid":"1001","personalData":{"item":{"name":"AA","lastname":"A2","age":22}},"FamilyData":{"item":{"name":"AA","lastname":"A2","age":22}}}  
    var family = System.Text.Json.JsonSerializer.Deserialize<Family>(jsonstring);  
    
    //then call the service with family  
    //var response = client.FamilyClient(family);   
    

    The Family model: It is created based on the dictionary, if there have any difference, you can change the dictionary or the Family model.

    public class Family  
    {  
        public string Customerid { get; set; }  
        public PersonalData personalData { get; set; }  
        public FamilyData FamilyData { get; set; }  
    }  
    
    public class FamilyData  
    {  
        public Item item { get; set; }  
    }  
    
    public class Item  
    {  
        public string name { get; set; }  
        public string lastname { get; set; }  
        public int age { get; set; }  
    }  
    
    public class PersonalData  
    {  
        public Item item { get; set; }  
    }  
    

    The result like this:
    203952-2.gif


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments