Thursday, 25 February 2010

Friday, 19 February 2010

Network problems

A few notes on an issue I have been experiencing. I had the following C# code that constantly threw a timeout error on line 5:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api-verify.recaptcha.net/verify");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

using (Stream stream = request.GetRequestStream())
{
    using (StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII))
    {
        streamWriter.Write(postData);
        streamWriter.Close();
    }

    stream.Close();
}

ipconfig

Running ipconfig gives you nice information like your default gateway.

tracert

Running tracert for api-verify.recaptcha.net showed a hop to the default gateway (the first entry in the list of results) and nothing but timeouts thereafter.

Default gateway

A gateway is a routing device that knows how to pass traffic between different subnets and networks. It will know some routes to given IP addresses but not the routes to every address on the Internet. A gateway will will know the addresses of other gateways it can hand the traffic off to if the required route is unknown to it. A default gateway will be on the same subnet and is the gateway to be relied on when it doesn't know how to route traffic.

In this case, wrong default gateway?

Monday, 8 February 2010

Could not load file or assembly when setting up existing web application

When setting up an existing .NET application on a fresh machine you may see the following error:

"Could not load file or assembly 'Some.Assembly.Name' or one of its dependencies. Access is denied."

A potential quick solution is to go to “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files” and modify the security settings for the folder to grant "Full control" to the 'users' group.

Not ideal but it will get the development environment up-and-running.

Wednesday, 3 February 2010

Track active items in Solution Explorer

When opening a source file in Visual Studio, if you want the solution explorer to reflect the location of the file ensure the appropriate option is set:

Tools > Options > Projects & Solutions > General

Set the Track Active Items in Solution Explorer option.

Alternatively if you’ve got ReSharper use Ctrl-Shift-T.

Monday, 25 January 2010

Invalid ViewState and WebResource.axd errors

Form submission

ViewState includes a Message Authentication Code (MAC). The MAC is generated using a validation key on the server. When the form is posted back the server compares the MAC in the ViewState with one regenerated on the server. If they differ the ViewState is regarded as invalid.

The value of the MAC can change when:

  • Data in the ViewState has changed (e.g. a hack attempt)
  • Truncated form data (e.g. timeout)
  • Using Server.Transfer can cause it to happen
  • The validation key used to generate the ViewState MAC is different than the key being used to generate the MAC for comparison

Note that the validation key may be different on different servers in a farm. You must take steps to ensure the validation keys match across machines. Validation keys can be different across application pools. Validation keys can change if the application pool restarts (e.g. if the key is set to AutoGenerate).

To avoid MAC mismatches:

  • Don't use the ViewState if you don't need to.
  • Turn off MAC generation by setting enableViewStateMac=false in the page or web.config. NOT RECOMMENDED!
  • Prevent your application pool from restarting by disabling the auto recycle and idle timeout settings in the application pool.
  • Hard-code the MAC validation key so that it's always the same (good for web farms). Hardcode the key in the <machineKey> tag in the machine.config or web.config.

See http://www.developmentnow.com/blog/InvalidViewstate+Or+Unable+To+Validate+Data+Error.aspx for a detailed overview.

Web resources

Similar issues can affect web resources accessed via webresource.axd. Requests to web resources will include a ‘d’ parameter (decryption key?). As such it is subject to the same issues with the key changing as above. This key will be an encrypted version of the web resource identifier used to retrieve the resource from the appropriate assembly.

One way in which web resources can be used is in Ajax or other JavaScript enabled web controls by adding the [WebResource] attribute to reference JavaScript files stored as resources. When the control is rendered on the page a reference to the script file will be generated as a request to webresource.axd

Also be aware of ScriptResource.axd (which contains all of the client-side JavaScript for Ajax) in web.config files.

Problems with web resources etc can give rise to the “Padding is invalid and cannot be removed” errors.

See http://msdn.microsoft.com/en-us/library/system.web.ui.webresourceattribute.aspx and http://msdn.microsoft.com/en-us/library/system.web.handlers.scriptresourcehandler.aspx.

Wednesday, 13 January 2010

How to export details of scheduled tasks

Run the following command to export all scheduled tasks to CSV format:

schtasks /query /FO CSV /V > c:\output.csv

This can then be opened with Excel. Alternatively, to make a copy/backup of all the scheduled tasks on a machine right-click on the %systemroot%\Tasks folder and copy it. Paste it where you want.

Wednesday, 13 January 2010

Tuesday, 12 January 2010

Rebuilding execution plans

To force SQL Server to rebuild its execution plans for a particular database run the following SQL:

DECLARE @intDBID INTEGER SET @intDBID = 
(SELECT dbid FROM master.dbo.sysdatabases WHERE name = 'DatabaseNameHere') DBCC FLUSHPROCINDB (@intDBID)

To rebuild execution plans for all databases run the following SQL:

DBCC FREEPROCCACHE

You can see the execution plans for a procedure by using the SET command:

USE [DatabaseNameHere]
GO

SET SHOWPLAN_ALL OFF 
GO

EXEC [WidgetQueries_RecentlyAddedContent] 0
GO

You can also force a recompilation for a specific procedure or trigger using sp_recompile stored procedure:

sp_recompile [ @objname = ] 'object'

There are many other options. Check the documentation for details.