Tuesday 19 October 2010

Pre-generate Xml Serialisation of a WebService Proxy to speed load time

Problem

Microsoft .NET 2+ will dynamically create Xml Serialisation code when instantiating a WebService proxy.  If the WebService is a large one with many types and methods defined then the load time can be unacceptably slow.

Solution

The solution is to use .NET tools to pre-generate the serialisation code and then replace the serialisation attributes in the proxy with a reference to the generated dll.

The following PowerShell script automates the process for a WSDL file, MyService.wsdl:

# This Powershell script assists in the creation of a WebService proxy that loads in a reasonable time.
# .NET will usually dynamically generate Xml Serialization code when the service proxy is instantiated.
# This results in a ~1min delay.
# In order to speed the load time it is necessary to pre-generate the Xml Serialization code.
# You can then replace the Xml Serialization attributes with a single reference to the pre-compiled dll.
# The script below automates the process.

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 0 -currentOperation 'Set Enviromnent'
$env:path = "C:\WINDOWS\system32; C:\WINDOWS; C:\WINDOWS\system32\WindowsPowerShell\v1.0; C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319; C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\NETFX 4.0 Tools; C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin"

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 1 -currentOperation 'Create basic proxy'
wsdl /n:MyWebServiceProxy MyWebService.wsdl

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 10 -currentOperation 'Compile basic proxy'
csc.exe /t:library /out:MyWebServiceProxy.dll MyWebServiceProxy.cs

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 20 -currentOperation 'Build proxy serialisation'
sgen.exe /p /force MyWebServiceProxy.dll

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 50 -currentOperation 'Remove XmlIncludeAttribute from proxy'
Get-Content MyWebServiceProxy.cs | % {$_ -replace '\[System\.Xml\.Serialization\.XmlIncludeAttribute', '//[System.Xml.Serialization.XmlIncludeAttribute'} | Set-Content -path MyWebServiceProxy_SerialisationRemoved.cs

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 70 -currentOperation 'Add XmlSerializerAssemblyAttribute to proxy'
Get-Content MyWebServiceProxy_SerialisationRemoved.cs | % {$_ -replace 'public partial class MyWebService', '[System.Xml.Serialization.XmlSerializerAssemblyAttribute(AssemblyName = "MyWebServiceProxy.XmlSerializers")] public partial class MyWebService'} | Set-Content -path MyWebServiceProxy_SerialisationAdded.cs

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 90 -currentOperation 'Compile new proxy'
csc.exe /t:library /out:MyWebServiceProxy.dll MyWebServiceProxy_SerialisationAdded.cs

Write-Progress -activity 'Building Proxy' -status 'Progress->' -percentcomplete 100 -currentOperation 'Finished.'



You’ll need to change the file and class names appropriately for your case or perhaps add parameters to the script.



 



What’s going on



Step by step:




  1. Add the locations of the tools used to the path environment variable.


  2. Use wsdl.exe .NET SDK utility to generate the proxy c# code file from the wsdl file.


  3. Compile the c# proxy into a dll.


  4. Use sgen.exe .NET SDK utility to generate an Xml Serialisation dll for the proxy.


  5. Using a regular expression strip out the XmlIncludeAttribute from the c# proxy.


  6. Using a regular expression insert a XmlSerializerAssemblyAttribute referencing the generated serialisation dll.


  7. Recompile the proxy dll with the reference to the serialisation dll.



References



With thanks to http://www.sharepoint-stuff.com/?p=199



Technorati Tags: ,

Thursday 24 June 2010

What is using a port on my machine?

Problem

My service connects to a TCP server over a particular port and when establishing the connection throws the exception: “SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted”.

Diagnosing the Issue

Use netstat at the command line to see what is using the port.

netstat –a
Displays all the ports in use.

netstat –b
Displays all the active connections and the process/exe that is using them

In my case a network error had occurred and I believe my service had left the client open, knew it was dead and so was trying to reconnect – throwing the exception.  Restarting the service resolved the issue but the code will need reviewing.

Monday 24 May 2010

Removing whitespace from HTML tags

The Problem

This situation occurs when using the .net 2.0 classes to transform XML into HTML with a style sheet the transform (System.Xml.Xsl.XslCompiledTransform).  the output will include whitespace inside of certain HTML tags that may occasionally display within form fields when output.  This can be a pain especially when displaying TextArea elements. In IE the behaviour appeared to change unexpectedly when some other element appeared before the TextArea – such as a dropdown list (select).

The Solution

A solution is to remove the whitespaces from these tags after it has been transformed.

private string CleanUpWhitespaceInTags(string xmlString)
{
return cleanUpWhitespaceInTagsRegEx.Replace(xmlString, cleanUpWhitespaceInTagsReplacement);
}

private static readonly Regex cleanUpWhitespaceInTagsRegEx = new Regex(@"<(?<tagName>[\w-]+)\b(?<attributes>[^>]*)>\s*(?<content>[^<]*?)\s*</\1>");
private const string cleanUpWhitespaceInTagsReplacement = "<${tagName}${attributes}>${content}</${tagName}>";


The above code uses a regular expression to match tags that have no inner xml/html and gets named captures of the tag name, it’s attributes and content (without any surrounding whitespace).



To break it down:

























































< match the opening tag’s starting brace
(?<tagName>[\w-]+) capture any valid tag name (containing one or more word characters or dashes)
\b check that we are at a word boundary
(?<attributes>[^>]*) capture any characters after the tag name and before an end brace as attributes
> match the opening tag’s end brace
\s* match any whitespace on the left of the content
(?<content>[^<]*?) capture any character that isn’t a starting brace but don’t be greedy
\s* match any whitespace on the right of the content
</\1> match the closing tag


Disclaimer: Whether this regex covers all scenarios has not been tested but it works for the situation we encountered.



The replacement string then reconstitutes the tag, attributes and content without the leading or trailing whitespace.



Technorati Tags: ,,

Friday 14 May 2010

Properly disposing a System.Threading.Timer

The following test fixture shows how a timer can be properly disposed in order to ensure that the thread executing the callback has completed (or we've timed out waiting for it):

[TestClass]
public class TimerFixture
{
[TestMethod]
public void Check_timer_dispose_behaviour()
{
System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(OnTick), null, 100, 0);

while (!isProcessing) Thread.Sleep(50);

WaitHandle waitHandle = new AutoResetEvent(false);

timer.Dispose(waitHandle);

isStopping = true;

WaitHandle.WaitAll(new[] { waitHandle }, 1000);

Assert.IsFalse(isProcessing);
}

private void OnTick(object state)
{
isProcessing = true;

while (!isStopping) Thread.Sleep(100);

Thread.Sleep(200);

isProcessing = false;
}

private bool isStopping = false;
private bool isProcessing = false;
}

Wednesday 24 March 2010

DevWeek 2010

Just got back from DevWeek 2010.  Here is a list of things I need to follow up on from the sessions I attended:

A Day of ASP.NET 4.0 : Fritz Onion

  • MemCache
  • Chart Control
  • GridView/ListView ‘remembers’ selected
  • Query Extender
  • Html Encode syntax and HtmlString class
  • AJAX 4.0
    • Script Loader/Manager
    • Sys.Require
    • Sys.onReady
    • CDN
    • Extender Controls.js
    • Client Templates {{propName}}
    • .sys-template class
    • Sys.create.dataView(“#id”, { data : data })
    • sys: prefix on attributes
    • Sys.bind(Sys.Get(“selector”))
  • Routing in ASP.NET Forms
    • RouteValue, RouteUrl, RouteParameter
  • Web Application rather than Web Site
  • Deployment:
    • Package/Publish, XDT in config files
    • IIS Import on farm
  • .svc services /js to get client proxy
    • [DataContract] [DataMember]
    • live binding {binding Title}
    • Sys.Observer.makeObservable(itemsArray);
    • Sys.Data.DataContext
  • ADO.NET Dynamic Data

97 Things Every Programmer Should Know : Kevlin Henney

  • Bought the book

Objects of Desire : Kevlin Henney

  • Parameter Objects
  • Pass in Context
  • Interpreter / Aggregator Patterns
  • Getter/Setter problem
  • Keep object valid at all times
  • Privatize
  • LSP
  • UML Inheritance Hierarchies
  • Layering: Concept <– Realisation
  • Layering: Domain –> Services –> Infrastructure
  • Apathy, ignorance, selfish
  • Names based on client usage not implementation

VSTS 101 – a beginner’s guide : Brian Randell

  • Project Collection (single db) > Team Project
  • Static Code Analysis
  • Code Metrics
  • Check-in policies
  • Team Build
  • Test impact analysis
  • VS Profiler?!
  • Mostly MS marketing

Agile DB techniques using VS 2010 : Giles Davies

  • Build Screen Saver, Brian the build bunny
  • Extension Properties for schema version number
  • Data Transfer Plan, Data Generation Plan
    • Data bound generator
  • Got to use this!

Intro to Sharepoint 2010 from ASP.NET devs : Fritz Onion

  • VS 2010 makes it easier.  Needs huge buy in.

Entity Framework in .NET 4 and VS 2010 : Eric Nelson

  • Entity Data Model (EDM)
    • Conceptual
    • Mapping
    • Storage
  • Lots of new goodness in 4.0
  • Insert Key
  • Foreign Key surfaced
  • ExecuteStoreQuery
  • EntityFunctions
  • Extension Manager
  • Feature Pack (POCO, code only, self-tracking)
  • Persistence Ignorance
  • http://bit.ly/ericnelson 

Code contracts and design for testability : Dino Esposito

  • Preconditions
  • Postconditions
  • Invariants
  • Interface contracts
  • Tools (rewriter, static checker, asm ref gen)
  • Contract Failure handling (in UT or logging)

Are singletons evil? : Andy Clymer & Kevin Jones

  • Singleton
  • Repository
  • Factory
  • Dependency Injection

Objects of value : Kevlin Henney

  • aka Attribute Object
  • No/insignificant identity
  • Create Value Objects that exist in the domain and ‘wrap’ simple types
  • Systems of Values
  • POVO
  • Patterns of Value : Immutable Value, Copied Value
  • Three finger rule: Identity, State, Behaviour
  • Smells: Anaemia and Multiple Stereotypes
  • Anatomy: Construction, Comparison, Classification and Conversion
  • Domain specific
  • Equals
  • My MonetaryAmount example

Programming with GUTs : Kevlin Henney

  • Things not to test in UT (perhaps in Integration tests)
  • POUT, TDD, DDT
  • Test per proposition not per method
  • Naming:
    • proposition not implementation
    • Underscores
  • Example based
    • Simple cases
    • Common cases
    • Boundary cases
    • Contractual error cases (rainy day)
  • Better roughly right than precisely wrong
  • Black box

Patterns dartboard : Andy Clymer & Kevin Jones

  • Adapter / Proxy
  • Null
  • Active Record
  • Strategy

Is the free lunch over? : Andy Clymer

  • Fork/Join (Parallel.Invoke, Tasks)
  • Pipeline (Queues, production line, stages)
  • Geometric decomposition (Break down the data)
  • Use Sequential version as UT?
  • Parallel Loops (loop of loops, Barrier)
  • Monte Carlo (educated guesses, best result in time T)

Use threads effectively to build responsive & scalable software : Jeffery Richter

  • Threads are bad
  • Threads are good
  • Async Programming Model (APM)
  • ThreadPool and Tasks
  • AsyncEnumerator