Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 203
Skalli Avatar asked May 12 '11 09:05

Skalli


People also ask

How do you structure a SOAP document?

A SOAP message is encoded as an XML document, consisting of an <Envelope> element, which contains an optional <Header> element, and a mandatory <Body> element. The <Fault> element, contained in <Body> , is used for reporting errors.

What is SOAP implementation?

SOAP (formerly a backronym for Simple Object Access Protocol) is a messaging protocol specification for exchanging structured information in the implementation of web services in computer networks.

How do I create a SOAP request and response in C #?

To make SOAP requests to the SOAP API endpoint, use the "Content-Type: application/soap+xml" request header, which tells the server that the request body contains a SOAP envelope. The server informs the client that it has returned a SOAP envelope with a "Content-Type: application/soap+xml" response header.

What is SOAP in C sharp?

SOAP (Simple Object Access Protocol) is a simple solution for interaction of different applications built in different languages and running on different platforms as it uses HTTP as its transport and XML as its payload for sending and receiving messages.


1 Answers

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.

like image 159
Kev Avatar answered Sep 28 '22 01:09

Kev