ChatGPT解决这个技术问题 Extra ChatGPT

SOAP client in .NET - references or examples? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations. Closed last year. Improve this question

Background:

I am creating a webservices site which will provide many types of simple services over SOAP and possibly other protocols too. The goal is to make it easy to do for example conversions, RSS parsing, spam checks and many other types of work. The site will be targeted mostly at beginner developers.

My Problem:

I have never developed any C#, or .NET for that matter. I did hack some VB6 many years ago but that's it. Now I need some examples of doing RPC calls over SOAP in C#. I have tried to search the web, and Stack Overflow, to find this but didn't find many resources, and I have no idea how to rank the resources (which are old? which are incorrect? etc).

I have created a simple example service, which is called like this in PHP:

<?php
$client = new SoapClient('http://webservi.se/year'); //URL to the WSDL
echo $client->getCurrentYear(); //This method returns an integer, called "year"
?>

I now want to call this method as easily as possible in C#. All references and examples are very welcome. Where do I begin? Which classes/modules/whatever can I utilize?

The solution does not have to involve SOAP at all if there are better communication frameworks (the back end is meant to be extensible), but note that the server side is implemented in PHP on Unix so proprietary solutions from Microsoft are out of the question on the server side.

Note that I need this so I can write documentation possible for J. Random Web Developer to follow (even if they are on shared web hosting). I therefore think the best approach should be to do this in code only, but even other ways of doing this are of course welcome.

Is something preventing you from creating a service reference to the WSDL's URL in your project?
Frédéric, my main problem is that I have no idea how ASP.NET handles this. What is a "service reference"? is it possible to do on code only or do I have to point-and-click in some way? Note that I need this so I can write documentation possible for J. Random Web Developer to follow (even if they are on shared web hosting).
@Emil, there's pointing and clicking involved in order to generate the C# code for the classes described in the WSDL document. After that, there's not much code involved if the service's URL never changes. See this article on MSDN.

K
KyleMit

Prerequisites: You already have the service and published WSDL file, and you want to call your web service from C# client application.

There are 2 main way of doing this:

A) ASP.NET services, which is old way of doing SOA B) WCF, as John suggested, which is the latest framework from MS and provides many protocols, including open and MS proprietary ones.

Adding a service reference step by step

The simplest way is to generate proxy classes in C# application (this process is called adding service reference).

Open your project (or create a new one) in visual studio Right click on the project (on the project and not the solution) in Solution Explorer and click Add Service Reference A dialog should appear shown in screenshot below. Enter the url of your wsdl file and hit Ok. Note that if you'll receive error message after hitting ok, try removing ?wsdl part from url. I'm using http://www.dneonline.com/calculator.asmx?WSDL as an example Expand Service References in Solution Explorer and double click CalculatorServiceReference (or whatever you named the named the service in the previous step). You should see generated proxy class name and namespace. In my case, the namespace is SoapClient.CalculatorServiceReference, the name of proxy class is CalculatorSoapClient. As I said above, class names may vary in your case. Go to your C# source code and add the following using WindowsFormsApplication1.ServiceReference1 Now you can call the service this way. Service1Client service = new Service1Client(); int year = service.getCurrentYear();

Hope this helps. If you encounter any problems, let us know.


Thanks for your answer. I really appreciate the screenshots and the theory description. Even if the theory may be easy, it's still a new environment for me. I'll try this out.
No problem :) Trust me, it's easy in practice too. and if you'll have any problem, SO is for helping each other right? :-)
Emil, you might be interested to know that underneath the GUI screens displayed above, the program wsdl.exe is doing the heavy lifting. You can run this yourself from the command-line and see the code that is generated for you.
As noted below by another commenter, in C# Windows Communication Foundation (wcf) represents both the technology for hosting services and calling services. The above shows you how to automatically create WCF proxies in Visual Studio, and is one of many ways to do this, including creating proxies using wsdl.exe as noted by mtutty.
I cant seem to find Service1Client service = new Service1Client(); In my serviceReference Name space there are just WSDL classes?
m
mtutty

I have done quite a bit of what you're talking about, and SOAP interoperability between platforms has one cardinal rule: CONTRACT FIRST. Do not derive your WSDL from code and then try to generate a client on a different platform. Anything more than "Hello World" type functions will very likely fail to generate code, fail to talk at runtime or (my favorite) fail to properly send or receive all of the data without raising an error.

That said, WSDL is complicated, nasty stuff and I avoid writing it from scratch whenever possible. Here are some guidelines for reliable interop of services (using Web References, WCF, Axis2/Java, WS02, Ruby, Python, whatever):

Go ahead and do code-first to create your initial WSDL. Then, delete your code and re-generate the server class(es) from the WSDL. Almost every platform has a tool for this. This will show you what odd habits your particular platform has, and you can begin tweaking the WSDL to be simpler and more straightforward. Tweak, re-gen, repeat. You'll learn a lot this way, and it's portable knowledge.

Stick to plain old language classes (POCO, POJO, etc.) for complex types. Do NOT use platform-specific constructs like List<> or DataTable. Even PHP associative arrays will appear to work but fail in ways that are difficult to debug across platforms.

Stick to basic data types: bool, int, float, string, date(Time), and arrays. Odds are, the more particular you get about a data type, the less agile you'll be to new requirements over time. You do NOT want to change your WSDL if you can avoid it.

One exception to the data types above - give yourself a NameValuePair mechanism of some kind. You wouldn't believe how many times a list of these things will save your bacon in terms of flexibility.

Set a real namespace for your WSDL. It's not hard, but you might not believe how many web services I've seen in namespace "http://www.tempuri.org". Also, use a URN ("urn:com-myweb-servicename-v1", not a URL-based namespace ("http://servicename.myweb.com/v1". It's not a website, it's an abstract set of characters that defines a logical grouping. I've probably had a dozen people call me for support and say they went to the "website" and it didn't work.

</rant> :)


Good stuff! It's not what I asked for, but you still answered a lot of questions I've thought about myself. "Contract first" seems to be the key even in my situation here.
J
John Saunders

If you can get it to run in a browser then something as simple as this would work

var webRequest = WebRequest.Create(@"http://webservi.se/year/getCurrentYear");

using (var response = webRequest.GetResponse())
{
    using (var rd = new StreamReader(response.GetResponseStream()))
    {
        var soapResult = rd.ReadToEnd();
    }
}

j
jesusduarte

Take a look at "using WCF Services with PHP". It explains the basics of what you need.

As a theory summary:

WCF or Windows Communication Foundation is a technology that allow to define services abstracted from the way - the underlying communication method - they'll be invoked.

The idea is that you define a contract about what the service does and what the service offers and also define another contract about which communication method is used to actually consume the service, be it TCP, HTTP or SOAP.

You have the first part of the article here, explaining how to create a very basic WCF Service.

More resources:

Using WCF with PHP5.

Aslo take a look to NuSOAP. If you now NuSphere this is a toolkit to let you connect from PHP to an WCF service.


J
John Saunders

You're looking in the wrong place. You should look up Windows Communication Framework.

WCF is used both on the client and on the server.


E
Eduardo Lago Aguilar

Here you can find a nice tutorial for calling a NuSOAP-based web-service from a .NET client application. But IMO, you should also consider the WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) for servicing. See WSO2 Web Services Framework for PHP 2.0 Significantly Enhances Industry’s Only PHP Library for Creating Both SOAP and REST Services. There is also a webminar about it.

Now, in .NET world I also encourage the use of WCF, taking into account the interoperability issues. An interoperability example can be found here, but this example uses a PHP-client + WCF-service instead of the opposite. Feel free to implement the PHP-service & WFC-client.

There are some WCF's related open source projects on codeplex.com that I found very productive. These projects are very useful to design & implement Win Forms and Windows Presentation Foundation applications: Smart Client, Web Client and Mobile Client. They can be used in combination with WCF to wisely call any kind of Web services.

Generally speaking, the patterns & practices team summarize good practices & designs in various open source projects that dealing with the .NET platform, specially for the web. So I think it's a good starting point for any design decision related to .NET clients.