Invoking the debugger from a .NET application

Recently I was in a particular debugging situation where the process that was debugged by Visual Studio was launching another process, and I needed to debug the startup of this second process too (no way to attach to the process from the IDE, then).

So something like the "debugger" instruction in JavaScript.

I have found that the solution is to call:


C#: automated method caller information via attributes

In C# there is a feature of the language that allows a method to receive as input parameters the member name, file path and line number of the calling method/property.

It is enough to add attributes to the method parameters receiving these values, as in the following example:

public void DoProcessing()
    TraceMessage("Something happened.");

public void TraceMessage(string message,
        [System.Runtime.CompilerServices.CallerMemberName] string memberName = "",
        [System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "",
        [System.Runtime.CompilerServices.CallerLineNumber] int sourceLineNumber = 0)
    System.Diagnostics.Trace.WriteLine("message: " + message);
    System.Diagnostics.Trace.WriteLine("member name: " + memberName);
    System.Diagnostics.Trace.WriteLine("source file path: " + sourceFilePath);
    System.Diagnostics.Trace.WriteLine("source line number: " + sourceLineNumber);

You can see that this feature can be particular useful during tracing.

The example above has been taken from Caller Information (C# and Visual Basic).

JavaScript CORS used to call asmx web-services

The following example shows how to implement a server implementing an asmx web-service and a JavaScript client invoking it from a different domain. The particularity is that this cross-site invocation requires CORS (Cross-origin resource sharing), supported in IE8+ and other popular browsers.

The server (part 1)

To create the server, I create in Visual Studio an empty web application. Then I add a Search.asmx web-service, adding the following method:


public string DateTimeNow(string Where)


return DateTime.Now.ToLongTimeString() + " in " + Where;


Then to test it I easily deploy it to the Azure Website

The client

To create the client, I create in Visual Studio another empty web application. I add a reference to the jQuery NuGet package, then I add a simple html page and an App.js file.

Here the html page:

<!DOCTYPE html>

<html xmlns="">


<title>My CORS Client Page</title>

<script src="Scripts/jquery-2.1.4.js"></script>

<script src="Scripts/App.js"></script>



<input id="city" type="text" value="Basel" />

<input type="button" value="Press here" onclick="test_cors_client(false, document.getElementById('city').value);" />



The App.js file is copied here too:
function test_cors_client(simple, where)
function escapeHTML(str) {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');

var soapEnv, soapAction;
if (simple) {
soapEnv =
"<soap:Envelope xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:soap=\"\"> \
<soap:Body> \
<HelloWorldResponse xmlns=\"\"> \
<HelloWorldResult>string</HelloWorldResult> \
</HelloWorldResponse> \
</soap:Body> \
soapAction = "";
else {
soapEnv =
"<soap:Envelope xmlns:xsi=\"\" xmlns:xsd=\"\" xmlns:soap=\"\"> \
<soap:Body> \
<DateTimeNow xmlns=\"\"> \
<Where>" + escapeHTML(where) + "</Where> \
</DateTimeNow> \
</soap:Body> \
soapAction = "";

//url: "http://localhost:8118/Search.asmx",
url: "",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\"",
headers: {
"SOAPAction": soapAction
function processResult(xData, status) {
if (status != "success") {
alert("error\n\n" + xData.responseText);
alert("success\n\n" + xData.responseText);
/* var createCORSRequest = function (method, url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Most browsers., url, true);
} else if (typeof XDomainRequest != "undefined") {
// IE8 & IE9
xhr = new XDomainRequest();, url);
} else {
// CORS not supported.
xhr = null;
return xhr;

//var url = "http://localhost:8118/Search.asmx";
var url = '';
var method = 'POST';
var xhr = createCORSRequest(method, url);
xhr.setRequestHeader("Content-Type", "text/xml; charset=\"utf-8\"");

xhr.onload = function (e) {
// Success code goes here.
alert('success\n\n' +;

xhr.onerror = function (e) {
// Error code goes here.
alert('error\n\n' +;

xhr.setRequestHeader("SOAPAction", soapAction);

This file contains both the jQuery implementation using $.ajax, and the XmlHttpRequest + XDomainRequest produced using
Trying the example, you will see that it works when the example is run locally (with Service.asmx copied in the same client website), but not with CORS.

The server (part 2)

To enable CORS on the server, we need to add additional headers on the server, and one quick method is to modify the web.config as here below:

<?xml version="1.0" encoding="utf-8"?>


For more information on how to configure your ASP.NET application, please visit




<compilation debug="true" targetFramework="4.5" />

<httpRuntime targetFramework="4.5" />





<add name="Access-Control-Allow-Origin" value="*" />

<add name="Access-Control-Allow-Headers" value="Content-Type, SOAPAction" />





Now it will be possible to invoke the Search.asmx web-service even from another domain through CORS and here is the zip file containing the complete solution: (704.8KB).

A few links used for reference:

C#: background task in a WinForm application containing a long running task

Recently I had to develop a WinForm application doing some checks. During development, with test data, everything was fast and the UI was acceptable. But once running the application with real data, as it was much more, I understood I was facing a long running task, as the UI was not responsive for 2+ hours.

From my studies, I knew the solution was to use the BackgroundWorker component, and one example was in How to: Run an Operation in the Background.

This was fine in the "old" versions of .NET, and even if still supported, I did some researches to see if a more modern approach was available, for example using the Task class introduced in .NET 4.
In fact the solution is pretty easy: you can run your background task with:

Task t = Task.Factory.StartNew(() =>
// do your long-time processing here
t.ContinueWith((Success) =>
// callback when task is complete
}, TaskContinuationOptions.NotOnFaulted);
t.ContinueWith((Fail) =>
// callback when task throws an exception
}, TaskContinuationOptions.OnlyOnFaulted);

And in how run now operations in the Task synced with the main thread, necessary to update the UI?
Using the Invoke method, better if after checking with the InvokeRequired property, as shown in C# - ThreadPool and Windows Forms.

Internet Explorer: how replace ActiveX with modern standard browsers

One customer has ActiveX controls implementing buttons in their web customizations of third-party products that can't be modified. These buttons where integrating with other Windows application running on the same client machine of the browser. With the goal of modernizing their application platform, they asked us how to replace these ActiveX with something standard and cross-browser compatible.

The suggested solution would be to use WebSockets, that are supported starting from Internet Explorer 10, but also in Chrome, Safari and Firefox.

The solution is to develop a host application, that would need to accept connections and support Websockets. .NET 4.5 supports Websockets natively and so is strongly suggested, but also previous versions support them through 3rd party libraries on Nuget.

An example where a similar solution is implemented is the Visual Studio feature called browser linkIn this case VS acts as a websockets server on the local machine, and any browser can connect and interact with it through it.

SharePoint 2013: how create host named site collection in C#

In C# you can create a hosted named site collection in C# using the method SPSiteCollection.Add in the overload reported in the link itself.

The two options (with PowerShell too) are described in Host-Named Site Collections (Provisioning by Code and in Specific Content Database).

Considerations about the rights needed from the user (eventually the app pool user with RunWith ElevatedPrivileges) to create site collection can be found in Custom SharePoint 2007 Site Collection Creation Page.

SharePoint 2010: access with Safari in iOS 8

Because of a problem in the OOTB browser definitions in .NET 2, carried up to .NET 3.5, iPhone with iOS 8 are not correctly recognized as mobile devices in SharePoint 2010.

The problem is described in iOS 8 / Safari 8 not working with ASP.NET AJAX-Extensions, but the solution in that article is not complete, as it fixes the JavaScript issue but not the mobile capabilities.

So I have merged that solution with the iPhone capabilities in the OOTB browsers definitions, getting the content of the new file that you can download here: iOS8.browser

Migrating applications from Windows 2003 to Windows 2008

When migrating an application from Windows 2003 to Windows 2008, the major concerns are around migrating from IIS 6 to IIS 7.

For Classic ASP applications, the settings can be taken from Moving asp Web site from IIS 6 to IIS 7.5 on Windows 2008 R2:

  • Configure the App Pool ".Net Framework version" to use "No Managed Code"
  • Set the "Managed pipeline mode" to "Classic"
  • Set "Enable 32 bit Applications" to true, in the "Advanced Settings"
  • Set the "Identity" to "Network Service", in the "Advanced Settings"
In case of errors, to help debugging you can temporarily enable:
  • In “Debugging Properties”, set "Send Errors to Browser" to True
  • In Internet Explorer, in “Internet Options” turn off “Show friendly HTTP error messages”

For .NET applications, the considerations are:
  • Maintain the Application Pool in the old Classic Mode, or convert it to the new Integrated Mode
    The process is impacted above all if HttpModules and HttpHandlers are used, and the process is described in Moving an ASP.NET Application from IIS 6.0 to IIS 7.0.
    In addition, the automated tool "APPCMD.EXE migrate config " can bed used. It is described in ASP.NET Integration with IIS 7.
  • If you are moving from an original 32 bit to 64 bit environments:
    • if the application is compiled for 32 bit mode, it is enough to set "Enable 32-Bit Applications" property to true in the Application Pool settings
    • If it is compiled for "Any CPU" you may want to try to run in in 64 bit mode. If it has not dependencies on 32 bit resources (i.e. assemblies deployed in the 32 bit GAC), it should run without any problem
    • If it is compiled for "Any CPU" and you need to run it in 32 bit mode on a 64 bit system, you can try to force its "bit-ness" using the CorFlags utility. The only risk is that this solution changes the assembly header, so it is not applicable with signed assemblies
    • If it is compiled for "Any CPU" and you need to run it in 32 bit mode on a 64 bit system and the assembly is digitally signed, you need to recompile the sources. Here you have two latest choices:
      • recompile forcing 32 bit mode
      • or better remove the dependency on the 32 bit resource so that the application can be run in 64 bit

Visual Studio 2013: how test the performance of the html UI

As new solutions are pushing more and more the html UI, with various JavaScript and AJAX calls to complicate the scenario, analyzing the performance of the UI requires new tools.

For this, in Visual Studio 2013 you can find the HTML UI Responsiveness Profiler.

Here a few links on this topic:

.NET client side debugging and profiling with Glimpse

Glimpse is a client side library for debugging and profiling, that you can install in your Visual Studio project with NuGet.

It is fully described in the following blog posts:


There are also extension to Entity Framework, to track database requests, and Elmah, already discussed in my other article .NET Health Monitoring with Elmah, that allows to add recorded exceptions in a new tab.


Finally, it's possible to develop custom tabs, described in this serie of articles: