简体   繁体   中英

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.

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:

在此输入图像描述

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM