How can I consume 2 web services with one class method? - c#

I'm creating a .Net application to consume the Soap APIs.
I downloaded 2 partner wsdl files from 2 instances(production and sandbox). I think the only difference of the two APIs are their endpoints.
I then added the web references to a single application. When I write the method to consume the APIs, I don't want to duplicate the code to do same thing(insert,update...).
How can I design my code so maybe I can pass a parameter to let the method know which target instance should it talk to?
Thank you!

If the services are truly the same and just the endpoint differs, you should be able to use the generated client's Endpoint property to change the endpoint.
var client = new ServiceReference1.WebService1SoapClient();
client.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://localhost:2850/WebService1.asmx");

Related

Call a soap client's method using a string and an object

I'm using an external open source project that provides me computation services that I create using a UI that it provides.
The project creates web-service endpoints automatically that I'm suppose to consume via my application.
My problem is that I can't interfere in the process. It's a black box that creates a service for me when I choose to deploy the project.
Each service has a bunch of different logical "private methods" that are exposed in the wsdl that's automatically created.
If I could create the service myself, I would create one an interface with an exposed method called Process that will have one general input request param and one general Response, something like:
public GeneralResponse Process(GeneralRequest request);
I want to create a generic out-point in my application which passes two parameters:
1.Endpoint Url to shoot the request to.
2.Generic request as an input param.
I'm using C# and the easiest way to consume a service is simply adding a service-reference, creating a client and calling the wanted method.
Since I don't want to add a client per service reference, I'll add a random one, change the client's endpoint address and shoot the request.
The problem with this approach is that the client generated will expose it's "private methods" and I don't want other programmers on my team to accidentally invoke them.
Bottom line:
Is there any elegant way to create a Generic soap client and invoke a method using a string? Similar to the way you call the Invoke method when you use reflection?
Something like this:
GenericRequest req = GetRequest();
SoapClient client = new SoapClient(endpointUrl);
GenericResponse res = client.Invoke("Process", req) as GenericResponse;

Sending SOAP requests in 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

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.

SOAP web service: many servers, one interface

I have a scenario in which I'm going to need an arbitrary number of servers to provide the same SOAP web service. I would like to generate one set of proxy classes and be able to supply them with a location to point them at the different servers at runtime. Unfortunately, it looks as though the wsdl:port node (child of wsdl:service) requires the address of a specific server to be hardcoded. It appears that due to this the URL will be baked into my proxy classes. I know that I could potentially modify this by hand-editing the generated proxy classes, or modifying the code generation, but I'd really prefer not to resort to that. I feel like there's got to be a better way to solve this problem. I just want to decouple the interface definition from the location that the service will be residing at. I'm using VS2008 and C#.NET if that's of any help though best would be a language-agnostic (SOAP or WSDL specific) general solution to this problem.
Why don't you load balance the web servers and then create a DNS entry for the load balanced IP address....essentially creating a web farm. This will allow you to reference the hostname rather than the static IP addresses and if you ever need to change the IP address of the load balancer or the web servers it is a one time change. Plus you then have redundancy and performance control.
If you're using a WebReference (pre-WCF) to get to the web service, you can simply set the Url property on the web service proxy class after you create it.
For WCF, you can provide a different endpoint address to the proxy class constructor, rather than using the default (among other possible solutions).
No, in .NET you can change the URL at runtime.
Service svc = new Service ();
svc.url = "Value read from config. file or some such"
output = svc.method (input);
When you add a web reference to your project, it places the address of the web service into the .config file of your application / web application. You can then simply change this setting in the config file to point to a different web service location, assuming of course that the services are identical.
The easiest solution would be to use a software load balancer such as HAProxy. At more cost, you could use a hardware solution such as Big-IP.
Here's a hint on how to decide the URL of WSDL. I´m just changing the port but it´s of course possible to make it more advanced.
public class PortChangeReflector : SoapExtensionReflector
{
public override void ReflectDescription()
{
ServiceDescription description = ReflectionContext.ServiceDescription;
foreach (Service service in description.Services)
{
foreach (Port port in service.Ports)
{
foreach (ServiceDescriptionFormatExtension extension in port.Extensions)
{
SoapAddressBinding binding = extension as SoapAddressBinding;
if (binding != null && !binding.Location.Contains("8092"))
{
binding.Location = binding.Location.Replace("92", "8092");
}
}
}
}
}
}
Put that in your Add_Code and add the following reference to your web.config.
<webServices>
<soapExtensionReflectorTypes>
<add type="Dev.PortChangeReflector,App_Code"/>
</soapExtensionReflectorTypes>
</webServices>
I hope you can get new ideas of this.
Client proxies have URL property you can set at runtime. To make it simpler, wsdl.exe utility has /appsettingurlkey key. When you generate a client proxy, it's constructor will check the key in appSettings and set the service URL accordingly. I believe WCF has this feature as well.
However, I would agree with #Matt and suggest you consider load balancing as the best solution in the long run.
Is this for scaling (each server provides the same data) or
for same API different data on each server?
For 2, then you can do as above, just change the service URL in code.
For 1, you could use round-robin DNS (e.g. you see multiple servers with at the command line type nslookup www.google.com).

Categories

Resources