Sunday, March 11, 2012

How call other websites webservice

You should use AJAX bridges to do this. The bridges ensures that a proxy is created on the server side, in the same web app folder, which can be used then by proxy javascript from client side.

You simple have to create an ASP.NET AJAX CTP Enable web site project, then, create an file with the .asbx extension, for example OLOS.asbx, which should look like this:


<bridge namespace="Supp" className="OLOS" >
<proxy type="Microsoft.Web.Preview.Services.BridgeRestProxy" serviceUrl="http://suppsite.com/WebServices/OLOS.asmx/getOrderContentByOrderNum"/>
<method name="getOrderContentByOrderNum" >
<input>
<parameter name="buId" />
<parameter name="orderNum" />
</input>
</method>
</bridge>

Then in your .aspx page add this:

<asp:ScriptManager ID="ScriptManager1" runat="server"><Services><asp:ServiceReference Path="~/Bridges/OLOS.asbx" /></Services></asp:ScriptManager>

Then call your web service method like this, from javascript:

function OnSearch(){ var parameters1 = {'buId' : txtSearchText.value,'orderNum' :'12345' }; Supp.OLOS.getGeodisInfo(parameters1, OnSucessgetGeodisInfo, OnError,"OLOS Search");}function OnSucessgetGeodisInfo(serviceresult){ $get('DIVElementTagId').innerText = serviceResult; }
 
 function OnError(result, userContext, response) { var errorString ="Test '" + userContext +"' failed! Response : " + response;if (result ==null) { errorString +=" Status code='" + response.get_statusCode() +"'"; }else { errorString +=" Message='" + result.get_message() +"'\r\nstackTrace = " + result.get_stackTrace(); } alert(errorString); }
You can add as many .asbx files as you want for each method and site you need. I suppose that the attributes to be changed in the .asbx file according to your needs are self explanatory.
Hope this helps

hi avmoldovan,

The example of the .asbx above is that all for creating a bridge? Is there somewhere an example how to create an .asbx file? when i use your example my project start to complain

Could not load type 'Microsoft.Web.Preview.Services.BridgeBuildProvider'

Is there something i need to add to my config file?

I already added

<

buildProviders>

<

addextension=".asbx"type="Microsoft.Web.Preview.Services.BridgeBuildProvider" />

</

buildProviders>

<

addverb="*"path="*.asbx"type="Microsoft.Web.Script.Services.ScriptHandlerFactory"validate="false"/>

Kind regards,


Did you add an refference to Microsoft.Web.Preview.dll ?

I do not know about any good references about .asbx files, but I use them with big success so far. However, have a loog on the following threads, they have really helpfull informations regarding this topic:

http://forums.asp.net/thread/1283700.aspx

http://forums.asp.net/thread/1436205.aspx

And if you still do not succeed, please enter here the error(s) you get.


Did you add an refference to Microsoft.Web.Preview.dll ?

I do not know about any good references about .asbx files, but I use them with big success so far. However, have a look on the following threads, they have really helpfull informations regarding this topic:

http://forums.asp.net/thread/1283700.aspx

http://forums.asp.net/thread/1436205.aspx

And if you still do not succeed, please enter here the error(s) you get.


Hi Avmoldovan,

thanks for your reply. i run into a next problem. my default.apsx complains that he can't find the resource service.asmx

first when i invoke the webservice it shows the method that the webservice is exposing. when i click on helloworld then i receive the error can't find resource service.asmx. below some code i use

.ASBX file

<?

xmlversion="1.0"encoding="utf-8" ?>

<

bridgenamespace="Test"className="Proxy">

<

proxytype="Microsoft.Web.Preview.Services.BridgeRestProxy"serviceUrl="http://localhost/AjaxWebservice/Service.asmx/HelloWorld" />

<

methodname="HelloWorld" >

<

input>

<

parametername="entry" />

</

input>

</

method>

</

bridge>

DEFAULT.ASPX

<

asp:ScriptManagerID="ScriptManager1"runat="server"><Services><asp:ServiceReferencePath="~/bridge/external.asbx"/></Services></asp:ScriptManager><div><inputid="SearchKey"type="text"/><inputid="SearchButton"type="button"value="Search"onclick="callWebService()"/></div><divid="Label">Loading ...</div>

<scripttype="text/javascript">function callWebService()

{

var txt = document.getElementById("SearchKey");var parameters = {'entry' : txt.value};

Test.Proxy.HelloWorld(parameters, onComplete);

}

function onComplete(result)

{

$get(

'Label').innerHTML = result;

}

</script>

Do you have any idea? thanks in advance


Hi Dennis,

undefinedhttp://localhost/AjaxWebservice/Service.asmx/HelloWorld

Enable GET over the web service you are using, by adding following lines in web.config of the web service, under <configuration>/<system.web> location:

<

webServices>

<

protocols>

<

addname="HttpGet"/>

</

protocols>

</

webServices>

Currently the Microsoft.Web.Preview.Services.BridgeRestProxy only use GET to pass parameters, so the web service has to be GET enabled.

For debugging HTTP output you can useFiddler, to see exactly what goes wrong when calling the web service. It will be also very helpfull to watch the internals of AJAX bridging at work.

Hope this helps


Hi Avmoldovan,

I have added the protocol to the config file of my webservice but still i get this error:

The resource cannot be found.

Description:HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL:/AJAXSite/Service.asmx

I seems to me that the bridge is working because the error reffers to my webproject /AJAXSite/Service.asmx. where service.asmx is created by the scriptmanager. The webservice self is also working correctly. please help

Regards


It seems that my response didn't got posted completed...

Can you confirm that when pointing your browser to the web service's URL is givving you the right response? I'm guessing that the problem might be in the web service. Also check the serviceUrl attribute value in the .asbx file to point in the right place.

If the web service performs OK, ther is one thing which you might have to do : register .asbx extension for you AJAX web site in IIS using the same handler as for .aspx extensions:

1. open IIS confugration management console

2. right click -> Properties on you virtual web folder you have for the AJAX web site

3. in the Directory tab of the configuration pop-up, click on the Configuration button, and you see the list of registered application extensions

4. click Add, and in the executable enter a path like: 'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll' and in the Extension box type : '.asbx'

5. close all windows by clicking OK


Hi avmoldovan,

my webservice is responsing correctly and the serviceurl is pointing correctly to the webservice. I'am running my Ajax website in debug mode in VS2005. when is run the website its opend i go to the following URL:

http://localhost:3074/AJAXSite/Default.aspx

when i click on the button it shows the following message:

Service


The following operations are supported. For a formal definition, please review theService Description.

HelloWorld

The problem is then caused by the web service. It should give you an error-clean response in plain html.

It seems that you are running the web service using the built in web server which ships with Visual Studio. I do not know if this might be the reason but I suppose it should work.

Can you try to rewrite the web service, but this time create the web project using Location : HTTP in case you created it using File System?

Also, the serviceUrl attribute should have the port on which the web service is running:http://localhost:3074/AJAXSite/Service.asmx?WSDL.

Also note, that if you have the web service under the same site as the AJAX solution, I'm not sure it will do the work. Try to separate the web service(put it in another web project) from the AJAX site, or use Amazon, msn, yahoo, google services, there are lots of them.


Hi Avmoldovan,

Thanks for youre reply. right now i'am running out of time. Tonight and in this weekend I will proceed my journey with bridges and webservices. please keep tracking this thread. I hope that i will succeed this weekend. Thanks for so far

Kind regards,


Hi Avmoldovan,

IT WORKS!! finally. I created a new webservice and it didn't work. Than i dropped my old .asbx file and created a new one. somehow old .asbx file had old references to the Helloworld method. I changed the name of the method of the webservice. when is made a reference to the new method it complains about the method name ("does not exists") so i created a new .asbx file and pointed it to the new method and ik works. Thanks a lot for youre effort.

Kind regards,


This is great news.

Take care at caching. I often observed that when changing something in the .asbx the changes are not reflected in the reality. go and delete temporary asp.net files to get a new generation of the .asbx.cs.

Good luck!

No comments:

Post a Comment