Codepolice

Random posts from Ola in English. Mainly about programming and the web.

New Relic and runAllManagedModulesForAllRequests flag in .net

I have been playing around with the awesome performance measuring tool New Relic lately and have been struggling with some performance issues that would have been really hard to find without measuring the actual metrics from the real production server.

Anyway, during the different changes i made i changed the option runAllManagedModulesForAllRequests from “true” to “false” in Web.Config and all of a sudden i got much worse numbers from New Relic compared to before this change. That was pretty weird since everyone said that this should be off for performance reasons.

Pretty soon i realized that the reason that the numbers got worse was because it stopped measuring the performance of scripts, style sheets and images which of course is really fast.  Now i will continue with the mystery of the “System.Web.Handlers.TransferRequestHandler” method that is making my app slower for some reason.

Update Check this thread on Stack Overflow for some more info about the System.Web.Handlers.TransferRequestHandler problem.

Run Dropbox As a Windows Service on Windows Server 2008

I know there is a lot of post about this already but i ran into some issues and had to collect some info from various places so i thought i might as well share my experience with the world. The goal is to be able to run Dropbox (or any other application that doesn’t have built in supported for services) as a service in Windows. This means you do not have to login to your computer to start Dropbox. It will start as soon as you turn on your computer and Windows has loaded.

  1. Download Windows Server Resource Kit 2003 and install it.
  2. You must of course have Dropbox and when it’s installed and running right click on it in the taskbar and choose preferences. Then uncheck “Start Dropbox on System Startup” and “Show Desktop Notification”. It should look like the image below.
  3. Quit Dropbox.
  4. Start cmd.exe and enter the following commands.

    Note 1! I got this error [SC] DeleteService FAILED 1072: when i executed the sc.exe the first time. I found this Stack Overflow thread that pointed out that you couldn’t have the Services Management windows open when doing this.

    Note 2! The other problem i had as i mention the comments was that the Windows SDK is installed to c:\program files (x86)\ and not “c:\program files\” as all the examples i saw had. When i tried to start the service i gor an error saying “The specified file cannot be found” and that was because it couldn’t found “srvany.exe”. I thought it couldn’t find Dropbox.exe but that wasn’t the case.

  5. When this is done start the “Services” Management App” (run -> search services and start “Services”). Right click on the “Dropbox Service”. Choose “Automatic” in the “Startup type” dropdown. Then click the “Log On” tab and choose “This Account”. Enter your current accounts username and password. This is needed otherwise your computer wont be able to access Dropbox.exe if it’s in a \users\ folder.

I think that was all the issues i had. My sources for this post are the following.

http://blog.dreamfactory.se/2011/03/11/installing-dropbox-as-a-service-on-windows-server-2008-r2-sp1-x64/

https://forums.dropbox.com/topic.php?id=46712

Send E-Mail When a task in Task Scheduler Fails

You would guess that this would be a really easy task but it turns out this was pretty complicated. Basically you need to create a schedule task that gets triggered by an event and do some editing of that task in notepad. I will just post my exported task here. Feel free to use it / modify it as you want.

Just save it as an XML file and import it in the Task Scheduler.

Using log4net to Send Mail With SmtpAppender In a Console Application

I recently built some console apps to do some testing on my server. I wanted to be able to run the apps and get it to print info to the console. I also wanted it to log to a text file and to send e-mails to me. Log4net was the perfect tool to get this done.

This is how my .config files looks with log4net. Of course you have to reference log4net and so on also, use nuget!

In the application i just init log4net like this.

I saw loads of examples where the used the “BasicConfigurator” instead of the “XmlConfigurator” for some reason but that did not work if i wanted to use the appenders specified in the .config files.

Anyway, since my console app is just running and then quits i had some problems with the SmtpAppender. I guessed it didn’t had time to send the mail before i exited the app but i found some code to solve this as well. Add this at the end of the app and you should be fine.

Another trick if you want to change the subject of the e-mail sent you can do like this.

Sorry for not linking to the sources of all this code but most is just stolen from various Stack Overflow questions.

Request.Files is Empty in a Web Forms Application

I was fixing some bugs in my application today and ran into a tricky bug. My application is based on Webforms but i use a lot of jQuery and other more modern technologies. On one place i have a upload form that is dynamically populated with jQuery templates. Since i create all my <input type”file”> with jQuery i can’t use ASP.NET’s built in <asp:FileUpload>, i can’t even have then as “runat=server”.

Today when i made some changes i removed the last file upload that had runat=”server” on it and a couple of hours later i noticed that all uploads had stopped working. After some debugging it hit me that i should check if the “auto generated” form that asp.net ads to each page actually had the enctype=”multipart/form-data” attribute still. And of course that was gone.

Naturally ASP.NET removed that attribute when i removed the last file upload that had “runat=server”. So if you do not want any file upload that has runat=server or <asp:fileupload> you have to add enctype manually to your form.

 

Build, Git Commit, FTP When You Publish In Visual Studio

For various reasons i have a really weird build setup in Visual Studio. I just want to write this post so i can remember how it works now if i ever re install my computer and something doesn’t work.

I use the regular publish feature in Visual Studio and deploy the code to a Website on my local IIS. This has always worked great and i don’t really want to mess with firewalls and stuff to make it deploy to any remote site on my server. That just seems complicated.

My first modification to my .csproj file (that actually is more or less a regular MS Build file) is a command to open a text file where i write the message that i want to use for a git commit later on. The reason why I’m doing this is because i always forget to commit to Git and i couldn’t figure out any other way to force me to enter something when i publish my app.

When i close notepad or the default text editor you use the build continues and when the build is complete i execute this command that runs a WinSCP script to sync my local folder with the remote folder on my server. You also see my second command to commit to Git there but ignore that for now.

The WinSCP script looks like this.

This works incredibly well.

At last i execute a PowerShell command to commit to GitHub using that message i wrote earlier. I use Posh-Git for PowerShell. My PowerShell script looks like this.

Well, that’s it. I understand i don’t explain this in much detail but as i said before i think this scenario is very unique to me and my situation. But feel free to ask any questions if you have similar needs.

Query the MySQL / WordPress Database from an ASP.NET site

We run a community site on ASP.NET Webforms that also has a blogged based on WordPress. For a couple of years now we have loaded the data from WordPress to the ASP.NET site via WordPress RSS Feeds but we have had issues with this for a long time.

Some days ago i decided to just query the MySQL database that WordPress uses directly from .NET instead of going via any WordPress API. The whole process was kind of straight forward.

Connection to MySQL from .Net

To just do a simple connection you just need the MySQL Nuget package than it works pretty much like any ADO.NET connection.

 Query the WordPress Database to get Posts by Category

I needed to get my posts by category and it was kind of simple.

I also needed to get the “Attached Image” or “Featured Image” from WordPress.

 

Problems with gzip when using IIS 7.5 as an Origin server for a CDN

I recently decided to try out my friend Sajals new CDN service over at Turbobytes. While seting this up he pointed out some issues i had with my custom origin domain that i run on IIS. Apparently IIS has some problems that many CDN doesn’t like.

The “Via” Header

To begin with many CDNs send a “Via” header to notify the server that they are a proxy. And for some reason IIS doesn’t  serve gziped content to a server that does this. This means that many CDNs just doesn’t get gziped content from IIS and therfore doesn’t serve gziped content to your customer. To resolve this you have to change some settings in your applicationHost.config.

The thing to fix the problem with the “Via” header is the two attributes noCompressionForHttp10=”false” and noCompressionForProxies=”false” and they kind of explain themselves. And remember the weirdness with the applicationHost.config to not open them in Notepad++ or any other 32bit based apps. You must edit them with a 64 bit text editor like the built in notepad in Windows.

IIS doesn’t return the first request gzip by default

Another issue with IIS as a origin server is that it doesn’t return the first request of a file as gziped. I suppose they figured that for a normal user it’s faster to just return the content and then serve the gziped version to the next user. But a CDN of course only do one request for the file and then sits happy and serving that file. And if that file is not gziped then it will stay not gziped. But there is a fix for this as well. Just modify the same section in applicationHost.config as you’ve edited before and you should be fine. I got lot’s of help in this question on Stack Overflow.

The “Vary” Header

Another possible issue is that IIS doesn’t return a “Vary” header if the content is not gziped. This could cause issues if the first request to your file from any client doesn’t request a gziped file. Since the vary header is not set then the CDN doesn’t try to fetch a new file. As far as i understand it. I just added a custom header to my web.config. The problem with this is that it ads multiple values in the Vary header when i request gziped content. I don’t know if this is a problem or not. I’ve asked a question about this on Stack Overflow. Will update here when i get some more answers to this.

Problem with multiple Vary values.

 

Get Details From a Generic 500 Error on IIS 7 & 7.5

Sometimes, i have never really understood when and why, IIS throws a really silent 500 Error page that just says “The page cannot be displayed because an internal server error has occurred.” Not very helpful.

But it’s really easy to fix. Just head to your IIS, go to the site you want to change, enter “Error Pages”, right click on 500 Errors and change it to detailed.

Tip from here.

 

Note To Self: 500.19 Error from IIS

Every time i set up a new environment i get some errors and one that hits me every single time is the 500.19 error. It has various reasons as i have mentioned before. But one of the main reasons is that i always forget to install the Url Rewrite Module to IIS. The 500.19 is really silent about this so it always take some time to figure out.