how to consume wcf service contract with nested datacontract class

Juraj Mikulas 1 Reputation point
2022-01-01T09:56:07.407+00:00

How to get data from nested datacontract Itinerary class?
Code:

[ServiceContract]
[XmlSerializerFormat]
public interface IStockService
{

[OperationContract]
GetAirfareRequest GetAirfare(GetAirfareRequest request);
}
[MessageContract]
[KnownType(typeof(Itinerary))]
public class GetAirfareRequest
{
[MessageHeader(Name = "h1")] public DateTime date;
[MessageBodyMember] public Itinerary itinerary;
}
[DataContract]
public class Itinerary
{
[DataMember] public string fromCity;
[DataMember] public string toCity;
}
public class StockService : IStockService
{
public GetAirfareRequest GetAirfare(GetAirfareRequest request)
{
return request;
}
}
console app client side:

using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Text;
using System.Threading.Tasks;
using Client.ServiceReference1;

namespace Client
{
internal class Program
{
static void Main(string[] args)
{
StockServiceClient client = new StockServiceClient();
try
{
GetAirfareRequest getAirfareRequest = new GetAirfareRequest()
{
h1 = DateTime.Now, itinerary = (how to get toCity?), (how to get fromCity?)
};
GetAirfareRequest response = ((IStockService)client).GetAirfare(getAirfareRequest);
Console.WriteLine(response.ToString());
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.ReadLine();
client.Abort();
}
catch (FaultException unknownFault)
{
Console.WriteLine("An unknown exception was received. " + unknownFault.Message);
Console.ReadLine();
client.Abort();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
Console.ReadLine();
client.Abort();
}
}
}
}

Developer technologies | .NET | Other
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Lan Huang-MSFT 30,191 Reputation points Microsoft External Staff
    2022-01-04T09:39:34.937+00:00

    Hi @Juraj Mikulas ,
    I guess you want to get ii data, you can do it directly.

    try  
                {  
                    Itinerary ii = new Itinerary()  
                    {  
                        toCity = "ddd",  
                        fromCity = "ff"  
      
                    };  
                    GetAirfareRequest getAirfareRequest = new GetAirfareRequest()  
                    {  
                        itinerary = ii,  
                    };      
                     
                }  
    

    https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.messagebodymemberattribute?view=dotnet-plat-ext-6.0
    Best regards,
    Lan Huang


    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.

    0 comments No comments

  2. Juraj Mikulas 1 Reputation point
    2022-01-04T16:06:51.017+00:00

    thank you. Visual studio shows mistake. See it on picture.
    162246-img-20220104-170311-091.jpg


  3. Juraj Mikulas 1 Reputation point
    2022-01-05T05:01:44.123+00:00

    Thank you, I tried. VS shows only "GetAirfareRequest" in console.


  4. Juraj Mikulas 1 Reputation point
    2022-01-05T16:17:30.333+00:00

    Thank you for advice. Yes, there is value bud not in soap envelope. I think there is not incorporated client to retrieve and send data. May be data sould be given on service side. I will try work on it latter.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.