Webservice won't accept JSON requests - asp.net

The main issue is that I cannot run a webservice that accepts requests in JSON format. I keep getting a 500 Server error stating that the "request format is invalid." My ASP.NET AJAX extensions are installed. My server is running Plesk Control Panel 8.6 which is undoubtedly causing these problems.
The default handler for this specified extension is shown in the web.config like so:
For my applications webservice to handle JSON it needs to have this reference:
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
Plesk is not allowing the request to be handled properly. I need to know the correct http directive(s) to put into the web.config to properly handle JSON webservices. I tried posting to the Plesk forum two days ago but no response yet.
Any insight would be great =)

There are confirmed errors with webservices and httphandlers. To avoid grief, upgrade to > Plesk 9.0.

Related

HttpHandler not being called

I need to write a HttpHandler that will serve up JavaScript files which are embedded resources in .DLLs in my project. A reference in a view cannot directly see such a resource, so I planned to use a HttpHandler module that would intercept any request with a path /js/[file] , find a matching embedded file and return the script.
The problem is that my HttpHandler code is never called, despite trying lots of different settings in the section of web.config. I'm obviously missing something but with no error messages I cannot see what that is. All I ever get is a 404 from the static file handler.
Q1) Am I missing something obvious?
Q2) Is there a way to get IIS to tell me why it is not calling my handler?
Summary: I am testing on IIS Express (v8) for an ASP.NET MVC 4 application.
I created a simple library that implements IHttpHandler and added a reference to this in my test MVC application, and the following lines in web.config:
<system.webServer>
<validation validateIntegratedModeConfiguration="true" />
<handlers>
<add name="ejs" path="js/*" verb="*" type="EmbeddedJsHandler.EmbeddedJsHandler, EmbeddedJsHandler" preCondition="integratedMode" />
The library is there, but it is never called. Any request with /js/test.js or whatever just results in a 404 error.
So far I've tried lots of different configurations and settings in the handler code. I've tried preCondition, resourceType="Unspecified", modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
I've tried paths:
js/*.js
js/*
js/*.*
I've checked the integrated mode settings section (in system.webServer) is being used, and confirmed it is.
I've searched stack overflow for similar cases, and tried many of the possible solutions.. still no joy.
Heck even Jon Skeet has these sort of problems!
Why isn't my IHttpHandler being called?
Finally figured it out by accident - it was a missing routes.IgnoreRoute() in the RouteConfig.cs file - the MVC routing engine wasn't configured to ignore this path, so was passing it to the static file handler.
Doh!
Check this:
How to: Register HTTP Handlers:
To register an HTTP handler for IIS 7.0 running in Integrated Mode:
Compile the HTTP handler class and copy the resulting assembly to the Bin folder under the application's root folder.
In the application's Web.config file, create a handlers element in the system.webServer section.
The following example shows how to register an HTTP handler that responds to requests for the SampleHandler.new resource. The handler is defined as the class SampleHandler in the assembly SampleHandlerAssembly.
<configuration>
<system.webServer>
<handlers>
<add name="SampleHandler" verb="*"
path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
Note: The resourceType attribute performs the same function as the Verify file exists option in IIS manager for IIS 6.0.
For IIS 7.0 running in Integrated mode, only the registration in the handlers element is required.
I cannot tell you directly why your handler isn't working, but I will give you an example of a handler we use and works for us:
<system.webServer>
<handlers>
<add name="JS handler" path="*.js" verb="*" type="Handlers.Minifiers.JSMinify" resourceType="Unspecified" preCondition="integratedMode" />
</handlers>
</system.webServer>
We also have this segment, which is at least necessary for running in Cassini
<system.web>
<httpHandlers>
<add verb="*" path="*.js" type="Handlers.Minifiers.JSMinify" validate="false"/>
</httpHandlers>
</system.web>
If this doesn't help, have tou tried using path="/js/*"?

WCF gzip compression?

I need to compress my WCF response.
After few google search, I found that
I need to enable compression on iis7
I need to add some entry in the applicationhost.config.
My wcf is hosted on discountasp.net. In the control panel, I didn't find any option to enable compression.
And also, the applicationhost.config is located in the 'C:\Windows\System32\Inetsrv\Config\applicationHost.config' directory.
And in the shared hosting environment, I dont have access to this directory.
My question is, if my above observation is correct, how do I get it done in the shared hosting??
edit:
I have found something here:
enter link description here
I tried this with no luck. In the response header in Fiddler, I dont see any compression.
One alternative is to implement an HttpModule that would intercept the response based on the content type and compress the output on the fly, using the GZipStream class.
This post has a complete example and I've used this method successfully in production. All you need to do is change your Web.config to register the http module:
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add name="JsonCompressionModule" type="JsonCompressionModule"/>
</httpModules>
The example does it for JSON responses but there's nothing that stops you from doing the same thing for XML responses.

AutoCompleteExtender not working for WebService hosted on IIS 7

I have a web service file in my project having a web method which is used for AutoCompleteExtender and which works fine when I debug it from VS.
But when I publish and host it on IIS, it's not working properly.
However, I tested the webservice method directly by typing the URL and it gave the desired output.
Is their a special setting needs to be done in IIS to make it working or any property of AutoCompleteExtender need to be set?
I had this issue as I moved a site from cassini to IIS7.5. After a lot of digging had to add the follwoing to the web.config in the system.webServer section. Hope it helps.
<modules runAllManagedModulesForAllRequests="true">
<remove name="ScriptModule" />
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</modules>
<handlers>
<remove name="ScriptHandlerFactory"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
After a upgrade from ASP.NET 3.5 to 4.0 I have the same problem and can't get the extender to work. From firefox and firebug I recive a 500 Internal Server Error with the message
System.InvalidOperationException: Request format is invalid: application/json; charset=utf-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
I have tested with the web.config settings suggested by http://msdn.microsoft.com/en-us/library/bb763183.aspx but recive configuration errors.
Try changing the order of the handlers (remove then add). In this example I have removed all but the AJAX/script handler.
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="WebServiceHandlerFactory-ISAPI-2.0"/>
<remove name="WebServiceHandlerFactory-ISAPI-2.0-64"/>
<remove name="WebServiceHandlerFactory-ISAPI-4.0_32bit"/>
<remove name="WebServiceHandlerFactory-ISAPI-4.0_64bit"/>
<!--<add name="WebServiceHandlerFactory-Integrated-4.0" ...</handlers>
Its not the problem of IIS or ath. Its a bug actually (I think). In your function in webservice to return the string array please check the parameter names or arguments to the function.
The name of the string parameter must be prefixText and that of int field should be count.
And there must be these two parameters for your functions in the webservice.
like this
[WebMethod]
public string[] getCountry(string prefixText, int count)
{
......
....
return ...
}
I was facing the same problem. AutoCompleteExtender was not working after hosting in IIS 7 Windows Server 2008 R2.
By changing the "Managed Pipeline Mode" of Application Pool from "integrated" to "Classic" worked for me.
This setting can be taken by selecting the application pool -> select Basic setting.
Thank you.
Try...this
go to start>run>inetmgr>
In the connections sidepane..select application pools
select the application pool that u hav assigned when deployed that project into iis(to check that go to sites>in connections pane...and right click on website that u have deployed and select manage website >advanced settings then at the top u wil able to see application pool)
then comeback and select that particular application pool in application pools in connections pane
right click on that particular application pool and select advanced settings..find out process model in that and select identity and browse through it and goto built in account and select local system.......then click ok...and get out of it...
I think it will work..it worked for me.....

IIS7 is throwing a 500 error intermittently. Can anyone help me diagnose it?

Sorry for the vague title, as I really can't explain this problem succinctly.
Basically I have Windows Server 2008 x64, IIS7, ASP.NET 2.05, and I have a site running in a Classic AppPool (and no I cannot run in Integrated).
When trying to load an *.aspx file for the first time (i.e. after installing site, restarting the server, etc) I get this error:
HTTP Error 500.0 - Internal Server Error
The page cannot be displayed because an internal server error has occurred.
Module: IsapiModule
Notification: ExecuteRequestHandler
Handler: PageHandlerFactory-ISAPI-2.0-64
Error Code: 0x800710dd
Logon Method: Anonymous
Logon User: Anonymous
The handler is the default IIS7 one:
<add name="PageHandlerFactory-ISAPI-2.0-64" path="*.aspx" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv2.0,bitness64" responseBufferLimit="0" />
I even tried adding in my own handler for aspx that looked like this:
<add name="aspx" path="*.aspx" verb="GET,HEAD,POST,DEBUG" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness64" />
The only thing that did was change the Handler part of the error notification to say IsapiModule.
The odd thing is that this error only occurs the first time (or when the server has been idle for hours). As soon as I see this error, if I refresh the page it's all fine and dandy again.
I even tried deleting the web.config file and that did absolutely nothing.
I can't seem to find a single answer for this problem on the internet.
Edit: I enabled Failed Request Tracking and this is what it shows:
MODULE_SET_RESPONSE_ERROR_STATUS
Warning ModuleName="IsapiModule", Notification="EXECUTE_REQUEST_HANDLER", HttpStatus="500", HttpReason="Internal Server Error", HttpSubStatus="0", ErrorCode="The operation identifier is not valid.
(0x800710dd)", ConfigExceptionInfo=""
And right before the error it shows:
NOTIFY_MODULE_START ModuleName="IsapiModule", Notification="EXECUTE_REQUEST_HANDLER", fIsPostNotification="false", fIsCompletion="false"
Now when I compare this to a successful run the difference is that the error produces MODULE_SET_REPONSE_ERROR_STATUS whereas the successful run doesn't (and then goes on to produce the correct HTML output).
Edit: I took a simple app and tried to get that running and I received the same error. But when the apppool was in Integrated mode it ran fine! Unfortunately I cannot migrate our app to integrated for reasons I cannot specify but I narrowed it down to the app pool. Also I don't have to restart the server to repro the error, instead recycling the app pool will do.
Summary:
- As mentioned below there's nothing in the Event Logs to indicate failure. I combed through all logs in Event Viewer
Best thing to do is enable Failed Request Tracing in the IIS section of the web site. You can then enable some filters which give you much more detailed information.
You can do this through the IIS Manager. Click your web site, then in the IIS section of the Features View, double-click "Failed Request Tracing Rules".
It's most likely not already enabled, so from the rightmost column select "Edit Site Tracing". Check the "Enable" checkbox and make note of the directory.
You can then either add a rule in that screen or go to your application and open the "Failed Request Tracing Rules" IIS feature from there.
From the rightmost column again, click "Add..." Then go through the wizard and set up the logging.
Load your page that's throwing the error again. Go to the logging folder and double-click the XML file. There's an XSL in that directory. Don't nuke it, cuz once it's gone it won't get recreated. :s The transformed XML will show you more info than you can possibly hope for.
I just this evening used that to discover a custom error page that I configured was using ~/ instead of "/", causing IIS to die.
When ASP.NET application starts loading, you may have some code that may take too long to execute, probably too big application variables or resources initialization.
The best way is to setup some sort of ping monitor, lot of ISPs provide pinging monitors that can monitor your html url on regular interval, that may help keeping your application all time alive !!
Try looking into initialization procedures of your asp.net app, you may want to increase some timeout values !!
Sounds like something while starting up the app pool. But it should be logging the actual error in the event viewer. Or you could turn CustomErrors off to debug it. The thing is, you need to see the actual error to figure out what's going on.
Do you have an unhandled exception handler in your ASPX code (something like Global Application_Error)?
You should be able to catch the exception and log it if it's coming from the ASPX code (which is quite possible).
I've seen sporadic errors like this before, I just can't remember the underlying cause at the moment.
How to: Handle Application-Level Errors
Well I don't know what was causing this but a clean install of the VM I was using fixed it. Hurrah!
You must add this script on your web.config:
<system.webServer>
<handlers>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</handlers>
I hope this help ! Cheer.

ASP.NET MVC AJAX Sys is undefined error

I am getting a "Microsoft JScript runtime error: 'Sys' is undefined" error on one of my pages in an MVC application when I attempt an AJAX call. The AJAX call is made from a partial view which is embedded in more than one page. It works fine on all of the pages except one. I have read posts pointing to the web.config file settings and .axd mappings as possible solutions, but the application is configured correctly in the web.config, and the .axd mappings are also correct in IIS. Plus it works fine on all of the pages that use this partial view except one. It is acting like the AJAX libraries are not loading for this one page.
The references to the script files are in the shared site.master file. All of the pages, including the one that doesn't work, reference the same masterpage.
Any ideas? I have been working on this one for two days now. Thanks.
EDIT: As Sam pointed out below, it would seem like the AJAX call is firing before the AJAX libraries have a chance to load. But, the AJAX call is triggered by a submit button long after the page has rendered, so the AJAX libraries have had plenty of time to load - sorry for not giving enough information the first time.
In web.config add the following line of code under appsettings tag:
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
Just in case... use the following to avoid path issues
<script src="<%= Url.Content("~/Scripts/MicrosoftAjax.debug.js") %>"
type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/MicrosoftMvcAjax.debug.js") %>"
type="text/javascript"></script>
Source: http://msdn.microsoft.com/en-us/library/dd381533.aspx
Thanks,
Arty
Load the page in Firefox, then use Firebug to inspect the page - you will be able to see all the individual scripts that have been loaded, plus all the network requests that were issued, and whether they succeeded or not. This is better than trying to troubleshoot from the server perspective.
If you are using IE8, you can use the Developer Tools window, but I think Firebug is better - both tools support JavaScript debugging though.
The most likely problem is that you are running script in your partial view before the scripts it is dependent on have had a chance to load - make sure that any script calls you have inside your partial view will only run once the page has loaded, and not immediately during loading.
All the above cases are ok.But sometimes developer forget to add javascript files for ajax .So please check that also.
Basically you might be missing: MicrosoftMvcAjax., MicrosoftMvcValidation.debug and MicrosoftMvcValidation JS file references.
Do the below steps:
PM> Install-Package MicrosoftAjax
PM> Install-Package MicrosoftMvcAjax.Mvc5
Include them in bundleconfig like below:
bundles.Add(new ScriptBundle("~/bundles/mvcFoolProof")
.Include("~/Scripts/MicrosoftAjax*",
"~/Scripts/MicrosoftMvcAjax*",
"~/Scripts/MicrosoftMvcValidation*",
"~/Scripts/mvcfoolproof*",
"~/Scripts/MvcFoolproofJQueryValidation*",
"~/Scripts/MvcFoolproofValidation*"));
Now it should work without any errors.
Add to web.cofig in section:
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
Regarding your response to Sam, one thing I've noticed in a lot of MVC apps is that people don't know how to deal with the ambiguity of relative paths and the application/runtime. For example, the URL rewriting pretty much guarantees that a particular page can appear at different depths than you anticipated, so ../../images will point somewhere else depending on whether you're looking at /products/widget or /products/widget/12345, even though the view might be the same. As Arty pointed out, the best way to deal with this is to let the engine do the work for you by using a URL utility and application-relative paths that will be fixed up by the application regardless of the context.
I have also found that using the following works with ASP.NET MVC2.
Instead of using MicrosoftMvcAjax.js, you use MicrosoftMvcValidation.js
Hope this helps someone.

Resources