Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi,

I am calling WCF function of my application from Javascript using asp:ServiceReference. I have declared ServiceReference in my Aspx page - "MyPage.aspx" as below.
ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">    
 <Scripts>
  <asp:ScriptReference Path="~/Scripts/script.js"/>
  <asp:ScriptReference Path="~/Scripts/jquery.js"/>
     
 </Scripts>        

 <Services>
  <asp:ServiceReference Path="~/Services/MyService.svc" />
        
 </Services>

</asp:ScriptManager>


I am calling WCF method MyWCFMethod in my Javascript as below.
JavaScript
function BeforeNavigate()
{
     var isConfirm = confirm("Do you want to Navigate?");
     if (isConfirm ) 
     {
       CCRClientService.MyWCFMethod(frmSuccess, frmFail, null);
       return false;
     }
}

function frmSuccessUnlock(result) { return true; }
function frmFail(result) { return false; }


Javascript function BeforeNavigate is called when user tries to navigate to other pages. When I come on "MyPage.aspx" and navigate to some other page after few seconds, my WCF method gets call successfully. But when navigate very quickly as soon as I come on "MyPage.aspx", then my WCF method does not get called.

Any idea why WCF method does not getting called when I navigate as soon as I come on "MyPage.aspx"?

Cheers
Rais
Posted
Comments
Ashraff Ali Wahab 11-Sep-12 10:24am    
Do you get any javascript error that CCRClientService is undefined?
RaisKazi 11-Sep-12 11:25am    
Nope, no error. It simply navigates to other page without calling WCF method, when I click on other link. But if I wait for few seconds on the page and then navigate, it does call WCF method.
Ashraff Ali Wahab 11-Sep-12 12:37pm    
where is the BeforeNavigate event attached to?
Its wierd that the webservice is not called with out any error.You tried firebug?
Nueman 11-Sep-12 10:25am    
I do not have an answer for you, but I congratulate you on a well defined question. Good job.
RaisKazi 11-Sep-12 11:28am    
Thank you Nueman. Its a strange issue. :)

1 solution

Found Alternate solution to resolve this issue.

1) Set EnablePageMethods property of your ScriptManager to true.
ASP.NET
<asp:scriptmanager id="ScriptManager1" runat="server" enablepagemethods="true" xmlns:asp="#unknown">
</asp:scriptmanager>


2)Write a static method in code-behind(aspx.cs) and add System.Web.Services.WebMethod attribute to it.
C#
[System.Web.Services.WebMethod]
public static void MyWCFMethod()
{
  //Implementation 
} 


3) And then call your WebMethod in Javascript as below.
JavaScript
function BeforeNavigate()
{
     var isConfirm = confirm("Do you want to Navigate?");
     if (isConfirm ) 
     {
       PageMethods.MyWCFMethod();
       return false;
     }

}


Hope this will be usefull. Cheers - Rais
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900