Sending SOAP requests in C# - c#

I am trying to consume a SOAP service in C#, so I added my WSDL as a Service Reference. So far, I have created an instance of the request I want to send, but I don't know how to send it, or process the response.
Can someone explain how to do this?

When you added the service reference, Visual Studio should generate some code for you, including a class for the service which is in its own namespace.
So, you need to create a new instance of this service:
var oService = new ServiceNamespace.ServiceClient();
Then you can call your methods on the service:
oService.SomeMethod();

here you can find the full documentation and sample:
http://msdn.microsoft.com/en-us/library/aa529276.aspx

Here is a full example of How you create a WebService and How to consume it. As I see you just need the part of how to consume it. But it is like a normal call function you send parameters and receieve a result parsed to an object. Sometimes Value Objects created by the Service Reference Tool. Hope it helps.
By the way it uses the Web reference, with a service reference is quite similar just the name of your Class is parsed with a SoapClient at the End, lets say that your service is named Foo, the Service reference will generate it for you like FooSoapClient

Related

Connecting to website's SOAP API with C#

I'm trying to connect to the a provider website which has a soap based API. I'm new to C# so I am not really sure how to do it. I've been googling around like mad but I am getting nothing but errors. Here is what I've done.
I've added the URL (ex:http://www.companysite.com/api/api.cfc?wsdl) as a web service inside my .net solution and gave it a namespace of CompanyAPI.
If i right click on the service reference inside the solution explorer window, then go to object explorer. I can see all of the available methods.
When i try to instantiate it is when I get errors like cannot instantiate an instance of abstract class.
'CompanyApi.Api MBApi = new MyApp.CompanyApi.Api();'
I have no clue what i am doing wrong. Thanks in advance for any help.
When you add a Service Reference in Visual Studio, it creates an interface to represent the service contract and a client class which implements the interface. You are probably attempting to create an instance of the interface directly, which is not allowed; you should create an instance of the client. The client should be named something like ApiClient, in which case you can do the following:
CompanyApi.ApiClient MBApi = new MyApp.CompanyApi.ApiClient();

Magento Go C# requesting API fails to initialize

I am trying to connect to Magento API using C#. I am using Magento Go service and from what I've read I am able to use their API - I hope I am not wrong here. So here is what I did:
I added a Service Reference to http://mydomain.gostorego.com/api/v2_soap?wsdl=1, and just adding a service worked fine. Now I created a test class with GetStuff() method, which looks like this:
using ww.Feeds.MagnetoGoService;
public static string GetStuff()
{
MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient s = new MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortTypeClient();
var login = s.login("username here", "key here");
return login.ToString();
}
When I run the program I get an error in first line saying:
Could not find default endpoint element that references contract 'MagnetoGoService.Mage_Api_Model_Server_V2_HandlerPortType' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Any ideas what this may be? Do I have to set something up in my Magento Go settings? Or maybe using Magento Go is not allowing API access?
Thanks a lot.
Forget SOAP with c# you will pull your hair out. Download Charls Cook's xml-rpc api libary for c# and use the xml-rpc method. You won't get all the snazzy intellisense but at least it will work. There's also c# solution from ez.newsletter they released with Cook's library demonstrating how to use 80% of the magento api calls.
Cook's library xml-rpc.net
http://www.xml-rpc.net/
ez.newsletter solution
http://code.google.com/p/csharlibformagexmlrpcapi/
If anyone ever has problems with this, my solution was this:
I used the reference in one project, but I actually called the class and had main program in another project. You need your Service reference to be in each project wherever you're using it. That fixed it! Alternatively you can create a new BasicHttpBinding() and putt all the options from app.config/web.config into that binder, then you don't need to reference to Service everywhere. I hope that helps!

I am trying to consume a PHP SOAP service from C# and create a class wrapper in VS 2010

I've been tasked with creating a class wrapper for a SOAP service, the idea is that you'll be able to treat it as a regular class. The main reason for this is that the WDSL for the SOAP service contains only one method and it's got 5 parameters and it's only kind of OO so you'd have to know all the method calls really well and it's a bit hard to remember them all.
OK, so I've tried adding a web reference, now web references can now be added as service references in VS 2010. You click add service reference advanced etc and it puts in a service reference. Great. Unfortunately if I try and access this from a class I can't.
I can build a console app and put code in the main procedure and access the method of the SOAP service fine but when I add a reference to a class library the intellisense won't allow me to select anything. I'd instantiate an instance like so:
SOAPService.webServiceService ws = new SOAPService.webserviceService();
ws.
and then the intellisense refuses to kick in. If I do the same in a web project or a console app then I can access it fine. I've added the namespace I've done all kinds of things. Also, I can add a web reference and get a DISCO file whenever I create a web project.
OK, also while I'm on the subject I also need to pass credentials to the web service in PHP.
The problem is that in the past I'd create some .net system credentials and add these and it would usually pass through if I was connecting to another .net service.
How should I be sending them to a PHP web service? I always get either invalid username/password combo errors or envelope malformatted error types
Thanks
Mr. B
So the intellisense is not working, but if you add the method in and try to use it does it work, or produce an error?
With regard to diagnosing authentication issues try using fiddler to view the SOAP messages that are being sent, and to view the reply. Do you have some other software that connects and authenticates to that service? Use fiddler to look at the SOAP messages and compare them to see if the header is different etc.
I'd normally do it like this,
using (Service service = new Service())
{
service.ClientCredentials.Windows.ClientCredential.Domain = "domain";
service.ClientCredentials.Windows.ClientCredential.Password = "password";
service.ClientCredentials.Windows.ClientCredential.UserName = "username";
}
Also with regard to the service working or not in general use fiddler if you have any problems, you can see the SOAP messages and it often gives you a clearer message.
I know in IIS you can turn on failed request handling that also gives you an insight from what is going on at the server end, perhaps you have some form of logging too for your php service?

how to pass array from WinForm to WebService

How to pass array from WinForm to WebService?
Can I get any C# sample?
In Visual Studio, simply add a Web Reference or a Service Reference to your WinForm project and it will create the service proxy for you. This assumes that your WebService is exposing a WSDL file that describes the methods and parameters used.
This is a pretty broad question, and it would depend entirely on the type of web service you are looking for. Here are some instructions on how to add a Web Service reference:
Add a link to a web service
Once added, you can call whatever method requires an array and pass in the array through the parameters. A sample instantiation and method call for a web service might look like this:
MyWebService myWebServiceInstance = new MyWebService(url);
string[] params = new string[2];
myWebServiceInstance.CallArrayMethod(params);
If the web service is SOAP based, it should have a WSDL. If so, simply import a service reference to the WSDL and it will set up the proxy for you. Then you create an array and pass it to the method in question.
If you are talking REST based services, I would look at the RestBucks implementation on CodePlex (http://restbucks.codeplex.com/). You will want to look at the client side code. It will show you how to add your "array" in the call body, while setting up header information, etc.
Worst case is going down to a lower level and creating your own Request object. Most likely that would be overkill.

See the SOAP request from Proxy class method calls

I'm using .NET 2.0 web services. If I add a reference to a WSDL and make a proxy class method call, what's the easiest way in VS to see the SOAP being sent?
Example, I added the PayPal WSDL Web Service Reference and made a call as so:
PayPalAPIAASoapBinding _client = new PayPalAPIAASoapBinding();
...rest of code and then
SetExpressCheckoutResponseType checkoutResponse = new SetExpressCheckoutResponseType();
checkoutResponse = _client.SetExpressCheckout(request); // makes the call here
I tried setting a debug point on line 2 but not sure how to dive in to see the SOAP. Obviously I could use something like Fiddler but want to just use Intellisense during debugging to drill down to the object that has the request. I would assume client would have it, my instance above but could not find it. Client is an instance of the PayPal Service.
I do see when I drill down into the base class PayPalAPIAASoapBinding that there is a version property but I can't get the value for this:
System.Web.Services.Protocols.SoapProtocolVersion.Default
when I try to paste that into my watch window, the value just shows the word Default not the true value that's sent. So this is why I need to look at the SOAP and so far in that binding object I don't see a property holding it. But it's gotta be somewhere in any requests you make in a web service in .NET, just don't know where to look during debug?
My end goal here is to be able to read the SOAP envelop before it's being sent really using any WSDL reference in VS.
There's no very easy way. See the example in the SoapExtension documentation on MSDN for a way to log the information.
If you were using WCF, you could just turn on logging in the configuration.
The easiest way to see the SOAP messages (regardless of the programming language) is to use a tool like SoapUI or TCPmon which lets you intercept send and received messages.
This is very easy (if the SOAP is not encrypted). Although it is not in VS.
The easiest way is to use the Fiddler. You can let your VS make Soap calls and see the traffic in raw view on the Fiddler. If the Soap calls are made over SSL, there are some extra steps that needs to be taken for Fiddler to trace them.

Categories

Resources