I have created a simple webmethod which I would like to be able to access from all my webpages. If I put the webmethod on an individual page I can access it from my page using:
PageMethods.MyMethod(onSuccess, onFailure);
I initially tried to put the webmethod onto my master page so that I could access it from any page but found that this would not work. I therefore created an asmx file to add a webservice. My problem now is that I can't work out how to call the webservice
from my page. When I created my asmx file it had 'Public Class MyService' in the asmx.vb file and class="MyApp.MyService" in the .asmx file where MyApp is the name of my web application.
Because it is a web app I think that I need to wrap my asmx.vb file in a namespace. So I added 'Namespace MyNamespace' before the line <System.Web.Script.Services.ScriptService()>. I then changed the Class name in the .asmx file to class="MyApp.MyNamespace.MyService".
I then tried to access the web service using:
Am I getting these in the wrong order or using the wrong names for things? I have tried various combinations of these names but it always fails saying the first name in the string I have used to call my web service is undefined (in this case MyApp). It could
be that it is something else altogether that is causing this to fail but it would be good to know whether I've got this bit right before I look elsewhere.
MyMethod is used to keep a session alive (the timeout is currently very short to allow for testing):
<WebMethod()> Public Shared Function MyMethod() As Integer
'Keep the session alive by writing to a session variable
Vivani
Member
6 Points
25 Posts
Calling Webservice from js in web application
Dec 06, 2012 10:41 AM|LINK
I have created a simple webmethod which I would like to be able to access from all my webpages. If I put the webmethod on an individual page I can access it from my page using:
PageMethods.MyMethod(onSuccess, onFailure);
I initially tried to put the webmethod onto my master page so that I could access it from any page but found that this would not work. I therefore created an asmx file to add a webservice. My problem now is that I can't work out how to call the webservice from my page. When I created my asmx file it had 'Public Class MyService' in the asmx.vb file and class="MyApp.MyService" in the .asmx file where MyApp is the name of my web application.
Because it is a web app I think that I need to wrap my asmx.vb file in a namespace. So I added 'Namespace MyNamespace' before the line <System.Web.Script.Services.ScriptService()>. I then changed the Class name in the .asmx file to class="MyApp.MyNamespace.MyService". I then tried to access the web service using:
MyApp.MyNamespace.MyService.MyMethod(onSuccess, onDataFailure);
Am I getting these in the wrong order or using the wrong names for things? I have tried various combinations of these names but it always fails saying the first name in the string I have used to call my web service is undefined (in this case MyApp). It could be that it is something else altogether that is causing this to fail but it would be good to know whether I've got this bit right before I look elsewhere.
MyMethod is used to keep a session alive (the timeout is currently very short to allow for testing):
<WebMethod()> Public Shared Function MyMethod() As Integer
'Keep the session alive by writing to a session variable
HttpContext.Current.Session("KeepSessionAlive") = DateTime.Now
Dim iTimeout As Integer = (HttpContext.Current.Session.Timeout * 60000) - 50000
Return iTimeout
End Function
It is called from a button on a modal form on my master page:
$('.renew-session').click(function () {
clearTimeout(modalTimeout);
MyApp.MyNamespace.MyService.MyMethod(onSuccess, onFailure);
});
Vivani
Member
6 Points
25 Posts
Re: Calling Webservice from js in web application
Dec 07, 2012 08:51 PM|LINK
I've solved this by turning my asmx service into a wcf service.