我创建了一个简单的网络方法,用于保持会话处于活动状态:
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
<ServiceContract(Namespace:="PMWebService")>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class WebService
' To use HTTP GET, add <WebGet()> attribute. (Default ResponseFormat is WebMessageFormat.Json)
' To create an operation that returns XML,
' add <WebGet(ResponseFormat:=WebMessageFormat.Xml)>,
' and include the following line in the operation body:
' WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"
<OperationContract()>
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 <OperationContract()>
End Class
我的脚本管理器定义如下:
<asp:ScriptManager ID="ScriptManagerMaster" EnablePartialRendering="true" runat="server">
<Services>
<asp:ServiceReference Path="~/Shared/WebService.svc" />
</Services>
</asp:ScriptManager>
我希望能够从我的所有网页中访问它。我尝试创建一个 wcf 服务来调用它:
$('.renew-session').click(function () {
clearTimeout(modalTimeout);
var stayAlive = new PMWebService.WebService
stayAlive.KeepSessionAlive(onTimeoutReturned, onDataFailure);
});
然而,这在“var stayAlive = new MyWebService.WebService”处失败,表示 PMWebService 未定义。
我试图按照以下示例进行操作:http ://www.dotnetcurry.com/ShowArticle.aspx?ID=235 。然而,当我完成这些步骤时,我在第 4 步得到了不同。当我查看我的脚本管理器的属性时,我没有看到服务集合。我的网站作为 Web 应用程序运行,所以我不知道这是否会有所不同。
我希望有人可以帮助解决这个问题。我也尝试过将其作为 asmx 服务运行,但这给出了同样的错误。我可以将它作为 PageMethod 运行,但我必须将它放在每个页面上。我也尝试过 ashx 处理程序,但我认为我不能从中返回超时值。