Skip to content

Hosting a WCF Service in IIS with Net Tcp binding

I started experimenting with different bindings on a WCF service because an appilcation we hosted for a customer had some performance issues. This application transfered massive loads of data with a standard httpBinding, in others words, all your objects is serialized on the Service, transfered as text (XML), and then recreated on the client that consumes the service. With a net.tcp binding instead the data transfer will be a lot smaller because the transferred would be binary!

I tried a couple of tutorials on the internet but I struggled so I want to make my own here. Here is how make a simple service on localhost with a net.tcp binding:

I have Windows 7 Professional and IIS7 and for making a nettcp binding possible through you have to enable Non-HTTP Activation. (Sorry for the Norwegian language)

image

Then start a new WCF Service application project ‘WCFService1’ in Visual Studio and compile it. NOTHING ELSE! Just keep the original sample code.

Then we test it with the defautl httpBinding. Open IIS Manager and click on your “Default Web Site”. Then set the path for this website to your newly created WCFService1

image

Open up WcfTestClient.exe (in your Microsoft SDK folder) and add service http://localhost/Service1.svc
The service should appear like this
image 

So far, so good. But what about that net.tcp binding?

Add another binding to your website through IIS Manager with net.tcp and binding info: “808:*”. This means that localhost will listen on port 808 which is default for net.tcp connections.

image

Go to Default Web Site –> Manage Website –> Advanced Settings and make sure that Activated protocols are “http, net.tcp”

image

Now you are almost done!
We have to modify our web.config for the WCFService application and change the bindings configured to nettcp instead of http. Make sure you have these lines

<endpoint address="" binding="netTcpBinding" contract="WcfService1.IService1">

and this one

<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>

Voila! Try now to open up WcfTestClient.exe (in your Microsoft SDK folder) and add service net.tcp://localhost/Service1.svc

If you have some trouble with connecting, maybe you can temporary turn off your firewall. The firefwall can sometimes be blocking your port

New side project

Every true programmer should have side projects. I haven’t had this for a while, but now I want to do something about this. A colleague and I have this idea of a website which is pretty much self-driven and I can’t wait to jump on this one!

At work you don’t always choose your technologies and frameworks so I’ve been really anxious about starting up a new MVC 3 web application project with NHibernate (or EF) and a IOC container. Guys, I’ve NEVER created a project with an IOC framework, and that’s horrible. I am sure that start working with these technologies will enlighten me and give me deeper insight on constructing larger applications.

So this weekend we will start drawing sketches and creating requirements and setting up our first iteration. Godspeed!

MetroTwit goes buggy

My favorite twitter client is MetroTwit. It has all the features I need (not many), and it has minimalistic and nice UI.

But now, when trying to use two laptops, the client give me an error mesage. It’s nice and descriptive about something has happened to my authorization between the client and twitter. Then it asks me if I want to disconnect this authorization. I answer yes, but MetroTwit does nothing and gives the same error message. I answer no, and the application crashes. I have even tried reinstalling the whole client and deleting the data under /AppData in my user folder, but nothing changes.

I want my MetroTwit back!

TableAdapters and Query Configuration Wizard in Visual Studio

In my current project we are bound to use typed datasets for communicating between Business services  and the client application. Yes, I know every wiseguy out there hates datasets and prefer using an ORM like nhibernate and transfer POCO objects between client and server, but that is not an option here.

Except for vulnerability against tight coupling, typed datasets can spare you a lot of trouble and work. But there are some features, like the TableAdapter Configuration Wizard (TCW), which has its horrible flaws!

Ever tried to write your own custom SQL in the TCW? Well, for basic and simple queries it’s no problem, but try adding a simple CASE statement.
Now, the TCW throws an exception and the workarounds are few (please, let me know if anyone has any magic tricks)

There are many complex queries in this project and the result is that almost every TableAdapter has its own inherited class so we can manually set the Custom SQL there.

But the worst is when working with typed Datasets and TableAdapters with the ODP.Net Oracle Provder, the whole application becomes like a big zombie. If you are really lucky you can do some changes to a Dataset in a couple of minutes, but if you are very unlucky and working with a complex dataset, you can actually wait for an hour or two for letting vs2008-2010 apply your changes. Really. that’s not fast. Or effective.

I have been talking to Christian Shay, the product owner of  ODP.Net and he’s been promising us a fix for this mess very soon. Let’s hope he’s not a zombie, too.

Names

Just for the record: My name is Martin Helgesen. But I am planning to include my mothers family name which goes waay back: Brusevold. And when I created my gmail account,  martin.helgesen wasn’t available, but martin.brusevold was.

Everybody, say hello to Martin Brusevold Helgesen.

Homecoming

So it is all set.

I am returning to my hometown Sarpsborg again to work for an IT company there. Oslo was a nice place to work, but it’s very tempting to have 5 minutes of travel time to work, instead of 105 minutes. That means I will have over 3 hours of more spare time  in my daily schedule!

I have worked with many interesting and higly competent co-workers and customers. I will surely miss them. Anders Hattestad if you are reading this, thanks for all I’ve learned. You’re the best Episerver developer, no doubt.

Virtual Roles and access control in EPiServer

In some scenarios in EPiServer you may need to create your own logic for accessing specific pages. This can be done in several ways, but one of them is to implement your own Virtual Role. EPiServer default includes a set of virtual roles which are being used frequently.

Let’s say you have an open webportal in your company, no need to log in. And one department in the company, located in another part of the world, should see a set of pages (and its children), while the rest of the company should not. This department has specific IP addresses so we can recognize a request from this specific department from the IP address.

One solution would be to create a virtual role that compares the request IP to a set of pre-defined IP ranges (e.g from a config file). Let’s call this role “Employee”. you have to register it in web.config like this (added to the bottom after the default roles):

<virtualRoles replacePrincipal="true"> 
<providers> 
<add name="Administrators" type="EPiServer.Security.WindowsAdministratorsRole, EPiServer" /> 
<add name="Everyone" type="EPiServer.Security.EveryoneRole, EPiServer" /> 
<add name="Authenticated" type="EPiServer.Security.AuthenticatedRole, EPiServer" /> 
<add name="Anonymous" type="EPiServer.Security.AnonymousRole, EPiServer" /> 
<add name="Creator" type="EPiServer.Security.CreatorRole, EPiServer" /> 
<add name="Employee" type="YourType, YourAssembly" /> 
</providers> 
</virtualRoles>

Then you have to create the implementation of the class. Inherit from the EPiServer.Security.VirtualRoleProviderBaseclass. Here is a class that compares the requests IP address:

public class EmployeeRole : EPiServer.Security.VirtualRoleProviderBase
{
public override bool IsInVirtualRole(IPrincipal principal, object context)
{
var clientIpAddress = HttpContext.Current.Request.UserHostAddress;
var validAddresses = new System.Xml.XmlDocument();
validAddresses.Load(System.Web.HttpContext.Current.Server.MapPath("/ipaddresses.config"));
var isequal = validAddresses.SelectNodes("//IPSet/ip").Cast().Any(node =&gt; IsInRange(clientIpAddress, node));
return isequal;
}   private static bool IsInRange(string clientIpAddress, XmlNode node)
{
byte[] clientIP = IPAddress.Parse(clientIpAddress).GetAddressBytes();
byte[] mask = IPAddress.Parse(node.Attributes["mask"].Value).GetAddressBytes();
byte[] ip = IPAddress.Parse(node.Attributes["address"].Value).GetAddressBytes();
bool isequal = true;
for (int i = 0; i &lt; ip.Length; i++)
if ((clientIP[i] &amp; mask[i]) != ip[i])
{
isequal = false;
break;
}
return isequal;
}
}

There you are finished creating your role! But how do you assign this role read-rights to a specific page and its subpages? Do it in edit mode. But first, you have to create a new group with the same name as the role you just created. EPi will then automatically associate this group with your created Virtual Role. After this, you can just add access rights for the group “Employee” to the pages in EPi you want.

New year. Great expectations

Hello everyone, my name is Martin and I work as a senior consultant in a IT  company located in Oslo, Norway.

I figured that since we now started a new year I will have set some goals for 2011. What will I achieve this year? It’s always nice to define goals. You can only get more experienced and the worst thing that can happen is not achieving your goals.

And for the record, adding a humongous number of goals will just result in lack of following up everyone of them, so if you want to be effective, concentrate on a few important.
Here is my list for 2011:

Writing tests
…the right way and be convinced that, hey, this is actually very useful! I’ve worked with legacy code several years now, and it’s soo hard to start writing tests on existing stuff. Hope I can join a new project from scratch very soon .

Hold several presentations
…that’s right, I want to challenge myself to keep presentations for an audience (not just working colleagues). You learn so much from this and evolve in many ways. Maybe it will be for the norwegian .net user group?

Keep track on myself
…on everything I do. How productive am I? How many new technologies or frameworks have I learned? How many books have I read? How many training hours a week? Guess it’s my way to try to optimize my life and how to live it. Measure, measure, measure.

Finishing a marathon
…in under 200 minutes (3:20:00). Yup, I’m a running man and I’ve finished several marathons and my personal best is 3:28:47. In Berlin, september this year,  I will be running the worlds fastest marathon track and I hope for a new record also 🙂

What’s your list like?