Showing posts with label domain. Show all posts
Showing posts with label domain. Show all posts

Wednesday, March 28, 2012

Calling a WebService in another website on the same domain (Solved)

Hi all.

I'd been messing about with Atlas this week and after some banging of head against wall managed to get the basics working. I then found myself wanting to build a modular application consisting of multiple web services, but was getting no joy calling web servies in external solutions (projects) - I had the usual stuff reported in Fiddler suggesting I add [WebOperationAttribute(true, ResponseFormatMode.Json, true)] to my web method.

Not having done any webservice coding before this was all a bit bewildering but with some persistence I managed to get it all working.

If anyone else is having trouble here's what you do:

1) Make sure your remote web service is also an Atlas application.

2) In the Web.Config file of the remote webservice add the following under the <system.web> section:

<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" type="Microsoft.Web.Services.ScriptHandlerFactory" validate="false"/>
<add verb="*" path="iframecall.axd" type="Microsoft.Web.Services.IFrameHandler" validate="false"/>
</httpHandlers>

3) In your WebService class file include a reference to the Microsoft.Web.Services namespace:

Imports Microsoft.Web.Services

4) On your WebMethod add the following WebOperationAttribute:

<WebOperationAttribute(True, ResponseFormatMode.Json, True)>

So, it should look like:

<WebMethod()> _
<WebOperationAttribute(True, ResponseFormatMode.Json, True)> _
Public Function HelloWorld(ByVal strValue As String) As String
Return strValue
End Function

(some examples I found (including that of Fiddler I think) had square brackets surrounding the WebOperationAttribute which confused the issue)

5) In your calling application, reference the remote service as follows:

<atlas:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<atlas:ServiceReference Path="http://localhost/AtlasTest2/Test.asmx" />
</Services>
</atlas:ScriptManager>


...and that should work.

Hope this helps. Any comments/suggestions welcome.

Ben

Hi Ben,

In theory, you should not have to go through the iframehandler if your other web service is in the same domain. Try simply havingpath="/AtlasTest2/Test.asmx" without thehttp://localhost part.

The reason you saw square brackets in some samples is that it is the C# syntax for attributes, while you're using VB.

David

Monday, March 26, 2012

call web services using bridging (ajax beta 2)

I have several web service methods(implemented in .net 1.1) to call, in a different domain. The response I get when callin any methods from this web service is the page I see at the URL where .asmx is located. The .asbx is:

<bridge namespace="Support" className="OLOS" ><proxy type="Microsoft.Web.Preview.Services.BridgeRestProxy" serviceUrl="http://suppsite/SuppServices/OLOS.asmx" /><method name="getGeodisInfo" ><input><parameter name="OrderInformation" /><parameter name="BUID" /></input></method><method name="getExpeditorsInfo" ><input><parameter name="OrderInformation"/><parameter name="BUID" /></input></method></bridge>

And the .aspx file:

.............. <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/OLOS.asbx" /> </Services> </asp:ScriptManager>..............Support.OLOS.getGeodisInfo({ 'OrderInformation' : txtSearchText.value, 'BUID' : ' ' }, OnSucessgetGeodisInfo, OnError, "OLOS Search");.............. function OnSucessgetGeodisInfo(result) { if (result != null) { var resultText = result; document.getElementById('res1').innerHTML = resultText; } }
 
In the innerHTML of the DIV where I show results I get the content from the page located athttp://suppsite/SuppServices/OLOS.asmx
I do not see the actual call to the web method being done. Fiddler shows a call tohttp://suppsite/CareServiceCall/OLOS.asbx/js/getGeodisInfo but the response... does not contain what the method should return...
Any ideas?
Thanks

So I'm guessing your service url for the Asmx endpoint should behttp://suppsite/SuppServices/OLOS.asmx/<method>. What you'll need to do, is implement a TransformRequest in your Bridge codebehind, and as part of this, you need to set the ServiceUrl of the BridgeRequestl on the BridgeContext to the correct serviceUrl for your method. Since this is asmx, I imagine what you want to do is something like:

public override void TransformRequest() {
BridgeRequest.ServiceUrl = BridgeRequest.ServiceUrl + "/" + BridgeRequest.Method;
base.TransformRequest();
}

Hope that helps,
-Hao


Thanks for the response. Indeed that woulddefinitely help, at least a little bit later in the development phase.

What I am actually trying to do here is: I want to have all the methods for this web service(http://suppsite/SuppServices/OLOS.asmx/) defined in the same .asbx.

I know that changing serviceUrl tohttp://suppsite/SuppServices/OLOS.asmx/getGeodisInfo does the job, but then the rest of the methods defined in the .asbx are not callable. Using the url suffixed with the method name, works only if the web service has GET support, otherwise is not working for me. I also need to get this working when the web service works with POST only. I currently do not need transforms for method returns... at least not for all of them.

Is there a way to define in the same .asbx multiple methods and use them from javascript(except the way provided in the above sample)?


What I posted below dynamically changes the serviceUrl for each bridge request to be the correct one based on the method being called, which indeed it only works for GET, but if you want to support POST for asmx, you will need to build your own Proxy type for the bridge to make POST requests in the format ASMX expects, as the RestProxy only supports GET requests.

Hope that helps,
-Hao


Thanks Hao, everything is crystal clear now.

Call remotely hosted web service from client-side?

All of the examples I have seen call the *.asmx file using a relative path but I need to reference a URL such ashttp://www.domain.com/webservice.asmx to call a remote web service. In the Atlas days I believe this was done using a bridge (*.asbx) but I can't seem to find any recent information regarding this method. Does it still exist?

Anyone know how to accomplish this?

Thanks,
Janea

Hi,

my buddy Garbin (Allesandro Gallo) wrote an interesting article about this recently:Mash-it Up with ASP.NET AJAX: Using a proxy to access remote APIs.

Grz, Kris.


Wow!Yes What an excellent article!! I actually ran across it when I was initially searching for an answer but I thought there might be an easier way, however it makes a lot of sense why it is done the way he describes it. So, I'm going to try it out... create a local web service to be used as a proxy to connect to the URL containing the web service and call the remote procedures from there. Seems like a simple concept... I feel like I should've figured it out on my own. Well we'll see how it goes... Thanks a ton Kris!