How to create a SOAP Request C# - c#

I know there are thousands of step-by-step samples for consuming a SOAP project with C#.
Actually, I tried many of those but didn't really get to understand how it works, how were needed components built or how to integrate a certificate for a secure connection.
I would be really grateful if any of you guys have a magical resource, since I read somewhere I shouldn't do it this way or another, I really want to know this well.
Thank you,
Best regards.

It's really simple actually. Suppose you have a service at http://myhost.com/XService.svc - right click References in the solution explorer and choose "Add a service reference" (older versions of VS called it web reference/web service)
A wizard appears; pop your URL into it, set a few options and hit Go
You'll get a client class set created that references the service, with methods that take a set of typed parameters based on what the service said it wanted when VS queried its WSDL. You might use it like:
var c = new XServiceClient();
bool result = c.CreateNewPerson("John Smith", 30, "js#hotmail.com");
The service client handles all of the xml creation, tcp socket connection, data transmission etc necessary to pass those 3 values you sent, to the web service so that the relevant method is called, the response is returned etc

Related

C# Calling Web Services to different URLs

I'm new to Web Services from C#, but have worked C# for years, just never needed to use Web Services. Due to privacy issues, I can't disclose actual URL, but there is a test server and a production server where the web services are identical in all other respects, and the services were written / managed by another entity.
https://LiveSite.SomeDomain.com/FolderInWebSite/TestWebServiceSoapHTTP
and
https://TestSite.SomeDomain.com/FolderInWebSite/TestWebServiceSoapHTTP
Do I need to create two separate web references to the project and create different instances of them to go, or can I via some property just change which URL version it is sending data to.
Additionally, not being familiar working web services, I see the classes as Visual Studio imported. I can create instances of the classes and set the applicable properties (int, dates, strings, string[] arrays, etc). But not seeing how to actually say ... Go send it now. and then getting the response back.
I've done this from an older application with another language and was doing direct with HTTP and SOAP where I was able to make my own connection to the URL, build the body of the SOAP message, then send it.
Just use the "Url" property.
var myProxy = new MyProxy();
myProxy.Url = "http://foo.com/myservice";
Edit for second part of the question:
There should be a method for each action exposed the API that you can call. For example if the API exposes a MyAction that takes a string, the code generator should have generated a method that you can use like so:
myProxy.MyAction("hello");

c#: How to consume webservice methods whose address is unknown at compile-time

I am consuming a webservice from a Java server. The webservice provides me with some methods that need. Up till now I have been using the method where I added the reference of the webservice in the project explorer, typed the address of the webservice and compiled it. But now I need it to pick up the address of the service at runtime from an xml file or something! is that possible?
There is Url property in generated proxy object that you can set at runtime. Covered in Creating the Web Service Proxy article on MSDN.
Sample from the article (shows how to also set credentials, you may also need to set Proxy):
var rs = new ReportExecutionService();
rs.Url = "http://<Server Name>/reportserver/reportexecution2005.asmx?wsdl";
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
Note that above answer is for case when you don't know exact Url of server till runtime, but you have WSDL/sample server available at design time and able to generate proxy via add web service in VS (or manually).
Alternatively you can call service via other classes implementing "HTTP GET" like HttpClient and configure anything you want, but will need to do your own parsing of results.

Web API to insert records

I am working on Taleo web API. I have an XML file with several records that I will have to insert into the Taleo system using its web API.
I have got the Taleo API guide from http://tbe.taleo.net/products/TBE_API_Guide.pdf.
For the first time, I am working on web API so I don't know where to start. Although the guide shows some examples using SOAP, I don't know how to make this request and retrieve the response using C# or VB.NET. I googled it but didn't get much information about it. If you share your ideas, I would really appreciate it.
From the PDF, it appears the WSDL for the service is at: http://tbe.taleo.net/wsdl/WebAPI.wsdl. In your C#/VB project, use the "Add Service Reference" option on the context menu and supply that URI to the WSDL. That will generate a proxy and many ancillary classes on which you can call whatever methods you need - the SOAP details are under the covers. You'll instantiate an instance of WebAPIClient and should see all the relevant methods there.
For example, in C# after creating a new Service Reference with (uninspired) namespace name of ServiceReference2, I can code the following (though I have no idea what it does!):
var x = new ServiceReference2.WebAPIClient(); // I suspect there's an overload expecting credentials
ServiceReference2.AccountBean y = x.getAccountById("bar", 0);
Exception handling is left to the reader :)

Creating SOAP message body

I would like to know how to create SOAP message, body, and envelope in C#. Any help or links appreciated.
I need to send a SOAP attachment to a third party Web Service. I don't need WCF. I know how it works. My client needs SOAP with attachment.
Here's a really super-short intro how to do this:
1) Create a new project (any kind - console app, windows app, web app - whatever) - File > New > Project
2) In your Solution Explorer, right-click on References and choose Add Service Reference
3) In the dialog box that pops up, you need to enter two things:
your URL where the service lives (typically with a ?wsdl query string to grab the WSDL - the service description)
your namespace where the service classes will live - pick whatever suits you
Then click on Go - this will talk to that service and see what it has to offer
4) Now, that dialog box should update, and show you the service and its operations, as discovered by Visual Studio:
5) Click on OK and some code gets generated in the background
6) Now instantiate a client-side proxy in your code, and call a method on it:
That's all you have to do - everything else, all the messy details of creating a SOAP header and message body, can be happily left to the WCF runtime.
Now go learn WCF!
marc_s: learn SOAP with Attachments before recommending others to learn WCF. SwA is not supported by .Net so he's got to roll his own and that is the background for his question.
Check this link out http://www.xefteri.com/articles/show.cfm?id=15
It describes process for VS.NET, but in VS2010 it is same process. This was an easiest way.
However, if you can construct SOAP message (for example, if you read WSDL and can construct message without any issues or you used something like SOAP UI (http://www.soapui.org/) to generate few mock up messages and got an idea) then you can simply do POST to that URL like in this example http://www.808.dk/?code-csharp-httpwebrequest

C# SOAP with a custom URL

Okay, simple situation: I'm writing a simple console application which connects to a SOAP web service. I've imported a SOAP Service reference and as a result, my application has a default endpoint build into it's app.config file.
The web service, however, can run on multiple servers and the URL to the proper web service is passed through the commandline parameters of my application. I can read the URL, but how do I connect the web service to this custom URL?
(It should be very simple, in my opinion. It's something I'm overlooking.)
Is this using an auto-generated class deriving from SoapHttpClientProtocol? If so, just set the Url property when you create an instance of the class.
Well, .NET can provide some very useless error messages sometimes. In IIS, the service was configured to AutoDetect cookieless mode. As a result, I had to append "?AspxAutoDetectCookieSupport=1" to the URL. Although that would fix the problem, it was just easier to go to the IIS console, open the properties of the service, go to the ASP.NET tab page, click the "Edit configuration" button, to to "State Management" in the newly popped up screen and change "Cookieless mode" into something other than "AutoDetect"...
Excuse me. Dumb error. Am going to hit myself on the head a few times for this. ;-)
As Jon said, you set the Url, as in:
Namespace.ClassName nwe = new Namespace.ClassName();
nwe.Url = "http://localhost/MyURL/site.asmx";

Categories