Implementing a SOAP client in C# with WCF and .NET 4.0

25,662

The quickest way to do this is to do right-click "Add Service Reference" in your client's project under References. Point the dialogue at the location of the WSDL and click Go:

enter image description here

The URL I used was for the style of a .NET service reference, you'll need to replace with whatever your PHP SOAP service uses to expose its WSDL.

Doing this will generate a client side proxy you can instantiate to communicate with your web service.

To access the service you can then do something like (although your specific implementation won't be the same):

MyService.MyWebServiceSoapClient ws = new MyService.MyWebServiceSoapClient();
string result ws.DoThing();

Pay particular attention to the SoapClient part of the proxy class name, this is added to the name of the soap service name by the proxy code generator.

The proxy generator will also create all the necessary configuration in your web.config or app.config file.

Share:
25,662
Skalli
Author by

Skalli

I'm a software developer with experience in .Net, especially C# and VB.Net. Web technology like SOAP, REST and so on is part of my daily work too. Besides .Net I focus on modern C++. A special interest of mine is 3D game engine programming.

Updated on January 30, 2020

Comments

  • Skalli
    Skalli over 4 years

    I'm having trouble getting started with WCF in .NET 4.0. This is my situation:

    I have created a small SOAP Server in PHP. I have a C# project in which I want to connect to this Server and initiate the SOAP communication.

    My problem is, I have no idea how to do this in C#. I can't find the right introduction to WCF. There are ways to do this. But I can't find the right classes and references to add to my C# project. Are the any tutorials how to achieve this in C#? I searched a lot and found nothing which helped me.

    I want to load the WSDL from my SOAP Server at run time, make the SOAP request, retrieve the answer and be done. But where can I get started? The MSDN site about WCF is only confusing me more.

    Edit: It probably is not necessary to fetch the WSDL file at run time. So that is not needed anymore.
    I used svcutil to create the class and embedded it in my project. I haven't been able to test it yet, because I have some trouble with the MySQL database (It's running and accessible from the mysql command line tool or mysqladmin, but I can't connect to it with any other programm...). I'll report back as soon as I know if it works.

    Edit 2: I've followed Kevs approach and it worked out very good in the end. My final problem was, that I used the Service Class in a DLL. I needed the app.config in the programm which used the DLL too. After I did that it worked out well.

    • Thorarin
      Thorarin about 13 years
      This is not something you would typically do in .NET. Normally, you would import the WSDL at development time and have it generate a client proxy by adding a service reference. Why do you need to fetch the WSDL at runtime?
    • Skalli
      Skalli about 13 years
      After doing more research I think it's not really necessary. I guess I won't need it and it doesn't work easy that way.
  • Thorarin
    Thorarin about 13 years
    While I agree that this is the quickest way to consume the service, the OP specifically requested loading the WSDL at runtime.
  • Kev
    Kev about 13 years
    @thorarin - whilst that may be true, he may also be thinking that the soap classes are built dynamically (kinda like late bound VB6/VBScript SOAP SDK style) every time you need to use the endpoint and may not be aware that he can generate a proxy just once.
  • Skalli
    Skalli about 13 years
    It is true that I was looking at run time generation. But I spend more time on research and I think I'll go with the import. Another note: The Add Service Reference didn't work like that in my case. I had to go through the 'Advanced' dialog and Add Web Reference. It said .NET 2.0 compatible. Didn't look optimal to me.
  • Kev
    Kev about 13 years
    @skalli - can you post your WSDL? There may be something in there that the WCF proxy generator isn't liking?
  • Skalli
    Skalli about 13 years
    @Kev: Found it today, had a small error in the WSDL which I overlooked. Now I can create the Service reference.
  • Skalli
    Skalli about 13 years
    I'm still having an issue. Everytime I instantiate the class with VersionService.VersionServiceSoapClient version = new VersionService.VersionServiceSoapClient(); it says that it can't find the standard endpoint element. I'm unsure how to handle this.
  • Kev
    Kev about 13 years
    @skalli - can you paste the wsdl somewhere? These problems tend to be WSDL related rather then VS issues, VS is pretty good a consuming WSDL and doing the right thing
  • Skalli
    Skalli about 13 years
    @Kev: Sure, I'm using this one: m3b-lockhausen.dyndns.org/UpdateServer/WSDL/UpdateServer.wsd‌​l. I hope this helps.
  • Kev
    Kev about 13 years
    @skalli - odd, works for me just fine. Do you have a valid parameter I can pass to get a result back?
  • Skalli
    Skalli about 13 years
    @Kev: Yes, you can use "Businessplan". It's currently the only working parameter. You can also test it here. I've added the WSDL as a Service Reference and then used the class as described earlier. Do you need any additional information?
  • Kev
    Kev about 13 years
    @skalli - just tried from here using: string minor, build; VersionServiceSoapClient sc = new VersionServiceSoapClient(); sc.getVersion("Businessplan", out minor, out build); and it returned minor=0, major=1. No exceptions.
  • Skalli
    Skalli about 13 years
    @Kev: Very strange. I'm still having the same problem, but use the same code as you do... I'll edit the first post on Monday with more code, maybe I've done something wrong. Thanks for the help so far!
  • Kev
    Kev about 13 years
    @skalli - what version of VS are you using and have you installed SP1?
  • Skalli
    Skalli about 13 years
    @Kev: I'm using VS 2010 Professional without SP1. I guess I'll install it on monday.
  • Skalli
    Skalli about 13 years
    I'm at home now and I'm just loading our repository... I'm curious if it works here.
  • Skalli
    Skalli about 13 years
    Tested it: Same problem here. So either I missed something, or something didn't work when I added the Service Reference. :-/
  • Skalli
    Skalli about 13 years
    @Kev: Ok, I've figured it out. I'll edit the opening post. Thanks for your help!