Google+ C#

Peter Bromberg's .NET Blog All Things Programming

How to improve the performance of Visual Studio 2017

3. November 2017 13:01 by admin in ASP.NET, C#, VIsual Studio 2017


1. Exclude VS and other tools from Windows Defender.

Here is a list of exclusions for a system with Visual Studio 2017 Professional, ReSharper and Node.js installed.

Processes to Exclude:

// visual studio & tools
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe
C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe
C:\Program Files\dotnet\dotnet.exe
// Node.js
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Web\External\node.exe
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\ServiceHub\Hosts\ServiceHub.Host.Node.x86\ServiceHub.Host.Node.x86.exe
// Node.js (if installed separately or upgraded)
C:\Program Files\nodejs\node.exe

Folders to Exclude:

// project folder (Whatever folder(s) you use for projects)
// visual studio & tools
C:\Program Files (x86)\Microsoft Visual Studio 10.0
C:\Program Files (x86)\Microsoft Visual Studio 14.0
C:\Program Files (x86)\Microsoft Visual Studio
C:\Program Files (x86)\MSBuild
C:\Program Files\dotnet
C:\Program Files (x86)\Microsoft SDKs
C:\Program Files\Microsoft SDKs
C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv
C:\Program Files (x86)\Microsoft Office
// cache folders
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files

2. Turn off indexing
Indexing Service is another bottleneck, pulling the disk I/O to 100% during builds and installing packages as well as shortening the lifetime of SSD drives.

Open the command prompt, type services.msc, and press enter. At the list of services, scroll down until you find Windows Search service. You’ll notice it has probably a Started status and Automatic startup type. Stop the service and set the startup type to Disabled. Do a system restart.
Search will still work, it just won't use the index.

3. Visual Studio Settings:

The following are recommended settings based on best practices for Visual Studio 2017
You can decide which features to leave enabled based on your own needs.

Environment -> General
Uncheck “Automatically adjust visual experience based on client performance”
Uncheck “Enable rich client visual experience”
Check “Use hardware graphics acceleration if available”
Environment -> AutoRecover
Uncheck “Save AutoRecover information every”
Environment -> Documents
Check “Save documents as Unicode when data cannot be saved in codepage”
Environment -> Keyboard
Set “ForceGC” to ctrl+num1
Set “ReSharper_Toggle” to ctrl+num0 (if ReSharper is used)
Set “ReSharper_EnableDaemon” to ctrl+num8 (if ReSharper is used)
Environment -> Startup
Set “At startup” to “Show empty environment at startup”
Uncheck “Download content every”
Environment -> Synchronized settings
Uncheck “Synchronize settings across devices when signed into Visual Studio”
Projects and Solutions
Uncheck “Track active item in solution explorer”
Check “Lightweight solution load for all solutions”
Projects and Solutions -> Build and Run
Check “Only build startup projects and dependencies on run”
Projects and Solutions -> Web Package Management
Set “Restore on Project Open” to false (for Bower)
Set “Restore on Save” to false (for Bower)
Set “Restore on Project Open” to false (for NPM)
Set “Restore on Save” to false (for NPM)
Text Editor -> General
Uncheck “Track changes”
Text Editor -> All Languages -> General
Uncheck “Enable virtual space”
check “Line numbers”
Uncheck “Navigation bar”
Text Editor -> All Languages -> Scroll Bars
Uncheck “Show annotations over vertical scroll bar”
Text Editor -> All Languages -> CodeLens
Uncheck “Enable CodeLens”
Text Editor -> Basic -> Advanced
Uncheck “Enable full solution analysis”
Text Editor -> C# -> Advanced
Uncheck “Enable full solution analysis”
Text Editor -> Basic, C# -> IntelliSense
Uncheck “Show completion list after a character is deleted”
Set “Snippets behavior” to “Never include snippets”
Text Editor -> CoffeeScript -> CoffeeLint
Set “Enable CoffeeLint” to false
Text Editor -> CSS -> CssLint
Set “Enable CSS Lint” to false
Text Editor -> CSS, HTML, JSON, LESS, SCSS -> Advanced
Set “Show errors as warnings” false
Text Editor -> JavaScript/TypeScript -> EsLint
Set “Enable ESLint” to false
Text Editor -> SQL Server Tools, U-SQL -> Intellisense
Uncheck “Enable Intellisense”
Text Editor -> [Language] -> Formatting -> General
Uncheck everything (if ReSharper is used for formatting assistance)
Uncheck “Suppress JIT optimization on module load (Managed only)”
Uncheck “Enable Edit and Continue”
Debugging -> Just-In-Time
Uncheck “Script”
Uncheck “Enable IntelliTrace”
Node.js Tools
Set “Check for surveys/news” to “Never”
Web Forms Designer
Uncheck “Enable Web Forms Designer”
Windows Forms Designer
Set “Automatically Populate Toolbox” to false
XAML Designer
Uncheck “Enable XAML Designer”

Kudos to developer Burak Tasci ( for many of the above settings.

Global Unhandled Exception Logging

28. November 2016 08:53 by admin in C#, EXCEPTIONS

We as developers often don't account for the fact that we may not be catching and handling all possible exceptions.This is particularly problematic in Windows Services as if something bad happens, the service may continue running but will be in a state where it isn't actually "doing anything".

Code like the following can be used to catch and handle any exception for which no handler has been wired in, and such exceptions can be logged using your preferred method:


namespace YourNamespace {

static class Program



static void Main()


AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);


static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)

{ HandleException(e.Exception);


static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)




static void HandleException(Exception e)


//Handle your Exception here




How to Ensure that jQuery is only loaded once

26. July 2016 13:50 by admin in ASP.NET, C#, Jquery

Often we have pages and master pages and "bundles" that load scripts like jQuery and we can't always be sure that a particular page already has it loaded. Here is a script that will only inject jQuery into a page if it has not already been loaded:


<!-- comment or uncomment next line to test it out --->
<script src=''></script>


// Only do anything if jQuery isn't defined

if (typeof jQuery == 'undefined') {

function getScript(url, success) {

var script = document.createElement('script');

    script.src = url;

var head = document.getElementsByTagName('head')[0],done = false;

// Attach handlers for all browsers

script.onload = script.onreadystatechange = function() {

if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {

done = true;

// callback function provided as param


script.onload = script.onreadystatechange = null;







getScript('', function() {

if (typeof jQuery=='undefined') {

// Super failsafe

} else {

alert("We Loaded it!");

// jQuery loaded 




} else { 

// jQuery was already loaded

alert("jQuery is Already Loaded!");

// Run your jQuery Code









Implementing a Custom IPrincipal in an ASP.NET MVC Application

25. January 2014 14:45 by admin in ASP.NET, C#, MVC

 I have an MVC application for which I have implemented a Custom ExtendedMembership - derived Membership class that is hosted via a WCF Service.

I have a custom MembershipProviderForwarder class that plugs right into my web.config as the Membership provider, but what it actually does is forward all Membership calls to my WCF Service, which uses the real custom Membershp provider, and returns back all the results to the app from over the wire.

In this manner I can have any number of MVC apps all using the same provider via my Webservice.

Since I have custom Membership fields, but only a couple, I didn't want to get into writing a lot of Profile code. Instead, I have a Custom User object that has these extra fields which can be returned from the UserProfile table in SQL Server.

The issue is that I only want to make Webservice calls when the user first logs in, and I want to store my custom fields in the Forms Ticket (it has a UserData property for just this purpose). In this manner I can use the PostAuthenticateRequest event to pull my custom data our of the forms cookie and attach my custom IPrincipal to the HttpContext.Current.User property for each subsequent request. Here is how I did that:


1. Create the interface


interface ICustomPrincipal : IPrincipal


    int UserId { get; set; }

    string FirstName { get; set; }

    string LastName { get; set; }



2. CustomPrincipal


public class CustomPrincipal : ICustomPrincipal


    public IIdentity Identity { get; private set; }

    public bool IsInRole(string role) { return false; }


    public CustomPrincipal(string email)


        this.Identity = new GenericIdentity(email);



    public int UserId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }



3. CustomPrincipalSerializeModel - for serializing custom information into userdata field in FormsAuthenticationTicket object.


public class CustomPrincipalSerializeModel


    public int UserId { get; set; }

    public string FirstName { get; set; }

    public string LastName { get; set; }



4. LogIn method - setting up a cookie with custom information


if (Membership.ValidateUser(viewModel.Email, viewModel.Password))


    var user = userRepository.Users.Where(u => u.Email == viewModel.Email).First();


    CustomPrincipalSerializeModel serializeModel = new CustomPrincipalSerializeModel();

    serializeModel.UserId = user.Id;

    serializeModel.FirstName = user.FirstName;

    serializeModel.LastName = user.LastName;


    JavaScriptSerializer serializer = new JavaScriptSerializer();


    string userData = serializer.Serialize(serializeModel);


    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(








    string encTicket = FormsAuthentication.Encrypt(authTicket);

    HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);



    return RedirectToAction("Index", "Home");



5. Global.asax.cs - Reading cookie and replacing HttpContext.User object, this is done by overriding PostAuthenticateRequest


protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)


    HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

    if (authCookie != null)


        FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

        JavaScriptSerializer serializer = new JavaScriptSerializer();

        CustomPrincipalSerializeModel serializeModel = serializer.Deserialize<CustomPrincipalSerializeModel>(authTicket.UserData);

        CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);

        newUser.UserId = serializeModel.UserId;

        newUser.FirstName = serializeModel.FirstName;

        newUser.LastName = serializeModel.LastName;

        HttpContext.Current.User = newUser;





6. Access in Razor views


@((User as CustomPrincipal).Id)

@((User as CustomPrincipal).FirstName)

@((User as CustomPrincipal).LastName)


and in code:


    (User as CustomPrincipal).Id

    (User as CustomPrincipal).FirstName

    (User as CustomPrincipal).LastName