Archive for the ‘Uncategorized’ Category

Making Cross-Domain Calls in SignalR

March 30, 2012

I saw this stack overflow post today on how to use SignalR across domains:

Since I wanted to try out SignalR anyway I thought it would be a nice way to get started and see if I can solve the problem and write a small tutorial on how to do it.

The first step in using SignalR cross-domain is to tell it where to find the hub to connect to. This is done using the following code that you should insert at the beginning of your JavaScript code:

$.connection.hub.url = 'http://[someotherdomain]/signalr';

If you try your application like this it will not work and you will have the same problem as the poster mentioned above. This is because for security reasons many browsers don’t allow cross-domain calls by default. A server has to add the special Access-Control-Allow-Origin header to its response to tell the browser to allow cross domain calls to this specific site.

You can add this value to your ASP.NET page using:

Response.AppendHeader("Access-Control-Allow-Origin", "*");

However even with this modification SignalR won’t do the cross domain call – at least not in Internet Explorer 9 where I tested it. Like the original poster I was stuck for quite a while because I didn’t find any solutions. The error message I got was:

SignalR: Connection must be started before data can be sent. Call .start() before .send() 

Poking arround the jQuery code and debugging a bit I eventually saw that there is a check inside jQuery to see if the call is a cross domain call or not and eventually this part of the code will terminate with the error “No transport”.

With this new error message I came upon this page explaining the problem and suggesting a possible solution to force jQuery into making a cross domain call using the following JavaScript line: = true; //force cross-site scripting

And it worked!!!

I also found some references suggesting JSONP as a possible solution, but it seems more complex and at least for me the solution above works fine.

Creating an URL to a WebAPI ApiController inside an ASP.NET MVC View

March 27, 2012


Today I was trying to add some dynamic content to my new IT consulting website. My website is based on ASP.NET MVC, but I needed to load some dynamic content in one part of the page and I thought I would try out the new ASP.NET WebAPI framework for this.


Creating the ApiController was easy, but when I wanted to use the controller in my ASP.NET MVC view, I wasn’t sure how to include the Url of the controller. Hardcoding the address didn’t feel right, especially because for the MVC controllers you have the really useful Url.Action method, which takes the name of an action and a controller and generates an URL to call this action. Unfortunately there wasn’t any built in method to call an ApiController. My first attempt was to use the following call instead, specifying the name of the route I want to use directly:

Url.RouteUrl("DefaultApi", new{controller="[CONTROLLERNAME]"})

Unfortunately, this didn’t work as expected — the function didn’t produce any Url.

After some googling I came across this stackoverflow post, with the missing piece of the puzzle:


Basically all you have to do is to add an empty httproute parameter to the Url.RouteUrl call. This apparently helps the system differentiate between normal routes and WebApi routes. So the final call looks like this:

Url.RouteUrl("DefaultApi", new{httproute = "", controller="[CONTROLLERNAME]"})

Keep in mind that the first parameter to RouteUrl refers to the name of the route you used in the RegisterRoutes function, which is “DefaultApi” by default.

Generate Entity-Framework DDL-Script for SQLite

April 17, 2011

By default the Entity Framework designer for Visual Studio only generates DDL Scripts for Microsoft SQL Server (CE).

However you can change the template file (.tt) which is used to generate the SQL statements. Just click on empty space in the designer and take a look in the property sheet window.

The default template is called and resides in a subfolder of the Visual Studio program files directory.

For one of my projects I needed the generated SQL to be compatible with SQLite, so I modified the original file for SQLite and created a new version, which you can download below.

After downloading the files and putting them in the correct directories, you should be able to select them inside the EF designer.

Keep in mind that I can’t give any guarantees that this files will work as you expect. I wrote them specifically for my project and I’m just sharing them in the hopes that they are useful for anyone.

Download the following zip file:

You will find 2 files in the zip file:

Put the file in the directory
.\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\

Put the GenerateSQL.Utility.ttinclude file in the directory
.\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\

You are done! Smiley Now you should be able to select the new template in the Visual Studio designer and enjoy the newly SQLite compatible SQL script.

Allowing cross-domain access from Silverlight to a standalone WCF-Service

March 12, 2011


Silverlight by default only let’s you access services which come from the same domain as the domain that served the Silverlight control. This guards against some types of security vulnerabilities.

A service can however opt-in to allow calls from Silverlight controls hosted on other domains. To do this a service has to provide a clientaccesspolicy.xml or crossdomain.xml file, which specifies the access policy for this service.

Silverlight, when asked to do a cross-domain call, will look for this file at the root of the domain where the target service is hosted.

The following is a sample clientaccesspolicy.xml file:

<?xml version="1.0" encoding="utf-8"?>
      <allow-from http-request-headers="SOAPAction">
        <domain uri="*"/>
        <resource path="/" include-subpaths="true"/>

You can also use a crossdomain.xml file, which is used by Adobe Flash, but is also recognized by Silverlight.

Standalone WCF-Services

It’s easy to provide the required file for a service hosted on web server like IIS. For a standalone WCF service however, there is no built-in functionality to achieve this.

You can however create a separate service which is responsible only for serving the required policy file and make it available at the services domain root.

The idea is copied from this forum entry (thanks Yi-Lun Luo):

Creating the web service class

public class CrossDomainPolicyRetrieverService
    [OperationContract, WebGet(UriTemplate = "crossdomain.xml")]
    public Stream GetFlashPolicy()
        return getPolicy("crossdomain.xml");

    [OperationContract, WebGet(UriTemplate = "clientaccesspolicy.xml")]
    public Stream GetSilverlightPolicy()
        return getPolicy("clientaccesspolicy.xml");

    private static Stream getPolicy(string file)
        if (!File.Exists(file))
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
            return null;

        WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
        return File.OpenRead(file);

This simple web service has 2 methods corresponding to the two files mentioned above. It will search for the file in the application directory and return them as the response for the file.

Configuring the service

To configure the service add the following lines to your app.config file:

<?xml version="1.0"?>
      <!-- snip... regular services -->

      <service name="MyNamespace.CrossDomainPolicyRetrieverService">
        <endpoint address="" binding="webHttpBinding"
            <dns value="localhost"/>
             <!-- Root Domain where the other services are hosted -->
            <add baseAddress="http://localhost:8732/"/>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        <behavior name="policyBehavior">
          <webHttp  />  
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  <!-- snip... -->

Testing the service

To should now be able to access the policy files using any web browser and pointing it at /clientaccesspolicy.xml (e.g. http://localhost:8732/clientaccesspolicy.xml)

By setting a breakpoint inside the service methods, you can easily watch Silverlight accessing the corresponding resource files.

Increasing the maximum item count of a feed in the Windows Feeds Engine

September 14, 2009

This post mainly serves as a reminder for myself, because after each reinstall of Windows I have to search for this all over again and it is not very easy to find.

By default the Windows Feeds Engine stores only a limited amount of items per feed (200). You can only increase this limit up to the predefined maximum of 2500.

If like me you like to preserve every blog entry made (or at least more than 2500 items per feed), you can increase this limit by creating the following registry DWORD value:


I usually set it to 0x7fffffff, which is the largest possible positive 32 bit integer you can set. Basically it means preserving every blog entry ever made for a specific blog.



Hello world!

July 7, 2006


my name is Thomas Krause and I will blog some random thoughts here about C#, programming in general and some other technology related stuff. My native language is german, but my plan is to blog mainly in englisch, so just forgive me for my grammar mistakes and don’t hesistate to correct me.