Search code examples
c#wcfexplicit-interface

How to access complex types Argument in C#


I am trying to use one WCF service, since we don't have the service URL we got XSD and WSDL. At this point of time trying for POC stuff for that. Using svcutil tool genarated class file. I am not very much familiar with WCF stuff, so first trying to use these classes in C#.

Below is my complete code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace smsclient
{
    public interface ISendSms
    {
    sendMessageResponse1 sendMessage(sendMessageRequest request);
    }

    public class AccessSMSDetails: ISendSms
    {

  sendMessageResponse1 ISendSms.sendMessage(sendMessageRequest request)
        {
           //Here is my implementation code.          
            sendMessageResponse sresponse = new sendMessageResponse();
            sresponse.messageid = 1;
            sresponse.recipient = "Chiranjeevi";
            sresponse.reference = "reference";
            sresponse.status = "sucsesss";

            sendMessageResponse1 sresponse1 = new sendMessageResponse1(sresponse);
            return sresponse1;

        }
    }

    public class Program 
    {
        static void Main(string[] args)
        {
            sendMessage sm = new sendMessage();
            sm.content = "Content";
            sm.destination = "Destination";
            sm.reference = "reference";
            sendMessageRequest sRequest = new sendMessageRequest(sm);
            sendMessageResponse1 sclient = new sendMessageResponse1();
            AccessSMSDetails asms = new AccessSMSDetails();
          //sclient=            
          // Here I am not getting the interface Method name to call. Please correct Me if this approach is wrong.
        }
    }
}

In the last line I'm unable to call the interface method.


Solution

  • You are using explicit interface implementation. Hence you need to cast the asms variable to the respective interface type for it to work:

    ((ISendSms)asms).sendMessage(…);
    

    Alternatively, if possible in your case, use a non-explicit implementation:

    public class AccessSMSDetails: ISendSms
    {
        public sendMessageResponse1 sendMessage(sendMessageRequest request)
        {
        }
    }
    

    and then you can call the method as usual:

    asms.sendMessage(…);