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:
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