Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

21 March 2017

Versioning .NET builds with MsBuild Tasks using TFS Revision or Git Revision

MSBuild - Detecting latest TFS Revision on Project Folder level using tf.exe history and replaces the Revision in AssemblyVersion and AssemblyFileVersion for AssemblyInfo.cs prior to building the project with CustomTasks in csproj.

Inspired by

I set off to create an MsBuild Tasks file that could be imported into csproj files and that calls tf.exe history as command line to get the latest revision on a project folder and embedding in the build assembly version revision number. I did not want this to be hidden in a build server step and I wanted the code to be open if a developer needed to investigate the tasks.







The end result is shared in this Gist:

(Updated with Git support on 2017-07-28)

13 December 2016

Adding Visual Studio Code Map Scrollbar

Since Visual Studio 2013 a code map has been available instead of a plain scroll bar.
Toggling is done from "Scroll Bars" in the settings tab either under All languages or under a specific language. 

I prefer this setting, with the "Wide" Source overview.



15 September 2016

Batching Commands for SQL and Optimizing network round trips.

Optimizing Database Access And Diving Into .Net SqlClient by Mladen Prajdić
http://www.sqlpass.org/24hours/2016/summitpreview/Sessions/Details.aspx?sid=53738

I was watching the session and found that I would like to add to the batch commands section. Back in 2003 i wrote a query optimization model that still works to this day that i would like to share.

Problem 

You have a lot of data you need to import, pushing it one row at a time is very inefficient, but what is the "right" amount of queries to push.

Solution

SQL commands are sent through a network packet to the server, these packets are default 4096 bytes / 4KB in size, this can be defined on the system but most leave this setting intact. This means that if you write an insert for one row and sends that to the server there are plenty of unused space in the packet of 4096. 

So the task at hand is very simple, you must optimize your query so that you send as many queries in one packet of 4096 but without going over, since it then sends the overflowing query in another packet with trailing empty space as a result. 

If you watch the session by Mladen Prajdić there is a reference to a System.Data.SqlClient.SqlCommandSet that is used internally by the .Net framework, my guess is that it works similarly and tries to optimize the network traffic to the server. 

A more brutal implementation (clean and simple) is the write a batch query engine yourself and measuring the byte length of the resulting query and then only pushing it to the server when the byte length is close to 4096 but not over. 

I have shared my base code for this brutal approach on https://gist.github.com/janhebnes/726811a5ba0d36c9de7c323ae177bf22

20 June 2016

Making Umbraco or any .Net CMS respect default documents

HttpHandler that helps respect the Request flow and serves the default documents of a directory instead of allowing e.g. Umbraco to kidnap the request for there purposes.

IIS does not serve the Default Document when set in Integrated pipeline mode. E.g. after installing Umbraco the httpModules will catch any traffic to the site and will return a 404 page instead of actually passing to the existing file system. In this case I wanted to add umbraco on top of an old asp and html based site. We did not want to move large amount of archive content, so using this modules allows for the classic Default Documents to be respected even if you are in a modern setup where Default Documents are being ignored. So first we check the file system for a response document if non is found the request is allowed to continue to Umbraco, allowing us to extend the existing content with Umbraco instead of having to nuke everything.

18 December 2015

C# Sending secure e-mails that are signed and encrypted

Securing email content can be done by sending the email in a closed network setup, directly to the receiving email server or by encrypting the content of the email. If you require the content to be really confidential you should go all the way and authenticate the sender by signing the email with a private certificate from the sender and encrypt the same email with the public certificate of the receiver.

This opens up a couple of failure points since certificates expire.
The following Gist handles both the signing and the encrypting but also takes care of notifying when the certificates have or are going to expire, and handles when they are expired.

17 December 2015

Turn on Compile-time View Checking for ASP.NET MVC

The .cshtml files are not natively compiled and therefore cheched for spelling mistakes at compile time. This can be changed, e.g. at build server level.

<target condition="'$(MvcBuildViews)'=='true'" name="AfterBuild">
<aspnetcompiler physicalpath="$(ProjectDir)\..\$(ProjectName)" virtualpath="temp"></aspnetcompiler>
</target>

<propertygroup>
<mvcbuildviews>true</mvcbuildviews>
</propertygroup>

This has been an option since 2010, not many know about it.
http://blogs.msdn.com/b/jimlamb/archive/2010/04/20/turn-on-compile-time-view-checking-for-asp-net-mvc-projects-in-tfs-build-2010.aspx

16 December 2015

Sitecore Code Generation based on T4

Sitecore Code Generation based on T4 and extending the sitecore base template fields with class information for mapping the c# code to correct types.

AutoGenerated.tt is used by Visual Studio to generate the .cs file with all the template classes.
T4XmlProvider is the service that generates the Xml that is required by the AutoGenerated.tt to function.
Fields are mapped to types with information in the configuration and information stored directly on the Sitecore template field.

This model was developed back in 2010 and now shared with everyone, we no longer use the model but it can serve as an inspiration to others.

C# Operator ^= boolean flag toggle

Reviewing some code I spotted an interesting ^= operator that I had not seen before.

isBackgroundWindowOpen ^= true; // Toggles boolean flag.

C# Operators reference:
https://msdn.microsoft.com/en-us/library/6a71f45d.aspx

x ^= y – XOR assignment. XOR the value of y with the value of x, store the result in x, and return the new value.