locked
Calling a Web Service using WCF without adding Service Reference RRS feed

  • Question

  • Hi,

    I have an application in .NET 2003 which calls various web services dynamically (without adding any web reference). I'm planning to migrate that to .NET 2008 and want to use WCF for calling the web services. Is there any way using WCF to call web services without the need to add a Service Reference.

    Thanks,

    Chendu.

    Wednesday, June 2, 2010 7:54 PM

Answers

  • Chendu,

    Using Add Service Reference is the easiest way to call a service when you are working with Visual Studio. If you still want to generate a proxy you can use the command-line tool svcutil. Otherwise you will have to work directly with the channel. For an example of how to do this see:  http://msdn.microsoft.com/en-us/library/ms751455.aspx

    Notice that the service contract must be defined in both the service and client. When using Add Service Reference in Visual Studio or the svcutil.exe tool, the proxy and service contract types are generated from metadata. If you don't download the service's metadata, you will need to have the service contract defined for the client.

    I hope this is helpful,

    Michael Green [MSFT]

    • Marked as answer by Bin-ze Zhao Thursday, June 10, 2010 5:34 AM
    Wednesday, June 2, 2010 8:11 PM
  • Hi,

    I don't think wcf can satisfy you in the way you described.

    Even if you have the web service address, how you are gonna generate the soap xml without the WS service reference since soap xml's imformation will be base on the service referece, if you know how the service is being defined, you can write same interface and binding information on your client side and use channel factory to realize the connection to the service.

    In wcf, ABC is critical for the communication between client and server which means you have to know the serivce address, binding information, contract priorly then you will be able to connect to the service.

    Thanks

    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, June 10, 2010 5:34 AM
    Wednesday, June 9, 2010 3:59 AM

All replies

  • Chendu,

    Using Add Service Reference is the easiest way to call a service when you are working with Visual Studio. If you still want to generate a proxy you can use the command-line tool svcutil. Otherwise you will have to work directly with the channel. For an example of how to do this see:  http://msdn.microsoft.com/en-us/library/ms751455.aspx

    Notice that the service contract must be defined in both the service and client. When using Add Service Reference in Visual Studio or the svcutil.exe tool, the proxy and service contract types are generated from metadata. If you don't download the service's metadata, you will need to have the service contract defined for the client.

    I hope this is helpful,

    Michael Green [MSFT]

    • Marked as answer by Bin-ze Zhao Thursday, June 10, 2010 5:34 AM
    Wednesday, June 2, 2010 8:11 PM
  • Hi Chendu

    In addition to Michael's suggestion, you can use a ChannelFactory<T> or ClientBase<T>. As Michael mentioned, you will still need to define the service contract and data contract for the web service you want to call.

    ChannelFactory:

    using(ChannelFactory<WebServiceContract> factory = new ChannelFactory<WebServiceContract>("EndpointName"))
    {
      WebServiceContract channel = factory.CreateChannel();
      string data = channel.GetData(1);
    }

    ClientBase:

    class MyProxy : ClientBase<WebServiceContract>, WebServiceContract
    {
      //constructors
      ...
      public string GetData(int value)
      {
       return this.Channel.GetData(value);
      }
    }

    Cheers

    Francois


    Please, mark the answer as helpful if it is. Thanks.
    Wednesday, June 2, 2010 9:05 PM
  • Thanks for the reply. The current state of my application is, it does not require any change if I have to call a new service or any changes to existing service because its not tightly binded with any service. I just pass the URL and the soap xml and it will call the service and return back the response xml. So I cannot add a service reference here. Also, I dont want to generate a proxy also. All I want is take the URL and Soap XML, connect to the service in the URL, pass the soap xml, get the response xml and just return that xml string to my client application.

    Can you suggest a way to do this with WCF? Also I have no control on the web service and also that is a Java web service and not .NET. Since you mentioned about the contract to be defined at service side also and its not possible, can you provide any work-around for that also.

    Thanks,

    Chendu.

    Thursday, June 3, 2010 3:06 PM
  • Thanks for the reply. The current state of my application is, it does not require any change if I have to call a new service or any changes to existing service because its not tightly binded with any service. I just pass the URL and the soap xml and it will call the service and return back the response xml. So I cannot add a service reference here. Also, I dont want to generate a proxy also. All I want is take the URL and Soap XML, connect to the service in the URL, pass the soap xml, get the response xml and just return that xml string to my client application.

    Can you suggest a way to do this with WCF? Also I have no control on the web service and also that is a Java web service and not .NET. Since you mentioned about the contract to be defined at service side also and its not possible, can you provide any work-around for that also.

    Thanks,

    Chendu.

    Thursday, June 3, 2010 3:06 PM
  • Hi,

    I don't think wcf can satisfy you in the way you described.

    Even if you have the web service address, how you are gonna generate the soap xml without the WS service reference since soap xml's imformation will be base on the service referece, if you know how the service is being defined, you can write same interface and binding information on your client side and use channel factory to realize the connection to the service.

    In wcf, ABC is critical for the communication between client and server which means you have to know the serivce address, binding information, contract priorly then you will be able to connect to the service.

    Thanks

    Binze


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Thursday, June 10, 2010 5:34 AM
    Wednesday, June 9, 2010 3:59 AM