question

73898628 avatar image
0 Votes"
73898628 asked ZhiLv-MSFT answered

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

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








dotnet-csharpdotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SOAP services have a WSDL (Web Service Definition Language) file that defines the service methods and types. Use Visual Studio to create a service reference from the WSDL. This will generate code you can use to access the remote service as if it is a local method.

https://docs.microsoft.com/en-us/visualstudio/data-tools/how-to-add-update-or-remove-a-wcf-data-service-reference?view=vs-2022




0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
0 Votes"
ZhiLv-MSFT answered

Hi @73898628,

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


2.gif (335.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.