How to call a webservice programmatically in asp.net - web-services

How to call webservice programmatically in asp.net without using add web reference?
My webservice url keeps on changing. Hence i need to capture the url at runtime and display the results.
Please advice.

You can change the URL of a web-reference at runtime (provided that the new address is hosting a service with the same schema that you originally used to create the reference):
MyWebService ms = new MyWebService();
ms.Url = "http://example.com/webservice.asmx";
ms.MyWebMethod();
Web References are definitely the way to go - whilst the classes that are created by the web reference are usually pretty heavy, all that strong typing makes it well worth your while.

you need to do the following steps.
PreReq :
First of all, you know the URL of web service.
Solution:
use wsdl.exe to create a proxy class and than compile it.
wsdl /out:myProxyClass.cs http://hostServer/WebserviceRoot/WebServiceName.asmx?WSDL
(there are other switches available
for wsdl. For Example to generate VB
class, you need to add switch
/language:VB)
Once your proxy class is generated you can easily consume in code.
MyProxyClass objService = new MyProxyClass();
DateTime time = objService.GetServerTime(); //Suppose service has method getServerTime

You can specify the end-point URL as part of the constructor of your client-side proxy class.
If you don't need to specify it during runtime then it can also be set in your web.config file.

Where are you trying to call the service and where the service file is located?
If the service is located on the same site. Why not just instantiate the class name from the service. Or just create a separate class and use interface

Related

C++ RESTful Service Router Pattern

I've been searching around for the past few days trying to find a good article that addresses what I'm trying to do, but haven't really found much yet. Most point me in the direction of pre-existing C++ MVC frameworks.
I have a C++ application with a tiny embedded web server that responds to HTTP requests. What I want to do is create a routing pattern in which I can define service end points and the application will be able to use the proper C++ controller class.
For example, I could define my service end points like the following in a text file:
POST /login UserController::login()
GET /user UserController::get()
GET /dashboard DashboardController::get()
And have the C++ application call the appropriate class and static method based on the service I'm invoking. So in the case of the above example, if I submit an HTTP POST request to /login, then the Router should invoke UserController::login(). Service end points don't necessarily have to be defined in a text file, there could be another in-memory data structure they can be defined in.
I've been considering something like the Proxy pattern: http://en.wikipedia.org/wiki/Proxy_pattern
Or I was looking at this pattern: http://gameprogrammingpatterns.com/service-locator.html
Before going too far down a specific implementation I wanted to get some feedback from others on how they might go about implementing such a requirement?
Thanks!

Correct way of initializing internal data for API in Django with Piston/Django-tastypie vs jsonrpc

After some API implementations, using jsonrpclib in Python, I need to migrate them inside a Django Framework project. I am quite new in Django and Piston/tastypie, but have some experience using jsonrpc/xmlrpc libs in my Python apps.
Until now I have developed some modules, with a ServiceClass attached to the register of jsonrpc server who handle the request and call the methods in the ServiceClass.
When the class is attached to the register, a new instance of the ServiceClass is created, loading all the initial data and keeping it in memory, so every method called through jsonrpc can have access to the internal values in that instance.
Now, I am trying to do the same in Django with Piston or Tastypie. I followed this link http://www.robertshady.com/content/creating-very-basic-api-using-python-django-and-piston and other resources, and all the documentation I read is clear, showing the correct way to work with it:
Modify url.py to map requests like "/api/" to a specific handler.
Add a handler.py in the api application, extending the BaseHandler of
Piston/Tastypie.
So I am wondering if its the correct way of working with Django and APIs, to create the instance of my ServiceClass (init the data, provide the methods) inside handler.py when I create the instance of the Handler extending the BaseHandler. Is this Handler class instantiated once when the server starts? What if my ServiceClass relies on some Model to load the data from it?
I want to avoid the framework to instantiate my class everytime a new request arrive to the /api/ application.
I will be glad to hear about any recommendation,
Thanks,
Specifically for piston... You shouldnt really use the handler in terms of an instance. Its more like a metaclass that you set up with class attributes. These attributes control whicch model the handler will be bound to if any. And what fields it should show or what methods it supports.
Generally the request enter one of your methods and you then handle the request however you want, as an isolated state. If it needs to use a shared resource or use the model for queries, that part is up to you, being shared from some imported resource . You said you need a model which is why you would bind it to the handler as a class attribute and then query on it. You shouldnt really store state on the handler.

How to retrieve WCF service link in flex using httpservices

I want to retrieve server content which is like wsdl link(WCF service URL)using FLEX 4.5.. I haven't worked with webservices on FLEX. I have worked with xml data retrieval using httpservices where I had a local xml datas. Right now, i am trying to retrieve a server content. I have provided with the service link, method name and xml tags. (seems like parameters).. Since this is the first time im trying the server content, I need some help. Your help is highly appreciated.. thanks in advance... Would be better if i can get a sample project on webservices.
This is what I'm trying. The service link is below.
http://mfsapi.blisslogix.net/RSS_FEEDS_SERVICE.svc
When I click on this link, i'm getting the below link.
http://mfsapi.blisslogix.net/RSS_FEEDS_SERVICE.svc?wsdl
where I can see a lot of tags.
I am using HTTPSERVICES and WEBSERVICES to work on this issue and i'm not getting the xml data. I guess I did some mistake on passing the parameters. Please walk me through the steps how can I pass the method and parameters with this link..
First you need to create a WebService tag. Or use ActionScript an object of type WebService.
<mx:WebService id="myWebService"
useProxy="false"
showBusyCursor="true"
load="OnServiceLoad(event)"
fault="OnFault(event)">
<s:operation name="GetInformation" result="onLoad(event)" fault="onFault(event)">
</s:operation>
</mx:WebService>
Then you need to specify the WSDL document location and load it.
myWebService.loadWSDL("http://mfsapi.blisslogix.net/RSS_FEEDS_SERVICE.svc?wsdl");
Then you can simply call the operations specified in the WebService tag.
myWebService.GetInformation();
Here is a link on how to communicate with web services using MXML and AS.

"Duplicate file name" for same WSDL namespace when using web-service from different sub-domains

Preface
We are providing customers with our service API.
Each customer has own subdomain (e.g. sergii.ourwebsite.com) and own WSDL URL, it looks like http://sergii.ourwebsite.com/api/bsapi.cfc?wsdl
Also, all the websites (including API, of course) using the same codebase.
Problem
Say, two applications on same CF-server. This can easily happen, because some of customer websites are hosted on our servers.
Both trying to use own API WSDL, say:
http://sergii.ourwebsite.com/api/bsapi.cfc?wsdl
http://galashyn.ourwebsite.com/api/bsapi.cfc?wsdl
And here come the problems.
When second website tries to register the web-service, CF throws an error:
Name:
https://galashyn.ourwebsite.com/api/bsapi.cfc?wsdl.
WSDL:
https://galashyn.ourwebsite.com/api/bsapi.cfc?wsdl.
org.apache.axis.wsdl.toJava.DuplicateFileException:
Duplicate file name:
/opt/coldfusion8/stubs/WS1985941973/api/Bsapi.java.
Hint: you may have mapped two
namespaces with elements of the same
name to the same package name. It is
recommended that you use a web browser
to retrieve and examine the requested
WSDL document to ensure it is correct.
If the requested WSDL document cannot
be retrieved or is dynamically
generated, it is likely that the
target web service has programming
errors.
Problem is that both of them are using same WSDL namespace, built from CFC path:
<wsdl:definitions targetNamespace="http://api">
Current solution
The only working solution for us is using the CFC aliases, like:
http://galashyn.ourwebsite.com/api/v1n1/bsapi.cfc?wsdl
http://galashyn.ourwebsite.com/api/v1n1/bsapi.cfc?wsdl
Each this CFC extends the parent like this:
<cfcomponent output="false" extends="api.bsapi">
<!--- this component used to extend base api version 1.x --->
</cfcomponent>
They produce different namespaces, which can be used without problems -- own namespace for each application:
<wsdl:definitions targetNamespace="http://v1n1.api">
<wsdl:definitions targetNamespace="http://v1n2.api">
This is pretty dumb workaround, but it works for now.
Other solution would be to use the single API sub-domain and identifying the customers by some key (we are already using them for security purposes), but it has serious negative problems for us because of some legacy code.
Please note that I don't know Java, so many specific advices are not so clear for me.
Google shows that this problem exists for years, but I can't find the smart solution.
So maybe here?
I can not "place the WSDL at a common URL for all customers" for now and I've explained why: because I have to use the sub-domains. If you know the way to put the WSDL at one URL and make service requests to another -- please tell me.
A WSDL is just an XML document that describes the web service. You can write (customize) it using CFML. For instance:
http://subdomain.domain.com/api/wsdl.cfm?api=bsapi&customer=subdomain
Then just copy the WSDL generated by CF, and use it as a template for your custom WSDL page. Replace the parts of the WSDL that are subdomain-specific and return the XML document. Be mindful of whitespace (perhaps see CFSilent, CFSetting), and consider using CFHeader to set the mime type to "text/xml".
Do all the customers use the same WSDL? Then place the WSDL at a common URL for all customers.
I also think you need to find out exactly what the error message means. I don't see where it has anything to do with the URL being used. If it had mentioned the URL of another customer, I'd have understood.
Part of this may be due to my lack of understanding of how CF works. In particular, what is this "registering web services" that triggers the problem when two customers do it?
Did you realize that the URL in the <soap:address/> element in the WSDL is only a hint? In many clients, it can be overridden. In a .NET client, just set the Url property of the proxy class. This should allow you to have a single WSDL at a single location, and yet have your customers each reference the proper subdomain, assuming there's some way to convey that information to them. For instance, if there is a way for you to know which customer is making the call, then perhaps you could receive calls on one URL and then redirect to the proper URL, or else use a SOAP Routing infrastructure to route to the correct one.
I hope you are not changing the namespaces from one customer to another. Namespaces have nothing to do with URLs, even if they happen to look like URLs.
There is a namespace attribute for the cfcomponent tag. You should be able to use this, along with cgi.host_name (? I'm at home, without docs) to specify a namespace that matches the subdomain being queried.
Something like:
<cfcomponent namespace="http://#cgi.host_name#/api/v1n1/bsapi.cfc">
Have caught this error message when tried to CFINVOKE webservice from localhost by test script located on same localhost (but in different folder). Sounds 'buggish' for me.

Distinguished field on a web service (BizTalk)

How do you access a field in the message returned by a web service call ?
I found Richard Seroters blog post but it says to drill through the Reference.map to the Reference.xsd ...
- problem is, I've not got a Reference.xsd
- I do have the Reference.map, and the child Reference.odx
- I also have the wsdl, but not the disco
It would seem that ...
If the method includes something other than primitive data types, there will also be a Reference.xsd file under Reference.map. After adding this reference, we’ll be able to create messages to communicate with the web service defining the type as a Web Message and choosing the appropriate schema.
and the web services I'm talking to just return a string, albeit actually XML
From this blog post
I am confused as to why you do not have an XSD that was generated by the BizTalk proxy? You can of course tag the generated proxy code class with attributes that will allow you to set the field as a promoted property or a distinguisted field. If you do not have either I am not sure you can do it. Take a look here.
-Bryan