0

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.

Chiranjeevi
  • 161
  • 1
  • 4
  • 22

2 Answers2

0

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(…);
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • @Tunchy: If i add below line getting exception. sclient= ((ISendSms)asms).sendMessage(sRequest); Error 1 'smsclient.AccessSMSDetails' does not implement interface member 'smsclient.ISendSms.sendMessage()' line no :169 18 smsclient – Chiranjeevi Apr 21 '15 at 11:14
  • Why to cast when we can declare! – Amit Apr 21 '15 at 11:15
  • @Amit Because he may need to access the full class, not just the interface… – Ondrej Tucny Apr 21 '15 at 11:15
  • May is good thing but current class contains only interface implementation. – Amit Apr 21 '15 at 11:16
  • @Chiranjeevi Then you are, likely, not showing the complete, up-to-date code in your question. – Ondrej Tucny Apr 21 '15 at 11:17
  • @Tunchy:no this is the only code i am using, actually this is genarated code from wsdl and xsd for wcf service, to create a client for understanding made this class structure.When I will consume i need only interface method in my client. – Chiranjeevi Apr 21 '15 at 11:20
  • The code in from your question with this exact line: `((ISendSms)asms).sendMessage(null);` added instead of the `// Here I am not getting …` comment *just compiles **fine***. – Ondrej Tucny Apr 21 '15 at 11:23
  • 1
    @Tunchy: Thank you so much it worked for me, now i will try to create Wcf service and client with this I hope.Thank you again. – Chiranjeevi Apr 21 '15 at 11:32
0

Try with

ISendSms asms = new AccessSMSDetails();
asms.sendMessage(request);
Amit
  • 882
  • 6
  • 13