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

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>

Related

400.0 Bad Request on REST API on Azure Web Site

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).

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.

VirtualPathProvider doesn't (quite) work in production on IIS 7.5

I have been working on a project that has common bits of functionality, specifically I wanted to share the master file and related images/js/etc. To that end, the master page and its dependent files are all wrapped into a "global" DLL that is utilized by all "subprojects". This all worked great in development, but deployment yielded a surprise which seems to catch a lot of people off guard: VirtualPathProvider doesn't work when precompiled.
Now thanks to this blog post containing a workaround I was able to give another attempt at getting it to work. Regretfully, it still doesn't.
I opted to get rid of my Global.asax implementation and went with the blog post's AppInitialize approach:
public static class AppStart
{
public static void AppInitialize()
{
HostingEnvironment hostingEnvironmentInstance = (HostingEnvironment)typeof(HostingEnvironment).InvokeMember("_theHostingEnvironment", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
MethodInfo mi = typeof(HostingEnvironment).GetMethod("RegisterVirtualPathProviderInternal", BindingFlags.NonPublic | BindingFlags.Static);
mi.Invoke(hostingEnvironmentInstance, new object[] { new MasterPageProvider() });
}
}
Since the actual provider works in debug, I won't include it. If you would like to see it, don't hesitate to ask. Just wanted to keep the question as short as possible.
The interesting aspect to this whole situation is that production yields no errors about not being able to find the master page. To me, this means the provider is working, but for whatever reason the rest of the resources (js/css/etc) aren't being retrieved from the assembly properly.
So my question comes down to this: what are the reasons that this solution would work great in development, but not in production on IIS 7.5?
UPDATE 11/20/2011
Tried out David Ebbo's suggestion and had no results unfortunately. My web config looks something like this now:
<configuration>
<connectionStrings>
<clear />
<!-- ... -->
</connectionStrings>
<system.web>
<pages>
<controls>
<!-- ... -->
</controls>
</pages>
<compilation debug="true" targetFramework="4.0" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
UPDATE 11/21/2011
Just to verify my suspicion that the VirtualPathProvider was actually working, I commented out the third line (mi.Invoke(....) and redeployed the site. As I suspected, it now breaks due to not being able to find the MasterPage file. This issue appears to be related to only static files being delivered through the VPP.
IIS 7.5 will handle the static files itself. You need to put a line for each static file you want it to ignore in your web.config file to make them get routed through your VPP. See below for examples.
<system.webServer>
<handlers>
<add name="Images" path="*.png" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
<add name="Stylesheets" path="*.css" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" modules="ManagedPipelineHandler" resourceType="Unspecified" />
</handlers>
</system.webServer>
Maybe the problem is that requests for static files are not going through ASP.NET by default in IIS.
Try whether turning on runAllManagedModulesForAllRequests in web.config helps. e.g.
<modules runAllManagedModulesForAllRequests="true" />
Take a look at this post. It explains how to get static files through a virtual path provider in IIS 7. I believe this will solve your problem.

Resources