title: Calling a webservice from js in a web application
body: <p>I have created a simple webmethod which is used to keep a session alive: </p>
  
  <pre><code>Imports System.ServiceModel
  Imports System.ServiceModel.Activation
  Imports System.ServiceModel.Web
  
  &lt;ServiceContract(Namespace:="PMWebService")&gt;
  &lt;AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)&gt;
  Public Class WebService
  
  ' To use HTTP GET, add &lt;WebGet()&gt; attribute. (Default ResponseFormat is WebMessageFormat.Json)
  ' To create an operation that returns XML,
  '     add &lt;WebGet(ResponseFormat:=WebMessageFormat.Xml)&gt;,
  '     and include the following line in the operation body:
  '         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"
  &lt;OperationContract()&gt;
  Public Shared Function KeepSessionAlive() As Integer
  
      'Keep the session alive by writing to a session variable
      HttpContext.Current.Session("KeepSessionAlive") = DateTime.Now
      'Give the user time to keep the session alive
      Dim iSecondsToKeepAlive As Integer = 50
      'Set the timeout before the 'keep alive' message is displayed
      Dim iTimeout As Integer = (HttpContext.Current.Session.Timeout * 60000) - (iSecondsToKeepAlive * 1000)
      Return iTimeout
  
  End Function
  
  ' Add more operations here and mark them with &lt;OperationContract()&gt;
  
  End Class
  </code></pre>
  
  <p>My scriptmanager is defined as follows:</p>
  
  <pre><code>&lt;asp:ScriptManager ID="ScriptManagerMaster" EnablePartialRendering="true" runat="server"&gt;
      &lt;Services&gt;
          &lt;asp:ServiceReference Path="~/Shared/WebService.svc" /&gt;
      &lt;/Services&gt;
  &lt;/asp:ScriptManager&gt;
  </code></pre>
  
  <p>I would like to be able to access this from all my webpages. I have tried creating a wcf service to call this by:</p>
  
  <pre><code>    $('.renew-session').click(function () {
          clearTimeout(modalTimeout);
          var stayAlive = new PMWebService.WebService
          stayAlive.KeepSessionAlive(onTimeoutReturned, onDataFailure);
      });
  </code></pre>
  
  <p>However this fails at "var stayAlive = new MyWebService.WebService" saying that PMWebService is undefined.</p>
  
  <p>I have tried to follow the example at: <a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=235" rel="nofollow">http://www.dotnetcurry.com/ShowArticle.aspx?ID=235</a>. However when I go through these steps I get a difference at Step 4. When I look at the properties for my scriptmanager I don't see a services collection. My website is running as a web application so I don't know if this would make any difference.</p>
  
  <p>I hope someone can help with this. I have also tried running this as an asmx service but that gave the same error. I can run it as a PageMethod but I would have to put this on each page. I have also tried an ashx handler but don't think I can return a timeout value from this.</p>