Monday, June 11, 2007

Enterprise library 3.1 – Policy Injection and Caching Block – Asp.Net 2.0

I’m currently investigating the advantage of upgrading from the version 2 of the Enterprise Library to version 3.1.

One advantage in the existing functionality is the way transactions are handled in the data access block compared with the previous version. Now the escalation to the DTC is prevented when many updates are made to the same database. Since we are firing fifty updates in a single transaction this causes no exception anymore.

The new functionality we will use and that does not require any code change is the rolling text file logger. We wrote something similar but we prefer to switch to the standard one Microsoft provides.

The great new functionality is the caching of the execution of the methods. This kicks in when the same method gets executed twice with the same parameters. The three things you need to do, to use the Policy Injection in combination with the Caching is that the object needs to derive from MarshalByRefObject, apply the caching attribute or use configuration and create the object using the PolicyInjection.Create method.

This creates an overhead for every object that is created and for every function that gets executed so take care where to use this caching block. The overhead is often a minor issue considering the drastic performance gain with caching.

The only issue that I have found till now is with hash code of the objects that are passed as parameters in the function that is enabled for caching. The issue is that since we work in a web environment our objects are destroyed between roundtrips so when a new call comes a new “identical” object is created. This new object has a different hash code since it is a new object.

The way we solved is for our custom defined objects, is to override the GetHashCode function. In this overriden function we check the real content of the object and return our hashcode depending on that. The price is that our GetHashCode is slower then the standard one.

The Policy Injection block is a great way to develop in a more Aspect Oriented way and will help us to focus on writing business logic and less tracing, caching, error handling, validation and other plumbing.

Monday, May 14, 2007

Hosting WCF Service in IIS with WAS (Windows Activation Service)

WCF allows to host the services in serval ways. Every scenario has it specific advantages and it is important to choose the right way to host your processes. The options for hosting are to host in IIS, an executable or a service.

Before Vista it was possible to host only webservices or remoting via HTTP in IIS because there was the limitation till IIS 6.0 that the communcation protocol had to be HTTP(s). It is mentioned in the MSDN that you can actually use WAS without IIS but till now I could not find any sample where this is explained.

The main complexity of hosting in IIS is to set up your IIS to allow TCP/IP for for the server and for the specific webapplication. The steps to get started are to install the WCF Non-HTTP activation components. To do that, go to the Start menu —> Control Panel —> Programs and Features, and then click "Turn Windows Components On or Off" in the left pane. Expand the Microsoft .NET Framework 3.0 node and ensure that the "Windows Communication Foundation Non-HTTP Activation" feature is checked.

Secondly run the following command as Administrator.
c:\windows\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']

Then a new application needs to be created in IIS. In this case I created the TestApp2 application. After creating it it is important to apply the following statement as Administrator.
C:\Windows\System32\inetsrv\appcmd.exe set app "Default Web Site/TestApp2" /enabled Protocols:http,net.tcp.

This makes sure that the application is also accessable via TCP/IP.

Now IIS has been set up to host components. The next step is to create a new project in Visual Studio 2005 of the type WCF Service Library and then remove all the commented text.

To keep the service as simple as possible I made the following implentation.

[ServiceContract()]
public interface IMyService
{
[OperationContract]
string SayHello(string value);
}

public class MyService : IMyService
{
public string SayHello(string value)
{
return "Hello " + value;
}
}

Then two files need to be added. The first one is the definition of the service. This must exist in a file with the extention svc and will be the entry point for IIS to the service. The file in this example is called Service.svc and the content of the file is
<% @ServiceHost Language=C# Debug="true" Service="MyService" %>
The next file that needs to be added is the Web.Config. One of the main things in the Web.Config file is the declaration of the IMetadataExchange contact. This allows to exchange the meta information over TCP/IP.


<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="MyService">
<endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding"
contract="IMyService" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="PortSharingBinding" portSharingEnabled="true">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.web>
<compilation debug="true"/>
</system.web>
</configuration>


Compile the service and then copy the files web.config and service.svc over to the folder that corresponds with the application you previosly created. In this example it is c:\inetpub\wwwroot\TestApp2. And create the folder Bin where the dll is copied.
The service is not created and can be accessed over TCP/IP. This means that it can possible to check if the methods exist using the Internet Browser.

The client can be either type of application and the reference needs to be set to
net.tcp://localhost/TestApp2/Service.svc. The service proxy is automatically created and can be accessed equalty to any WCF Service.

Tuesday, May 08, 2007

I got the request to make an entry on what kind of tools I use on a day to day basis while developing ASP.NET 2.0 Applications. The main tool I use is of course VS2005 with Team foundation server as the source control system. I separated the other tools depending on the environment they are used from.

Plug-ins for Visual Studio

Simian - Similarity Analyser This tool makes it possible to detect duplicate code within a file, project or solution.
Team Build SidekickAllows to Check the team build in and out.

Plug ins for Internet Explorer

FiddlerAllows to intercept the HTTP Traffic. Very useful to see the content of the files that are requested.
Developer toolbarAllows to check the properties of the elements that are rendered in the browser.

Tools for the build

Cruise Control.NetAllows the continues integration of .NET applications
TFS CruiseControl.Net pluginThe Plug in to use Team Foundation Server with Cruise Control

Tools for the Team Foundation Server

Teamplain
Web UI for the Team Foundation Server
Custom developed RRSfeed
This tool allows to subscribe to all the work items, your work items, the team builds, the check ins via a news reader tool.


There will be more information on the ASP.NET developer tools later.

Friday, May 04, 2007

TeamPlain Web Access for Team System

Today we have installed TeamPlain on our Team Foundation Server. The version we installed is the 2.0 RC.

The only scary moment when installing it on the Team Foundation Server is when you host it in IIS next to the other Sites (Sharepoint , TFS Server, Default WebSite) and hope that IIS is mapping the port correctly.

The tool works great and it is faster then we expected once the firewall port is opened ;)

The only drawback we found till now is the licensing. Although Microsoft claims it is included with the Team Foundation Server licence you actually should have a TFS CAL to access the site. The main reason why we want to use the TFS Web client is not to buy TFS client access licenses for the not developers. So it is going to be interesting to see if MS creates a new type of licenses for web access?


All information

Monday, April 02, 2007

Using Callback Contracts in WCF

The best sample I found of a full duplex example with WCF can be found at http://dotnetaddict.dotnetdevelopersjournal.com/wcf_alarmclock.htm

And the sourcecode can be found at http://files.blog-city.com//files/J05/88284/b/duplexalarmsystem2.zip

Wednesday, March 28, 2007

WCF and IIS

The easiest sample how you can use WCF togehter with IIS can be found at
http://blogs.msdn.com/trobbins/archive/2006/11/27/how-to-hosting-a-wcf-service-in-iis.aspx

Monday, March 26, 2007

Object binding with WPF

It took me some time to find a good sample of doing object binding with WPF. I decided to publish my sample.

I created a class called patient.

public class Patient
{
private int _id;
private string _name;
private string _picture;

public string Picture
{
get { return _picture; }
set { _picture = value; }
}

public Patient(int id, string name , string picture)
{
_id = id;
_name = name;
_picture = Picture;
}

public string Name
{
get { return _name; }
set { _name = value; }
}
public int ID
{
get { return _id; }
set { _id = value; }
}
}

This patient class is filled with data by an engine class. This engine class is called PatientEngine.

public class PatientsEngine
{
public static List GetPatients()
{
List pList = new List();
Patient p1 = new Patient(1, "Jack", "Images/Jack.png");
Patient p2 = new Patient(2, "Jim", "Images/Jim.png");
Patient p3 = new Patient(3, "Johny", "Images/Johny.png");
pList.Add(p1);
pList.Add(p2);
pList.Add(p3);
return pList;
}
}

This class has a static method to fill the generic list of patients.

To call the engine you need to

  1. Create a new windows XAML application
  2. Open the window1 file
  3. Add in the window tag the namespace declaration where your engine is located, in my case in "WindowsApplication4" do this by adding this line.
    xmlns:src="clr-namespace:WindowsApplication4"
  4. Add the ObjectDataProvider in the element
    <objectdataprovider key="patients" objecttype="{x:Type src:PatientsEngine}" methodname="GetPatients"></objectdataprovider>
  5. Create a data template



    <DataTemplate x:Key="patientFormating" DataType="Patient">
    <StackPanel Orientation="Vertical">
    <TextBlock>
    <TextBlock.Text>
    <Binding Path="Name" />
    </TextBlock.Text>
    </TextBlock>
    <Image>
    <Image.Source>
    <Binding Path="Picture" />
    </Image.Source>
    </Image>
    </StackPanel>
    </DataTemplate>
  6. Add the code to display a listview in a DockPanel


    <DockPanel DataContext="{Binding Source={StaticResource patients}}" Grid.Column="0" Grid.Row="0">
    <ListView x:Name="listView1" ItemsSource="{Binding }" ItemTemplate="{DynamicResource patientFormating}" IsSynchronizedWithCurrentItem="True" DockPanel.Dock="Left" />
    </DockPanel />