
December 21, 2012 10:25 by
Jervis
In this blog post I will show how to implement a custom XmlMediaTypeFormatter that extends the default ASP.NET Web API XmlMediaTypeFormatter in a way that it ignores XML namespaces when parsing xml messages.
By default the ASP.NET Web API XmlMediaTypeFormatter is not able to parse XML requests that contain any XML namespace declarations. If you would like to support clients, that (for any reason) send messages containing XML namespaces you can use the IgnoreNamespacesXmlMediaTypeFormatter that is defined as follows:
public class IgnoreNamespacesXmlMediaTypeFormatter : XmlMediaTypeFormatter
{
// See http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
private const string NamespaceRemover =
@"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='xml' indent='no'/>
<xsl:template match='/|comment()|processing-instruction()'>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match='*'>
<xsl:element name='{local-name()}'>
<xsl:apply-templates select='@*|node()'/>
</xsl:element>
</xsl:template>
<xsl:template match='@*'>
<xsl:attribute name='{local-name()}'>
<xsl:value-of select='.'/>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>";
private readonly XslCompiledTransform _xlstTransformer;
public IgnoreNamespacesXmlMediaTypeFormatter()
{
var xslt = XDocument.Parse(NamespaceRemover, LoadOptions.PreserveWhitespace);
_xlstTransformer = new XslCompiledTransform();
_xlstTransformer.Load(xslt.CreateReader(), new XsltSettings(), new XmlUrlResolver());
}
public override Task<object> ReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
try
{
// Read XML
var xmlDocument = XDocument.Load(new XmlTextReader(stream));
// Transform XML
var resultStream = new MemoryStream();
_xlstTransformer.Transform(xmlDocument.CreateReader(), XmlWriter.Create(resultStream, new XmlWriterSettings() { OmitXmlDeclaration = true }));
resultStream.Position = 0;
// Process request with XmlMediaTypeFormatter default functionality
return base.ReadFromStreamAsync(type, resultStream, contentHeaders, formatterLogger);
}
catch (XmlException)
{
return base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
}
}
}
In detail the IgnoreNamespacesXmlMediaTypeFormatter removes the XML namespace declarations from the XML message and passes the modified XML to the base class to use the default XmlMediaTypeFormatter functionality. Removing the XML namespaces is done with a XSLT transformation (see http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl)
To activate the IgnoreNamespacesXmlMediaTypeFormatter add the following lines in the file Global.asax.cs:
protected void Application_Start()
{
[...]
// Remove default XmlFormatter and add (customized) IgnoreNamespacesXmlMediaTypeFormatter GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
var ignoreNamespacesXmlMediaTypeFormatter = new IgnoreNamespacesXmlMediaTypeFormatter{ UseXmlSerializer = true };
GlobalConfiguration.Configuration.Formatters.Add(ignoreNamespacesXmlMediaTypeFormatter);
[...]
}

December 21, 2012 04:55 by
Jervis
Visual Studio 2012 introduces another great feature for creating a user control out of the existing code. Till now we used to create different User controls and use it in main page. Now, with the introduction of Visual Studio 2012, we are developing the pages and generate the User controls from the existing page design.
For our sample, I used the default ASP.Net web application created by the Visual Studio. The Default.aspx page looks like the below.

Let us create our new User Control from the ordered list specified in the Default.aspx. Select the Code Snippet and right click to open the Context Menu.

From the Context Menu, select “Extract to User Control” option. This will open the “Save as” window, where we need to specify the User Control name and select OK.

Now, Visual Studio will create the new “MyUserControl” user control and modify the Default.aspx page to utilize the same.
MyUserControl is generated using the selected ordered list code as shown below.

Notice the changes in the Default.aspx page. Register tag for registering the user control is added on top of the page. Selected code snippet [in our case ordered list code] is replaced by the user control declaration.

Note: Visual Studio will copy only the designer code; it won’t copy the corresponding code behind code like the event handlers associated with the code snippet.

December 14, 2012 09:31 by
Jervis
Microsoft’s SQL Server Express is a fantastic product for anyone needing a relational database on a limited budget. Server Express is free but it comes with a few limitations such as only utilizing 1 GB of RAM, databases are limited to 10 GB, and it does not include SQL Profiler. For low volume sites that do not need enterprise level capabilities, this is a compelling solution. Here is a complete SQL Server feature comparison of all the SQL Server editions.
There is one other important limitation that needs to be mentioned which is that SQL Server Express does not include SQL Agent. This means that you can not schedule administrative tasks such as backing up your databases. As everyone knows backing up data is critical and with SQL Server Express you can still backup your databases but you have to do it manually. So what does one do if you don’t have a budget to license SQL Server but you need scheduled backups of your databases?
The answer is SQL Scheduler. SQL Scheduler is a simple lightweight application that installs on your server and runs as a service. Using the application’s GUI you can create multiple jobs to run your backups on a predefined schedule and you can even configure it to send you an email notification if it completes or fails to run properly.
After you download the program, unzip the archive and run InstallService.bat file from the command prompt. The installation will complete quickly.

Once you launch the program you just need to connect to your local SQL Server Express instance on your server. Here is how it will appear without any jobs configured.

From the File menu create a new job. In this example we’ll create 1 job to take a full backup of each database that is configured on the server.

Click on the Details tab and you’ll be able to enter your SQL statements for whatever task you’re trying to accomplish. Be sure to edit the script and change the path of where you want the database backups stored on your server.

On the schedule tab you can define the time and date of your job will run.

To have the job send you an email upon completion or in the event of a failure open the SQLScheduler.WindowsService.exe.config file and enter the mail server address along with the and username and password for authentication.
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from=”[email protected]”>
<network host="locahost" userName="x" password="x” />
</smtp>
</mailSettings>
</system.net>
Once the job runs a full backup will be created for each database and it will be stored in a subfolder of the path you specified in the script.
Now that your daily full backups are being created locally on the server the next step would be to either configure Windows Server Backup or configure 3rd party online backup solution.