Thursday, February 11, 2010

Fixing Ajax using WCF in Web.config and IIS 7.0

Sometimes when you call WCF from javascript (http://Site/Service.svc/js) or adding your WCF URL as service reference in the script manager, you find that the JS Proxy of the WCF returns 404 not found from the server. You can Check my previous post for more error details here: http://kholyos.blogspot.com/2010/02/how-to-solve-404-not-found-when-calling.html
You can fix this issue using alternative method, by modifying in the web.config of your application. Using the <handlers> tag under <system.webserver> can do the trick for you.

By adding the following Elements under <configuration><system.webserver><handlers> you can fix the issue:

<add name="svc-ISAPI-2.0" path="*.svc" verb="*" scriptProcessor="%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" requireAccess="Script" resourceType="Unspecified" />



<add name="svc-ISAPI-2.0-64" path="*.svc" verb="*" scriptProcessor="%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" requireAccess="Script" resourceType="Unspecified" />



for more Information about handlers tag, you can follow this link to MSDN: http://msdn.microsoft.com/en-us/library/ms691481.aspx

That's it. And Enjoy :).

Monday, February 8, 2010

How to Solve 404 Not Found When Calling WCF from Javascript using script manager

When you and Service reference in the script manager to point to .svc file, and then calling it from javascript, sometimes it gives you script error in the page that the service cannot be found. To solve this issue, follow these steps (Considering you have IIS 7.0):
  1. Make sure that .Net framework 3.0 or later is installed on the server. You can find it here.
  2. Register the service model to the IIS by doing the following:
    • Open CMD.exe
    • Navigate to: "C:\Windows\Microsoft.NET\Framework\v3.0\Windows Communication Foundation"
    • Run "ServiceModelReg.exe -r"
    • then, if you have x64 Windows, Navigate to: "C:\Windows\Microsoft.NET\Framework64\v3.0\Windows Communication Foundation"
    • then run: "ServiceModelReg.exe -r"
  3. Open IIS. You can use Run->inetmgr.
  4. Click on the site that hosts the .svc file, and browse the Features view in the IIS. Then, open the Handler Mappings.
  5. In the right Panel (Actions), Choose Add Script Map.
  6. A window will appear to add new script map, and fill the following parameters:
  7. Request Path : *.svc

    Executable : %SystemRoot%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

    Name: svc-ISAPI-2.0

  8. Click Add Script Map again if you have x64 Windows and use the following parameters:
  9. Request Path : *.svc

    Executable : %SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll

    Name: svc-ISAPI-2.0-64

  10. Then browse the Service using your internet browser : http://yourURL/...../YourService.svc/js, if you can see the scripts generated by Microsoft Ajax then you are done. Otherwise, you should allow anonymouse in your web site.
That's it and Enjoy :).