May 6, 2011

Have WCF Service hosted on windows service.

This one is quite old, anyway, as I am using my blog for adding notes to myself as well, I will repeat the solution so I can later find it :).

Maybe you will find it useful as well.

To host a WCF service on top of Windows service you need to do the following:

1. Create the windows service that will host the WCF Service.

2. In the windows service add references to the following assemblies:

2.1. System.ServiceModel

2.2. System.ServiceProcess.

3. Declare the following in the Service class:

ServiceHost host = null;

4. In the OnStart method of the service, instantiate the host variable and the WCF Service instance:

protected override void OnStart(string[] args)
{
    if (host != null)
    {
        host.Close();
    }


    SomeService.ServerAgent server = new SomeService.ServerAgent();
    host = new ServiceHost(server);
    host.Open();
}

5. We need to handle Service stop as well, add the following code to the OnStop method of the Windows Service:

protected override void OnStop()
{
    host.Close();
    host = null;
}

6. All we need to do now is to set some bindings / endpoints. NOTE: They should go to the app.config file of the WINDOWS service. Just copy them from the WCF Service web.config.

You are done.

Install and start the windows service and navigate to the Url Address you added as endpoint. You should be able to browse it.

Coding Practice : Always refresh the 2nd Visual Studio Solution :).

I had a problem with some code in a Visual Studio, the errors were something like when you have a missing file, or a missing method or stuff like that.

They were happening all the time and I was wondering what is going on.

The problem was that I was having 2 Visual Studio instances running with different solutions loaded in them, I was coding in the first solution and touched a shared project (.csproj).

The second Visual Studio asked me to refresh. I answered no and the old code was in the Visual Studio #2.

To overcome this, you may:

1. Always answer “Yes” to the refresh dialog (bad practice) as you may forget sometimes or accidentally click “No”.

2. Click refresh on the solution file in the second visual studio (bad solution, it is a bit slow and you may forget to do this).

3. Go to Options –> Environment –> Documents –> and check the following checkboxes:

   3.1. Detect when file is changed outside the environment (check).

   3.2. Auto-load changes, if saved : (check)

I will go with approach #3 and see if it has some shortcomings as I can’t think any right now.

A screenshot bellow shows the exact settings:

image

Having column display name in RadGrid Column chooser but not in the header of the grid

A little bit of background for this one.

I had a Telerik RadGrid that was having few columns ...

It had the column chooser enabled. Few of the columns were icons (for example “Answered”, “Rating”).

I wanted to have them without header text so I added the Template column and everything was just fine.

Until I right clicked on the header in order to show the column chooser. It was displaying the columns with auto generated names like “GridTemplateColumn7”, “GridTemplateColumn10”.

My goal was to keep the header of the grid clean (e.g. no label) because the icons were 16x16 and that’s all the space I needed to occupy with those columns, having a header “Answered” would extend the entire column. So to solve this case here is what I did:

Solution:

1. Added header text to the column and saw that the header text is appearing in the column chooser.

2. To hide it from the header of the column I added an empty <HeaderTemplae>.

The column should look like this:

<telerik:GridTemplateColumn HeaderText="Bookmark">
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    </ItemTemplate>
</telerik:GridTemplateColumn>

 

This would solve the problem for Template columns (and all column types that allow you to define HeaderTemplate). This will not work for GridBoundColumn for example as this type doesn’t have HeaderTemplate.

 

Hope this helps someone out there …