Search code examples
c#asp.netwcf

How to consume WCF service


i am new to C# development, as a first task i got assigned with WCF service, and tried some samples in code project. Now i am working with complex types, but unable to get the response. from the wsdl file and xsd using svcutil.exe wsdlname xsd and got two files based on those files created one service in local and trying to consume in the below way.

Below is Interface

I am able to run this service able to see the service url and able to refer in my client.

Below is i am trying to call the service from client to this stub, but not able to get the idea ,how to call

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.ServiceModel;

    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                 ServiceReference1.sendMessageResponse1 s = new
ServiceReference1.sendMessageResponse1();
                 ServiceReference1.sendMessageResponse1 s1 = new ServiceReference1.sendMessageResponse1();
                 //s1.messageid = 1;
                 //s1.recipient = "Chiranjeevi";
                 //s1.status = "Sent";
                 //ServiceReference1.sendMessageResponse ss= (ServiceReference1.sendMessageResponse) s1;
                 Console.Read();            
             }
        }
    }

But while trying to call the service i am not getting any out put for default constructor also. tried with console.writeline();

I am trying to call the sendMessageResponse1 sendMessage(sendMessageRequest request); from service.

getting the below error, while trying to call the above method. Error 1 Cannot convert type 'ConsoleApplication3.ServiceReference1.sendMessageResponse1' to 'ConsoleApplication3.ServiceReference1.sendMessageResponse'


Solution

  • Well, you're trying to convert an instance of one class to an instance of another.

    ServiceReference1.sendMessageResponse1 s = 
        new ServiceReference1.sendMessageResponse1();
    ServiceReference1.sendMessageResponse1 s1 = 
        new ServiceReference1.sendMessageResponse1();
    
    //s1.messageid = 1;
    //s1.recipient = "Chiranjeevi";
    //s1.status = "Sent";
    //ServiceReference1.sendMessageResponse ss=
    //    (ServiceReference1.sendMessageResponse) s1;
    

    The following line of code will break, because there is not a conversion between class sendMessageRequest and sendMessageRequest1.

    ServiceReference1.sendMessageResponse ss=
        (ServiceReference1.sendMessageResponse) s1;
    

    Here is the deal:

    • s1 is of type sendMessageResponse1.
    • Your trying to cast it into a type of sendMessageResponse.
    • That isn't going to work, because the former isn't derived from the latter.

    That's exactly what the error is telling you:

    Error 1 Cannot convert type 'ConsoleApplication3.ServiceReference1.sendMessageResponse1' to 'ConsoleApplication3.ServiceReference1.sendMessageResponse'