Tag Archives: .net

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>

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.

Don’t do iisreset do …

a mini batch file in your start menu, when you’re debugging the application startup of a .Net application, and keep wanting to restart it:

taskkill /im aspnet_wp.exe /f
if not errorlevel == 0 pause

or

taskkill /im w3wp.exe /f
if not errorlevel == 0 pause

which does the job much faster.

Assuming, of course, that your reason for wanting to do an iis reset was to force a .Net application restart. And you don’t mind forcing all running .Net applications to restart. And you’re not on a busy production machine at rushhour.

Why are my TFS local folder paths ridiculously long?

Well, without actually answering the question (oh, okay then – it’s because they usually match the branch and directory path in TFS of course, but you knew that anyway) what you really wanted to know was how to fix it.

Easy. You want to play with the rows in File – Source Control – Workspaces – Edit…

Specifically, instead of one row listing the TeamProject root like this:

Source ControlFolder Local Folder
$/MyTeamProject C:\MyLocalWorkspaceRoot\

You want several rows, one for each solution you are currently working on, listing the actual solution file directories in TFS that you personally work with, and the – much shorter – local path you want to check out in:

Source ControlFolder Local Folder
$/MyTeamProject/Path/To/BranchXX/SolutionAA/ C:\MyLocalWorkspaceRoot\XX-AA
$/MyTeamProject/Path/To/BranchYY/SolutionBB/ C:\MyLocalWorkspaceRoot\YY-BB

Now you get just the files you want, without clicking through 10 levels of empty directories.

Workspace 101. Martin Woodward