LocalizedDisplayNameAttribute class for Asp.Net MVC2

For whatever reason, in MVC2 the DisplayName attribute isn't localizable in the way that ValidationAttributes are - it doesn't have a constructor that looks up resources.

So here's how to localise DisplayName:

public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
    public LocalizedDisplayNameAttribute(Type resourceType, string resourceKey) : base(LookupResource(resourceType, resourceKey)) {  }
    public LocalizedDisplayNameAttribute(Type resourceType) : base(LookupResource(resourceType, DisplayNameAttribute.Default.DisplayName) ) { }

    internal static string LookupResource(Type resourceType, string resourceKey)
    {
        return new ResourceManager(resourceType).GetString(resourceKey) ?? resourceKey;
    }
}

Note! This will all fail dismally unless ... your resource file is marked as public, rather than internal. Because Views are not compiled as part of the web project assembly, rather they are compiled at runtime by the asp.net compiler into a different assembly

A no-code routing service using .Net 4 on IIS

A year ago I was working with Waheed Hussain who pointed out that the .Net framework includes a Routing namespace which allows you to implement a service router with zero code. Literally. It's an IIS application with no code, just a web.config. Here's an example:

< ?xml version="1.0"?>
<configuration>
	<system.serviceModel>
		<routing>
			<filters>
				<filter name="MatchAll" filterType="MatchAll" />
			</filters>
			<filtertables>
				<filtertable name="MyFilterTable">
					<add filterName="MatchAllFilter" endpointName="MyDestinationEndpoint" priority="0"/>
				</filtertable>
			</filtertables>
		</routing>

		<services>
			<service behaviorConfiguration="routingConfiguration" name="System.ServiceModel.Routing.RoutingService">
				<endpoint address="destinationUrl/" binding="basicHttpBinding" name="routerEndpoint1" contract="System.ServiceModel.Routing.IRequestReplyRouter" />
			</service>
		</services>

		<client>
			<endpoint name="MyDestinationEndpoint"
					  address="https://Destination.1.Host.com/destinationUrl/"
					  binding="basicHttpBinding"
					  bindingConfiguration="basicHttpBindingWithClientCertificate"
					  behaviorConfiguration="clientEndpointCredential"
					  contract="*" />
		</client>

		<behaviors>
			<servicebehaviors>
				<behavior name="routingConfiguration">
					<routing routeOnHeadersOnly="true" filterTableName="MyFilterTable" />
					<servicedebug includeExceptionDetailInFaults="true" />
					<servicemetadata httpGetEnabled="true"/>
				</behavior>
			</servicebehaviors>
			<endpointbehaviors>
				<behavior name="clientEndpointCredential">
					<clientcredentials>
						<clientcertificate storeName="My" 
                               storeLocation="LocalMachine" 
                               x509FindType="FindBySubjectName" 
                               findValue="MyClientCertificate" />
					</clientcredentials>
				</behavior>
			</endpointbehaviors>
		</behaviors>

		<bindings>
			<basichttpbinding>
				<binding name="basicHttpBindingWithClientCertificate">
					<security mode="Transport">
						<transport clientCredentialType="Certificate"/>
					</security>
				</binding>
			</basichttpbinding>
		</bindings>

		<!-- The clever bit - activate with no code needed -->
		<servicehostingenvironment>
			<serviceactivations>
				<add relativeAddress="Destination.1.Host.com.svc" 
             service="System.ServiceModel.Routing.RoutingService, System.ServiceModel.Routing, version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
			</serviceactivations>
		</servicehostingenvironment>
	</system.serviceModel>

	<system.web>
		<compilation debug="true" targetFramework="4.0" />
	</system.web>

	<system.net>
		<settings>
			<servicepointmanager expect100Continue="false" />
		</settings>
	</system.net>
	
</configuration>

Escape Characters in Windows Command Line

Escape Characters in Windows’ CMD.EXE doesn't entirely work for me. I wanted to embed or escape quote characters in a command line, to give to the binPath parameter of sc.exe to create a windows service. But this contribution from John McNelly in the comments did give the solution:

John McNelly •  Jun 4, 2009 @1:10 pm

You can also use ^ as a line continuation character for readability.
To escape an embedded ” use a backslash, useful with paths within “c:\Program Files” :

sc.exe create Foo ^
binPath= "\"C:\Program Files\foo.exe\" \"C:\Program Files\foo.ini\"" ^
displayName= "Hello, world"

Thanks John.

What xml am I about to send to a service?

In C#, in Visual Studio Debugger, if you are about to send a message to a service but you want to see what you're about to send, do this:

System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(message.GetType());
s.Serialize(new System.IO.FileStream("c:\\temp\\OutgoingMessage.xml",System.IO.FileMode.Create), message)

Cut and paste it one full command (ie to the semicolon) at a time into the immediate window, and replace message with your actual variable.

Similarly for the return message:

result= serviceProxy.MyServiceMethod(message);
System.Xml.Serialization.XmlSerializer s2 = new System.Xml.Serialization.XmlSerializer(result.GetType());
s2.Serialize(new System.IO.FileStream("c:\\temp\\Result.xml", System.IO.FileMode.Create), result)

It's a quick hack for when you're in the middle of a debugging session - note it doesn't close the file properly - but great for when you've been caught on the hoof. If you do this regularly, you're better off using a developer's proxy like fiddler2 and using that to inspect messages.