400.0 Bad Request on REST API on Azure Web Site - asp.net

I have migrated a REST service to an Azure web site, but it keeps coming back with a 400.0 Bad Request error (error code zero).
The service works locally, and works perfectly when running in a Web Role. After moving into a Web Site, it started coming back with the 400.0 error.
My web.config:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
I already have ASP.NET compatibility set in my class:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class BookingService { ... }
and the proper routing defined in Global.asax.cs:
private void Application_Start (object sender, EventArgs e) {
RouteTable.Routes.Add(new ServiceRoute("booking/service", new WebServiceHostFactory(), typeof(BookingService)));
}
Now when I try to access the REST API locally, it works fine:
http://localhost/booking/service/help
However, after uploading the web service into an Azure Web Site and doing:
http://xxx.azurewebsites.net/booking/service/help
it came back with a 400.0 Bad Request. Detailed error logs show:
Request: ManagedPipelineHandler
Notification: ExecuteRequestHandler
Handler: System.ServiceModel.Activation.AspNetRouteServiceHttpHandler
Error code: 0x00000000
I am at a total lost. Googling the web didn't yield anything helpful. I tried putting the following into web.config:
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="Foo.RoutingHandler" />
</handlers>
and defining a handler by extending UrlRoutingHandler and defining VerifyAndProcessRequest but it still doesn't work.

Finally found the error after stripping the service off bit by bit.
What happened was:
RouteTable.Routes.Add adds a route that creates an object of class BookingService as the handler
The class BookingService has a static property that is initialized
This initialization statement failed (due to some reason not to mention here)
Therefore, when the routing service tries to create a BookingService class, the class's static initializers were first run and failed
As a result, the code never reached the routing service, and so the service failed at the AspNetRouteServiceHttpHandler handler
If the class failed not while running static initializers, but during object creation, it is likely that the routing service will then throw a more meaningful error pin-pointing the problem
In summary, if you see a routed request failing at the handler, it may be caused by class code that couldn't load (due to static initializers failing or some other reason).

Related

Log4Net working locally but not on remote Azure DB

I locally develop an ASP.NET MVC application and use Log4Net to log message on the local database with the following connection string (log4net.config):
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=.\;Initial Catalog=MyDatabase;Integrated Security=True" />
This is perfectly working (database entries are appearing in the Log table).
Now I'd like to log to remote azure database server, so I just changed the connection string into following:
<connectionString value="Data Source=mydb.database.windows.net,1433;Initial Catalog=MyDatabase;User ID=username#mydb;Password=mypassword;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;" />
I inserted my IP address in the firewall (as proof I can connect via SQL Studio on my machine to the azure db instance).
When debugging the application I don't see any exception caused by log4net.
I use castle-windsor to inject the ILogger where needed, here is my Installer:
public class LoggerInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.AddFacility<LoggingFacility>(f => f.UseLog4Net().WithConfig("Configuration\\log4net.config"));
}
}
The database on the azure DB instance is the exact copy of the local one (same create script).
But nothing appears in the remote database. What am I doing wrong? How can I find out the problem?
Thanks a lot
Log4net will never throw an exception if it fails. It is designed to not interact with you application. The fastest way of finding why lognet is not logging is by enabling the interal log4net logging:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="log4net.Internal.Debug" value="true"/>
</appSettings>
</configuration>
OR
<configuration>
...
<system.diagnostics>
<trace autoflush="true">
<listeners>
<add
name="textWriterTraceListener"
type="System.Diagnostics.TextWriterTraceListener"
initializeData="C:\tmp\log4net.txt" />
</listeners>
</trace>
</system.diagnostics>
...
</configuration>

Receiving 'This operation requires IIS integrated pipeline mode.' error when adding httpModule to VS web project

Can someone shed some light on what's going on? I have a website created using VS 2010. The following code is added by default by VS when adding an httpModule. When I run the app through Casseni, the highlighted line throws the error "This operation requires IIS integrated pipeline mode."
public void Init(HttpApplication context)
{
// Below is an example of how you can handle LogRequest event and provide
// custom logging implementation for it
**context.LogRequest += new EventHandler(OnLogRequest);**
}
#endregion
public void OnLogRequest(Object source, EventArgs e)
{
//custom logging logic can go here
}
My web.config file was updated as such:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<httpModules>
<add name="GlobalModule" type="MyApp.Global.GlobalModule, EduCarePro"/>
</httpModules>
</system.web>
<system.webServer>
<modules>
<remove name="GlobalModule"/>
<add name="GlobalModule" type="MyApp.Global.GlobalModule, EduCarePro" preCondition="managedHandler"/>
</modules>
</system.webServer>
Is there something else that must be configured in the Web.Config to prevent this error??
The answer to this question is that Visual Studio's development web server does not support this functionality. You must run this code on an a machine running IIS.
TAKEN FROM (And Worked for me too as i had same problem)
Link

new handler triggers debug failure in visual studio

I have an existing asp.net website that works. When I (F5) debug it works. However, I am working on a new IHttpHandler for the site. As soon as I add the <system.webServer><handler></handler></system.webServer> section to the web.config visual studio refuses to F5 debug with the error:
Unable to start debugging on the web server. The web server could not
find the requested resource.
With the handler in place, if I attach-to-process then I can successfully attach to the process (and with the System.Diagnostics.Debugger.Break(); line I can step through the handler's code). I also added my handler to a different website and was able to reproduce this issue.
My Environment: .NET 4.0, Visual Studio 2012, using local IIS in integrated mode on Windows 7.
While trying to sanitize the code to paste here, I ended up commenting out everything in my handler except the boiler-plate, and the issue still occurs. Here are the code snippets:
The handler class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MySvc
{
public class MyServiceHandler : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
//#if DEBUG
// System.Diagnostics.Debugger.Break();
//#endif
}
}
}
and the web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<clear/>
<add key="A1" value="sanitized"/>
<add key="A2" value="sanitized"/>
</appSettings>
<connectionStrings>
<clear/>
<add name="MyDatabase" connectionString="sanitized"/>
</connectionStrings>
<system.web>
<compilation debug="true"/>
<httpRuntime maxRequestLength="1048576" executionTimeout="3600"/>
<sessionState mode="SQLServer" cookieless="false" timeout="5" allowCustomSqlDatabase="true" cookieName="My.Session"
sqlConnectionString="sanitized" />
<machineKey
validationKey="sanitized"
decryptionKey="sanitized"
validation="sanitized" decryption="sanitized" />
</system.web>
<system.webServer>
<handlers>
<clear />
<add name="MyHandler" path="*.bwsvc" verb="*" type="MySvc.MyServiceHandler, MySvc" />
</handlers>
</system.webServer>
</configuration>
I've read through The Web Server Could Not Find the Requested Resource and many other articles along the same lines. None of it seems applicable to this situation, nothing mentions handlers causing problems.
Am I missing something in my handler or is this something Visual Studio doesn't support or some other issue?
I found the problem. It turns out the <clear /> in the <handlers> section was responsible for causing the problem. As soon as that line is removed, the debugger works again (and many other things too).
Since my application pool is in integrated pipeline mode, the handlers section actually inherits from the server settings which specify what handles such core things as *.aspx and such, so doing a clear on it means that IIS didn't know what to do with anything in my application.

How to write to OutputDebugString from ASP.net web-site?

i need to output some debugging information from code on a web-site.
How can i call OutputDebugString from an ASP.net web-site, and have it appear to users running DbgView?
Note: Web-sites do not support System.Diagnostics.Trace.TraceWarning(...).
Okay, here is a complete example. It's a console but the principles and the code are much the same. I couldn't test capturing from OutputDebugString on my machine today because I don't have admin rights. On a server, you'd write to TextWriterTraceListener instead of a console. If you can't write and read from OutputDebugString using pinvoke, may the customer doesn't have the rights or the app doesn't have the necessary rights.
Also! If the Debug.WriteLine isn't showing up, maybe the website is compiled in RELEASE mode and DEBUG isn't define. TRACE by default is defined for RELEASE And DEBUG. TraceSource writes to OutputDebugString unless you've cleared the default listener, which a lot of people do as a matter of habit since OutputDebugString in my experience can slow things down esp if you aren't actually looking at the output at the moment.
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TraceToOutputDebugString
{
class Program
{
[DllImport("kernel32.dll")]
static extern void OutputDebugString(string lpOutputString);
static void Main(string[] args)
{
//Put these lines in your asp.net Page
OutputDebugString("Hello from Pinvoke OutputDebugString");
TraceSource trace = new TraceSource("app");
trace.TraceInformation("Hello from TraceSource");
Trace.TraceInformation("Hello from 1.1 Trace.TraceInformation");
Debug.WriteLine("Hello Debug.WriteLine");
System.Console.ReadKey();
}
}
}
And here is the config.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.diagnostics>
<sources>
<source name="app" switchName="app">
<listeners>
<add name="Console" type="System.Diagnostics.ConsoleTraceListener"/>
</listeners>
</source>
</sources>
<switches>
<add name="app" value="Information"/>
</switches>
<trace>
<listeners>
<add name="Console" type="System.Diagnostics.ConsoleTraceListener"/>
</listeners>
</trace>
</system.diagnostics>
</configuration>

Custom HttpHandler Error: Could not load type 'FileProtectionHandler'

I am trying to implement a Custom HttpHandler (for the first time), I have been given a tutorial to follow but couldn't get it to work. I then found another tutorial but couldn't get that to work, they are both giving me the same error message.
The custom handler is to protect people from downloading certain file types, although i think the error is somekind of configuration problem as I can't get the website to work at all once I add the httpHandlers to the Web.Config file.
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Could not load type 'FileProtectionHandler'.
Source Error:
Line 47: </compilation>
Line 48: <httpHandlers>
Line 49: <add verb="*" path="*.pdf" type="FileProtectionHandler"/>
Line 50: </httpHandlers>
If you require more code please let me know.
Thanks for any help. J.
<%# WebHandler Language="VB" Class="FileProtectionHandler" %>
Imports System
Imports System.Web
Imports System.Web.Security
Imports System.IO
Imports System.Web.SessionState
Public Class FileProtectionHandler : Implements IHttpHandler
Private Function SendContentTypeAndFile(ByVal context As HttpContext, ByVal strFile As [String]) As HttpContext
context.Response.ContentType = GetContentType(strFile)
context.Response.TransmitFile(strFile)
context.Response.[End]()
Return context
End Function
Private Function GetContentType(ByVal filename As String) As String
' used to set the encoding for the reponse stream
Dim res As String = Nothing
Dim fileinfo As New FileInfo(filename)
If fileinfo.Exists Then
Select Case fileinfo.Extension.Remove(0, 1).ToLower()
Case "pdf"
If True Then
res = "application/pdf"
Exit Select
End If
End Select
Return res
End If
Return Nothing
End Function
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.ContentType = "text/plain"
context.Response.Write("Hello World")
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
I had similar problem. Solution was in root namespace defined in properties.
In my code I do not have namespace, so in this case you need to use
type="[namespace or root namespace].[your class name]"
Try filling out the namespace that the class lives in as well as the assembly it's built to.
Something like this
<add verb="*" path="*.pdf" type="FileProtectionHandler, Beswick"/>
or possibly this
<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler, Beswick"/>
or this
<add verb="*" path="*.pdf" type="Beswick.FileProtectionHandler"/>
Just had the same problem adding a new IHttpHandler to an existing project. The handler I added had build action property "Content" rather than "Compile". Changing it to compile fixed the issue
.NET 4.5 WebForm ,it was fixed for me after adding the ProjectName.ClassName
<httpHandlers>
<add verb="*" path="scripts/*" validate="false" type="ProjectName.NoAccessHandler"/>
</httpHandlers>
and I have extra part not positive if it actaully do anything under system.webServer -> Handlers I have this
<Handlers>
<add verb="*" path="scripts/*" name="NoAccessHandler"
preCondition="integratedMode" type="NoAccessHandler"
resourceType="Unspecified"/>
</Handlers>
I've just come back to this issue after a lengthy break from it. I'm not sure if i've got it fully working as yet as from first testing it's not protecting the file if a user isn't logged into the website, but I am no longer getting the error message.
I found the fix to the problem here: HttpHandler 101 FAIL
If none of these answers works, and your project is a Web Appliccation (as opposed to a Web Page as in HttpHandler 101 FAIL), check the build output path. I had recently changed my platform to x86, which changed Properties ->Build -> Output path to
bin\x86\Debug
I changed this back to
bin\
and it worked.
I encountered a similar error while debugging an Azure Web App locally (the error persisted when deployed to Azure). I suspect that the error has something to do with locally stored configuration/compilation files, which are not updated properly even when the solution is cleaned and rebuilt. I had two different projects which produced identically named dll's (albeit to different locations), not sure if this had any effect on the issue.
After lengthy experimentation, the solution for me was to go to Solution Explorer in Visual Studio, right-click on the project --> Properties. Under the Application tab, change the Target framework to something else (I changed 4.6 to 4.6.1). You will get a prompt saying that the project will be reloaded, click OK. After reload, do the same thing reverting back to your original version (4.6 for me). This fixed the issue for me.
It would be nice to understand the root cause of the issue. I still get the error sometimes when reopening the project, and I have to go through the above steps again.
Changing the order and keeping as below in config file fixed my issue.
<system.webServer>
<handlers>
<remove name="traceverbhandler" />
<remove name="optionsverbhandler" />
<add name="extensionlessurlhandler-integrated-4.0" path="*." verb="*" type="system.web.handlers.transferrequesthandler" />
<remove name="extensionlessurlhandler-integrated-4.0" />
</handlers>
</system.webServer>

Resources