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

No comments:

Post a Comment