AsyncLock

A naive implementation of async lock

https://github.com/ebalynn/AsyncLock

Behind the scenes the implementation uses SemaphoreSlim to do all the heavy lifting which already has a support for async/await.

You would normally use AsyncLock within a “using” statement. To keep the implementation as simple as possible I do not use any cancellation tokens for any of the operations.

Usage:

private readonly AsyncLock _lock = new AsyncLock();
public async Task DoSomethingAsync()
{
  using (await _lock.EnterAsync())
  {
    await Task.Delay(TimeSpan.FromSeconds(1));
  }
}

Simples

Clean event handler invocation with C# 6

Jon Skeet's coding blog

The problem

Invoking event handlers in C# has always been a bit of a pain, because an event with no subscribers is usually represented as a null reference. This leads to code like this:

It’s important to use the handler local variable, as if instead you access the field twice, it’s possible that the last subscriber will unsubscribe between the check and the invocation:

Now this can be simplified slightly using an extension method:

Then in each event, you can write a single line:

However, this means having a different extension method for each delegate type. It’s not too bad if you’re using EventHandler but it’s still not ideal.

C# 6 to the rescue!

The null-conditional operator (?.) in C# 6 isn’t just for properties. It can also be used for method calls. The compiler does the right thing (evaluating the expression only once) so you can do…

View original post 407 more words

Visual Studio and Git

So it happens that the company I started working for yesterday uses Git for all their source control needs. Having heard a lot of positive stuff about Git, but having no experience of actually using it,I had to dig around to find some decent integration tools that work well with Visual Studio 2013.

To my surprise, as of Visual Studio 2012.2, Microsoft already supports Git (I had to double check this and pinch myself!) There are Visual Studio Tools for Git available for download directly from Microsoft and there is quite a chunky write up Getting Started with Git in Visual Studio and Team Foundation Service

Will try it out at work today!

Converting XML or JSON to C# Classes in Visual Studio

Kapil's space

In the Visual Studio 2012 IDE, a new feature is introduce to convert XML document into C# classes as a Serializable type.

In the .NET framework 4.5 there is another Paste Special option is exists in the Edit menu which enables you to copy the XML as C# Classes.

Paste-xml-as-classes.png

Copy the XML  you want to create a class/classes for, place the cursor in a class file on the location you want the code to be added and select the following menu items:

  • Edit.
  • Paste Special.
  • Paste XML as Classes.

And you’re done.

Suppose you have following XML File.
xml-in-vs-2012.jpg

And when you paste the above XML using Paste XML as classes. It will looks like that.

xml-as-classes.jpg

View original post

Configure Visual Studio 2013 for debugging .NET framework

In order to configure Visual Studio 2013 do the following in the Tools -> Options -> Debugging -> General menu:

  • Disable just my code
  • Disable step over properties and operators
  • Disable require source files to exactly match the original version
  • Enable .NET framework source stepping
  • Enable source server support

This is what you need to do:

RefsourceSetup

Application Domains

Application Domains

An application domain is the run-time unit of isolation in which a .NET program runs. It provides a managed memory boundary, a container for loaded assemblies and application configuration settings, as well as delineating a communication boundary for distributed applications. Each .NET process usually hosts just one application domain: the default domain, created automatically by the CLR when the process starts. It’s also possible — and sometimes useful — to create additional application domains within the same process. This provides isolation while avoiding the overhead and communication complications that arise with having separate processes. It’s useful in scenarios such as load testing and application patching, and in implementing robust error recovery mechanisms.

Application Domain Architecture

In most cases, the processes housing the application domains are created implicitly by the operating system — when the user double-clicks your .NET executable file or starts a Windows service. However, an application domain can also be hosted in other processes such as IIS or in SQL Server through CLR integration. In the case of a simple executable, the process ends when the default application domain finishes executing. With hosts such as IIS or SQL Server, however, the process controls the lifetime, creating and destroying .NET application domains as it sees fit.

Continue reading “Application Domains”

A Use for Partial Classes/Structs

The keyword partial has been with us for a while now. Until recently I couldn’t find a use for partial classes/structs unless some of the code is auto generated and you want to make sure you don’t interfere with auto generated code. That is until recently I have been asked to create a struct representing a local date – a data that is not affected by time-zones and lacks a time component,

To make it as close as a native CLR type I had to implement a bunch of interfaces, ISerializable, IEquitable<T>, IXmlSerializable. In addition it had to implement some explicit and implicit convertions, !=, ==, >=, <=, > and < operators. Soon, what seemed like a simple struct grew into couple of thousands lines of code,

There is nothing preventing you from putting all the code into one big file, however I think I found a more elegant solution – use the partial keyword and split one large LocalDate by either the interface being implemented or things it’s implementing. So I ended up with:

  • LocalDate.ISerializable.cs
  • LocalDate.IEquitable.cs
  • LocalDate.Operations.cs
  • LocalData.Conversions.cs

I have also used one of the Visual Studio plugins to nest all of those files under a single LocalData.cs.

So far I couldn’t come back with a single draw back of such approach. Please let me know if there are any!

Using .NET Reference Source for debugging

Improvements when debugging .NET Reference Source

Historically since the inception of this effort, Microsoft have published sources and PDBs for every major .NET framework update namely .NET framework 4.0 and 4.5. However these builds would be rendered effectively useless the moment any update to the framework was released, since the binaries on the updated box no longer matched the PDBs that were indexed on the reference source server. Unfortunately the design of the system that they had in place was geared towards doing single and infrequent pushes of sources and symbols out and did not account for the sheer volume of builds and patches that come are produced out of the .NET framework build system.

Continue reading “Using .NET Reference Source for debugging”

Blog at WordPress.com.

Up ↑