HTTP caching divergence between IE & Firefox

 Firefox&IE
I came across an interesting bug caused by the way IE and Firefox diverge regarding how they implement Http caching. Pictures displayed on a web pages were supposed to be refrehed every 10 seconds by a javascript timer. The problem was that the images didn't refresh on Firefox.

I used Fiddler & Firebug to analyze the HTTP traffic and rapidly came to the conclusion that this bug was related to the HTTP caching mechanism. To solve this bug I had to understand how HTTP caching works. The HTTP 1.1 spec describes how the caching mechanism of a web server should be implemented - this is a simplified version:
A) The client send an HTTP request to a server
B) Based on several factors (see below) the server will decide if it will serve a specific resource with a response code 200 or  return a 304. A response 304 means that the client should use his cache to serve the resource.

A web server returns a 304 when all the following conditions are true:
1. The resource on the server is configured to be cached (for most of the web servers this means that cache is not disabled for the specific resource)
2. The Last modified date of the resource is < than the Request date
3. The Request does not contain any header that disable the cache (see RFC)
4. The resource is fresh enough (see RFC)

In my situation the web server returned a 200 when using IE and a 304 when using Firefox. This was because condition 1 -2 & 4 were true but 3 was only false when using IE. The page displying the images contained a meta tag: <META HTTP-EQUIV="Pragma" CONTENT="no-cache">.  This tag instructs IE to add the header pragma=no-cache when it  request the image.  This HTTP header instruct the web server that it has to return a 200 (see condition 3). The problem was that Firefox don't understand this tag.

I solved the bug by tackling the root cause of the problem; I disabled the cache on the web server for this particular resource.


If this is not feasible in your situation other possibilities are:
- Disable the cache via the request by adding the proper meta tags, for Firefox a valid tag is: <meta content="-1" http-equiv="max-age" >.
- Make sure that the last modified date is updated correctly
- Generate a random character as a querystring after the jpg extension: e.g: myimage.jpg?201003321023

 

kick it on DotNetKicks.com

HTTP caching divergence between IE & Firefox

 Firefox&IE
I came across an interesting bug caused by the way IE and Firefox diverge regarding how they implement Http caching. Pictures displayed on a web pages were supposed to be refrehed every 10 seconds by a javascript timer. The problem was that the images didn't refresh on Firefox.

I used Fiddler & Firebug to analyze the HTTP traffic and rapidly came to the conclusion that this bug was related to the HTTP caching mechanism. To solve this bug I had to understand how HTTP caching works. The HTTP 1.1 spec describes how the caching mechanism of a web server should be implemented - this is a simplified version:
A) The client send an HTTP request to a server
B) Based on several factors (see below) the server will decide if it will serve a specific resource with a response code 200 or  return a 304. A response 304 means that the client should use his cache to serve the resource.

A web server returns a 304 when all the following conditions are true:
1. The resource on the server is configured to be cached (for most of the web servers this means that cache is not disabled for the specific resource)
2. The Last modified date of the resource is < than the Request date
3. The Request does not contain any header that disable the cache (see RFC)
4. The resource is fresh enough (see RFC)

In my situation the web server returned a 200 when using IE and a 304 when using Firefox. This was because condition 1 -2 & 4 were true but 3 was only false when using IE. The page displying the images contained a meta tag: <META HTTP-EQUIV="Pragma" CONTENT="no-cache">.  This tag instructs IE to add the header pragma=no-cache when it  request the image.  This HTTP header instruct the web server that it has to return a 200 (see condition 3). The problem was that Firefox don't understand this tag.

I solved the bug by tackling the root cause of the problem; I disabled the cache on the web server for this particular resource.


If this is not feasible in your situation other possibilities are:
- Disable the cache via the request by adding the proper meta tags, for Firefox a valid tag is: <meta content="-1" http-equiv="max-age" >.
- Make sure that the last modified date is updated correctly
- Generate a random character as a querystring after the jpg extension: e.g: myimage.jpg?201003321023

 

kick it on DotNetKicks.com

ASP.NET MVC with Webforms

source code can be found here 

It’s now generally admitted in the community that Unit testing and TDD (Test Driven Development) are valuable techniques when it comes to increasing the overall quality of our code. Nevertheless unit testing can be costly especially when you’ve applications with a lot of logic implemented in the UI. Therefore if we want to make our application testable we need to separates the UI from the rest of the application.

Martin Fowler described on his site some patterns that separate and diminish the UI logic to a bare minimum. They are all variants of the classical MVC (Model View Controller) pattern. The MVC split the application in 3 parts: the view handles the display, the controller that responds to user gestures and the model that contains the domain logic. The MVC is the foundation of very popular portal frameworks like Ruby on Rails.

To build web sites applying the MVC pattern with .Net developers can choose among several MVC frameworks like Monorail or the new ASP.NET MVC. In anyway, MVC frameworks like ASP.NET MVC are based on completely different paradigm as the ASP.NET Webforms framework. This means that you have to re-learn to program web apps from scratch. Another setback is that there are no ways to refactor your old ASP.NET applications so that they can fit into the MVC framework. I want to make myself clear, I believe that frameworks like Monorail or the coming System.Web.MVC are the future way of programming web apps in .NET but it demands a considerable amount of effort to learn new frameworks. It’s difficult for someone like me who has invested lots of years in mastering the classical ASP.NET code-behind model to re-learn everything from scratch. In the meantime this should not be an excuse to not make my code more testable.

In this post I will explicit through a simple example how to use the model view controller pattern on top of the code-behind model. We will create a login form with the MVC pattern.

Setup your solution

Create a new solution “Zoo” with 3 projects –>

  • ZooWebsite-> ASP.NET web appplication
  • ZooLibrary -> Class library 
  • ZooTest - Class library
  • Create a reference from ZooWebsite to ZooLibrary

    (ZooWebsite , add reference, project tab select ZooLibrary)

  • On ZooLibrary add a reference to System.Web

ne project

 

The View

To make our code testable it’s very important to be able to decouple the UI from the ASP.NET code-behind.  Therefore we will create an interface our ASP.NET page should implement.  This View interface will represent the contract the UI as to conform to.  When we will test our controller we will not do this with our actual web page but through a mock object that implements the View interface.    

Add an interface ILoginView on the project ZooLibrary:

   1:  namespace ZooApplication.Library
   2:  {
   3:       public interface ILoginView
   4:      {
   5:          string ErrorMessage { get;set;}
   6:          string EmailAddress { get;set;}
   7:          string Password { get;set;}
   8:          void RedirectFromLoginPage();
   9:          System.Web.UI.WebControls.Button BtnLogin { get;set;}
  10:      }
  11:  }
  • Edit the default aspx page and enter: Welcome you are authenticated!
  • Add the login.aspx to the ZooWebsite project.
  • Edit the source of the login.aspx part -> add two textboxes, a button, and validators:
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="Login.aspx.cs" Inherits="ZooApplication.Website.Login" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
                Login form<br />
                <asp:Label ID="LblErrorMsg" runat="server" Text="Invalid login" Visible="false" ></asp:Label><br />
                Email Address:
                <asp:TextBox ID="TxbEmailAddress" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RfvEmailAddress" runat="server" ErrorMessage="Enter your email address!"
                    ControlToValidate="TxbEmailAddress">
                </asp:RequiredFieldValidator>
                <asp:RegularExpressionValidator ID="RevEmailAddress" runat="server" ControlToValidate="TxbEmailAddress"
                    ErrorMessage="Invalid email address!" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
                </asp:RegularExpressionValidator></div>
            <div>
                Password:
                <asp:TextBox ID="TxbPassword" runat="server"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RfvPassword" runat="server" ErrorMessage="Enter your password!"
                    ControlToValidate="TxbPassword">
                </asp:RequiredFieldValidator>
            </div>
            <div>
                <asp:Button ID="PageBtnLogin" runat="server" Text="Login" />
            </div>
        </div>
    </form>
</body>
</html>

Because the code-behind is not testable and don’t make part of the SUT (Subject Under Test) we want to diminish the code-behind logic to a bare minimum.   The view responsibility is limited to output  the data coming from our model in a human readable way and to expose user input to the controller.

Therefore we implement our interface through an aspx page that only contains a set of properties that binds data coming from the controller with our web controls. 

Generally we will try to implement all the presentation logic into the controller. The only exception here will be the RedirectFromLoginPage() method.

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Collections;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.WebControls;
   9:  using System.Web.UI.WebControls.WebParts;
  10:  using System.Web.UI.HtmlControls;
  11:  using ZooApplication.Library;
  12:   
  13:  namespace ZooApplication.Website
  14:  {
  15:      public partial class Login : System.Web.UI.Page, ZooApplication.Library.ILoginView
  16:      {
  17:   
  18:          public string EmailAddress
  19:          {
  20:              get
  21:              {
  22:                  return TxbEmailAddress.Text;
  23:              }
  24:              set
  25:              {
  26:                  TxbEmailAddress.Text = value;
  27:              }
  28:          }
  29:   
  30:          public string ErrorMessage
  31:          {
  32:              get
  33:              {
  34:                  return LblErrorMsg.Text;
  35:              }
  36:              set
  37:              {
  38:                  LblErrorMsg.Text = value;
  39:              }
  40:          }
  41:   
  42:          public string Password
  43:          {
  44:              get
  45:              {
  46:                  return TxbPassword.Text;
  47:              }
  48:              set
  49:              {
  50:                  TxbPassword.Text = value;
  51:              }
  52:          }
  53:   
  54:          public Button BtnLogin
  55:          {
  56:              get
  57:              {
  58:                  return PageBtnLogin;
  59:              }
  60:              set
  61:              {
  62:                  PageBtnLogin = value;
  63:              }
  64:          }
  65:   
  66:          public void RedirectFromLoginPage()
  67:          {
  68:              FormsAuthentication.RedirectFromLoginPage(this.EmailAddress, false);
  69:          }
  70:      }
  71:  }

 

The model

It’s our model that is responsible to validate the user login and password against the DB. 

We create a DB named ZooDB:

  • Add an APP_Data folder to your ZooWebsite project
  • APP_Data, new item, Database

Execute this script to create a table Profiles on the ZooDB:

CREATE TABLE [dbo].[Profiles](
      [ProfileID] [int] IDENTITY(1,1) NOT NULL,
      [EmailAddress] [nvarchar](255) NOT NULL,
      [Password] [nvarchar](255) NOT NULL,
 CONSTRAINT [PK_Profiles] PRIMARY KEY CLUSTERED
(
      [ProfileID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Now configure your web.config file to add the connectionstring and the authentication part:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="ZooDB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=.\App_Data\ZooDB.mdf;Integrated Security=True"/>
  </connectionStrings>
    <system.web>
      <authentication mode="Forms">
        <forms name="AuthCookie" path="/" loginUrl="login.aspx" protection="All" timeout="10">
        </forms>
      </authentication>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>

image002

Test the application, it should compile and we should be redirected to the login page

image004

Our model implements an Authenticate method. Again we will make use of interfaces to decouple the model from the controller.

On the ZooLibrary project create an interface ILoginModel:

   1:  namespace ZooApplication.Library
   2:  {
   3:      public interface ILoginModel
   4:      {
   5:          bool Authenticate(string emailAddress, string password);
   6:      }
   7:  }

 

The method Authenticate of the class LoginModel will check the validity of supplied email address and password.  You can implement the model with your preferred data access code.  Personally I use Subsonic because it’s really simple to use and it’s based on the active record pattern, the same pattern used in Rails.

But for the moment let’s use standard ADO.NET code:

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:  using System.Data.SqlClient;
   5:  using System.Configuration;
   6:   
   7:  namespace ZooApplication.Library
   8:  {
   9:      public class LoginModel : ILoginModel
  10:      {
  11:          public bool Authenticate(string emailAddress, string password)
  12:          {
  13:              SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ZooDB"].ConnectionString);
  14:              SqlCommand cmd = new SqlCommand(
  15:                  "SELECT count(EmailAddress) FROM [Profiles] " +
  16:                  "WHERE EmailAddress=@EmailAddress AND Password=@Password",
  17:                  conn);
  18:              cmd.Parameters.AddWithValue("@EmailAddress", emailAddress);
  19:              cmd.Parameters.AddWithValue("@Password", password);
  20:              try
  21:              {
  22:                  conn.Open();
  23:                  if((int)cmd.ExecuteScalar()==1)
  24:                      return true;
  25:   
  26:                  return false;
  27:              }
  28:              finally
  29:              {
  30:                  conn.Close();
  31:              }
  32:          }
  33:      }
  34:  } 

The controller

The controller job is to figure out how the view should display the model. Therefore the controller should have an association with the model and the view.

  • Add a new class to ZooLibrary name it LoginController.

We start by defining a constructor that takes a ILoginView and a ILoginModel as parameters.
The Initialize method will be called by the page to instruct the controller to take control over the view and the model.
In the initialize method we will prepare the view to be rendered and subscribe to the events triggered by the view.

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Text;
   4:   
   5:  namespace ZooApplication.Library
   6:  {
   7:      public class LoginController
   8:      {
   9:          private ILoginView _view;
  10:          private ILoginModel _model;
  11:         
  12:   
  13:          public LoginControler(ILoginView view, ILoginModel model)
  14:          {
  15:              this._view = view;
  16:              this._model = model;         
  17:          }
  18:   
  19:          public void Initialize()
  20:          {
  21:              this._view.ErrorMessage = "";
  22:              this._view.BtnLogin.Click += new EventHandler(BtnLogin_Click);
  23:          }
  24:   
  25:          public void BtnLogin_Click(object sender, EventArgs e)
  26:          {
  27:              if (this._model.Authenticate(this._view.EmailAddress, this._view.Password))
  28:                  this._view.RedirectFromLoginPage();
  29:              else
  30:                  this._view.ErrorMessage = "Invalid emailaddress or password!";
  31:          }
  32:      }
  33:  }

clip_image002

 

Integrating the MVC into the page

When we program against an asmx page it’s always the page that receive the initial control from the ASP.NET framework.
So it’s the page that need to instantiate the model, the view and the controller.

   1:      public partial class Login : System.Web.UI.Page, ZooApplication.Library.ILoginView
   2:      {
   3:          private LoginController controller;
   4:   
   5:          protected override void OnInit(EventArgs e)
   6:          {
   7:              base.OnInit(e);
   8:   
   9:              ILoginModel model = new LoginModel();
  10:              controller = new LoginController(this, model);
  11:          }
  12:          protected void Page_Load(object sender, EventArgs e)
  13:          {
  14:              controller.Initialize();
  15:          }
  16:  
  17:       }
 

Testing

We are now able with the help of mocking frameworks to test the logic in the model and the controller.

The code here uses NMock but you can use your prefered mocking framework to implement your tests:
In the meantime the test for our LoginController should look like this:

   1:  public void LoginController_LoginTest()
   2:          {
   3:              ILoginView view = mocks.NewMock<ILoginView>();
   4:   
   5:              ILoginModel model = mocks.NewMock<ILoginModel>();
   6:              LoginController target = new LoginController(view, model);
   7:             
   8:              Expect.AtLeastOnce.On(view).GetProperty("BtnLogin").Will(Return.Value(new System.Web.UI.WebControls.Button()));
   9:              Expect.AtLeastOnce.On(view).GetProperty("EmailAddress").Will(Return.Value("unitEmail@test.be"));
  10:              Expect.AtLeastOnce.On(view).GetProperty("Password").Will(Return.Value("password"));
  11:              Expect.Once.On(model).Method("Authenticate").With(view.EmailAddress, view.Password).Will(Return.Value(true));
  12:              Expect.Once.On(view).SetProperty("ErrorMessage").To("");
  13:              Expect.AtLeastOnce.On(view).Method("RedirectFromLoginPage");
  14:   
  15:              target.Initialize();
  16:              target.BtnLogin_Click(view.BtnLogin, null);
  17:   
  18:              mocks.VerifyAllExpectationsHaveBeenMet();
  19:             
  20:          }

I hope this introduction to the MVC pattern has been profitable to you.

 

kick it on DotNetKicks.com

GIT with VisualStudio

It’s about a week that I’m trying to use Git with Visual Studio. My intention was to replace SVN by Git because it allows for a more complete development workflow.

The idea is that you can make local commits of your changes and when the changes are mature enough they can be pushed on a central Git or SVN repository. This provides more flexibility and shorten the time between two commits. 

I found the experience of working with a local source controller very convenient and I’m  convinced that this model will soon be adopted on all platforms.  Nevertheless my experience with Git was not completely positive and I’m not planning (any more) to abandon SVN/Ankh/Tortoise. I’m sure that Git makes a perfect fit when developing on Linux but my experience on Windows/VisualStudio was mitigated.

These are my list of pro’s and con’s.
Pros:

  • Enables local commits
  • Very lightweight
  • Command line tools are simple and powerful
  • Branching, Merging and Taggging are easy and reliable
  • Good integration with Windows through TortoiseGit
  • Perfect integration with SVN (you can use Git as a client for SVN)

Cons:

  • Lack of integration with VisualStudio. I tried Git Extensions but compared to AnkhSVN this tool is just a buggy toy.
  • No mature hosting providers. GitHub is the primary repository for Git in the cloud, the one I tested. Frankly I found their security mode (SSH key) not user friendly. Also the tracking tools are poor and at least confusing compared to an Assembla.

Because of these relatively important problems I plan to continue using SVN for the moment. Nevertheless when I need a small local source controller (e.g. when giving courses/presentation) Git is definitely the way to go. Also I plan to use Git when I’m disconnected from the SVN repository for more than a day.

 

Kata2: The Decorator Pattern

 

This the second part of the series of Coding Kata’s. Here we’ll explore the Decorator pattern.

This is the user story:

You need to send alerts to your customers when a new product is available. 
An Alert is represented by the Alert Class:

public class Alert
{
      public String DestinationAddress { get; set; }
      public String SenderAddress { get; set; }
      public String Content { get; set; }
      public string Log { get; set; }
}

Classes used to send the Alerts implement the following interface:

public interface ISender
{
        void Send(Alert alert);
}

 

  • Create an Alerter class that send the appropriate alerts.  
  • Alerts are always send by e-mail.
  • Customers can also subscribe to receive alerts via sms and/or through messenger. 
  • Use the Decorator pattern to configure the Alerter

    You do not need to use real infrastructure code to send the alert, just append a string that contains the Sender through which the message was send.
    ->Append the following text to the Alert.Log when the message is send by:
  • e-mail      : “Message was send by e-mail”
  • sms         : “Message was send by sms”
  • messenger: “Message was send by messenger”

 

Show Solution

 

Coding Kata on design patterns: Kata1, The Abstract Factory Pattern

 

This post is the first in a series dedicated to Coding Kata’s and design patterns.  

Because I’m not always able to remember all the patterns I decided to learn by practice.  

Every week I will try to create a Kata with a simple problem that has to be resolved with one of the Gov’s design patterns.  

Because I want to start smoothly we’ll start with one of the simplest pattern -> the Abstract Factory.

Kata1 ->

- A product has a property Name.  

- Because the Name is used as an identifier it can’t be changed.  

- We need to be able to construct 2 products with following names: Product1, Product2.

- Use the abstract factory pattern so that you’re able to create Products.

 

This is my solution for Kata1.

 

 

Sharepoint 2007 Certification Guide

Here under you can find two usefull ressources to prepare for the exam:

 

-       The Wrox book: Professional Sharepoint 2007 Development

 

-       The Blog of: Adam Roderick

 

By focusing primarily on these two resources I managed to pass the Sharepoint Certification with what could be considered a ‘high score’. 

Create a WCF service and client, using Msmq, programmatically

  download source code

Most of the WCF examples on the web uses the config file to setup the address, binding and contract of a WCF service. I dislike this way of configuring client and service in WCF because most of the time it leads to config files that are full of crab and that becomes unmanageable. I prefer to construct my WCF services programmatically. Therefore I’ve create a bunch of helper classes that contains most of the configuration settings that are applicable in a particular domain.

In this article I discuss one of these classes  I use to create local queues. With local I mean queues used by components making part of the same application. This class can be used as a factory to create the service (serviceHost) as well as the client part (Channel) for components communicating through Msmq.

This article provides a small tutorial on how to create a simple console app. I demonstrate how to create the client and service host without any configuration by using a helper factory class: WCFMsmqFactory

Prerequisite:

- This article assumes you’ve installed Msmq on your box.  If this isn’t the case install Msmq see->  here for Xp & Win server 2003 or here for Vista & Win 2008 & Seven.

- If you’re a vista user you need to register your namespace for by typing in the command (in administrator mode):
netsh http add urlacl url=http://+:8000/BackToOwner/gatewayservice/sms user=[your username]

1) Create a new console application project: ‘WCFMsmqFactory’ and add a reference to

  • System.ServiceModel
  • System.Messaging
  • System.Transactions

 

2) Create a the WcfMsmqFactory class by copying the source code here beneath:

   1:  public class WcfMsmqFactory<I, T> where T : I
   2:     {
   3:         private string _queueAddress;
   4:         public WcfMsmqFactory(string queueAddress)
   5:         {
   6:             _queueAddress = queueAddress; 
   7:   
   8:             if (!MessageQueue.Exists(queueAddress))
   9:                 MessageQueue.Create(queueAddress, true);
  10:         } 
  11:   
  12:         public ServiceHost CreateMsmqServiceHost(string metadataAddress, string namespaceName)
  13:         {
  14:             var sHost = new ServiceHost(typeof(T));
  15:             var binding = new MsmqIntegrationBinding(MsmqIntegrationSecurityMode.None);
  16:             binding.DeadLetterQueue = DeadLetterQueue.System;
  17:             binding.Namespace = namespaceName;
  18:             sHost.AddServiceEndpoint(typeof(I),
  19:                                            binding,
  20:                                            new Uri(String.Format(
  21:                                                        @"msmq.formatname:DIRECT=OS:{0}",
  22:                                                        _queueAddress)
  23:                                                )
  24:                 ); 
  25:   
  26:             // Expose the service metadata on the metadataAddress
  27:             var smb = new ServiceMetadataBehavior();
  28:             smb.HttpGetEnabled = true;
  29:             smb.HttpGetUrl = new Uri(metadataAddress);
  30:             sHost.Description.Behaviors.Add(smb); 
  31:   
  32:             return sHost;
  33:         } 
  34:   
  35:         public I CreateChannel()
  36:         {
  37:             var binding = new MsmqIntegrationBinding(MsmqIntegrationSecurityMode.None);
  38:             var address = new EndpointAddress(String.Format("msmq.formatname:DIRECT=OS:{0}", _queueAddress));
  39:             var channelFactory = new ChannelFactory<I>(binding, address);
  40:             return channelFactory.CreateChannel();
  41:         }
  42:     }
  43:   

This class is perfectly reusable in any project and provide an abstraction on how to create programaticaly WCF services using the msmq binding. To instantiate the class we’ve to pass the interface and his actual implementation. The interface type we’ll be used to create the client & server part. The implementation type (service) will only be used to create the server.
The constructor creates a queue if it’s not already available. Note that the example don’t use security as the transport level security would demand to use active directory. As my pc does not connect to an Active Directory server I had to instantiate the binding without security:

var binding = new MsmqIntegrationBinding(MsmqIntegrationSecurityMode.None);

Not setting this setting resulted to the following error message: ” Binding validation failed because the binding's MsmqAuthenticationMode property is set to WindowsDomain but MSMQ is installed with Active Directory integration disabled. The channel factory or service host cannot be opened”

For the dead letter queue I use the default dead letter queue (see comment) settings:

binding.DeadLetterQueue = DeadLetterQueue.System;

To prevent the service to display the namespace as Tempuri.org it’s important to provide the same namespace settings as in your service contract & behavior directive.

binding.Namespace = namespaceName;

The following code exposes the metadata information (wsdl) on the provided metadata address:

var smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.HttpGetUrl = new Uri(metadataAddress);
sHost.Description.Behaviors.Add(smb);

3) Now lets create the message definition.
Add a new class SmsMessage:

public class SmsMessage
{
        public string Number { get; set; }
        public string Body { get; set; }
}

4) Define the service definition:
Add the file SmsService.cs:

   1:  [ServiceContract(Namespace = "http://My.Domain.Services.WCF")]
   2:  [ServiceKnownType(typeof(SmsMessage))]
   3:  public interface ISmsService
   4:  {
   5:      [OperationContract(IsOneWay = true, Action = "*")]
   6:      void SubmitSms(MsmqMessage<SmsMessage> msg);
   7:  } 
   8:   
   9:  [ServiceBehavior(Namespace = "http://My.Domain.Services.WCF")]
  10:  public class SmsService : ISmsService
  11:  {
  12:      public void SubmitSms(MsmqMessage<SmsMessage> msg)
  13:      {
  14:          var sms = msg.Body;
  15:          Console.WriteLine(string.Format(
  16:                                "SMS send to {0} with body='{1}'",
  17:                                sms.Number,
  18:                                sms.Body)
  19:              ); 
  20:   
  21:      }
  22:  }
  23:   

The SmsService class contains the real implementation code. As the purpose of this article is not to demonstrate how to actually send sms messages I didn’t provide the real code here. This demo only output the message on the console.

5) Finally let’s put all the parts together.
Add the following code to the main part of the program:

   1:   static void Main(string[] args)
   2:      {
   3:          //define variables
   4:          const string queueAddress = @".\private$\sms";
   5:          const string metadataAddress = "http://localhost:8000/BackToOwner/gatewayservice/sms";
   6:          const string nameSpaceName = "http://My.Domain.Services.WCF";
   7:          var message = new SmsMessage()
   8:                            {
   9:                                Number = "+322678821",
  10:                                Body = "This is a sample sms message!"
  11:                            };
  12:          var msmqMessage = new MsmqMessage<SmsMessage>(message); 
  13:   
  14:          //define factory 
  15:          var factory = new WcfMsmqFactory<ISmsService, SmsService>(queueAddress); 
  16:   
  17:          //Do the work
  18:          using(var serviceHost = factory.CreateMsmqServiceHost(metadataAddress,nameSpaceName))
  19:          {
  20:              Console.WriteLine("Starting the server...");
  21:              serviceHost.Open();
  22:              Console.WriteLine("Instantiating the client channel...");
  23:              var channel = factory.CreateChannel();
  24:              using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
  25:              {
  26:                  Console.WriteLine("Sending the message...");
  27:                  channel.SubmitSms(msmqMessage);
  28:                  scope.Complete();
  29:              }
  30:              Thread.Sleep(1000);
  31:              Console.WriteLine("Closing the server...");
  32:              serviceHost.Close();
  33:          }
  34:          Console.ReadLine();
  35:      }
  36:  }
  37:   
 

   Running the application should display a console with:
Starting the server...
Instantiating the client channel...
Sending the message...
SMS send to +322678821 with body='This is a sample sms message!'
Closing the server...

The real magic happens here:

var factory = new WcfMsmqFactory<ISmsService, SmsService>(queueAddress);

With this few line of code we’ve instantiated a factory that is able to create the client as the server of our service. The only thing we’ve to pass is the queue address – the rest of the configuration is encapsulated in our Factory class and can be easily reused.   We can provide our factory classes to all the enterprise and create a framework on top of WCF that will standardize and facilitate how WCF is used inside our company.

GVD

kick it on DotNetKicks.com

Visual Studio TextMate Font Black Background

 

 

 

 

 

 

It’s about 2 years now that I use a black background in Visual studio with TextMate inspired font "Monaco" and I still love it. For those who wants to try you can download the fonts and the setting file from my server on http://www.belgianagencies.com/content/Visual Studio 2008_Monaco.zip
Install the fonts by copying the Monaco.ttf to c:\windows\fonts and use the Visual Studio Tools-Import & Export settings wizard to configure the font’s settings.

GVD

View Geoffrey Vandiest's profile on LinkedIn

What is the role of a software architect in an Agile Enterprise?

I’ve defined here under what my vision is on the tasks and responsibilities of a Agile Software Architect role should have in the Enterprise. The Enterprise usualy also  has another role that could be defined as the Enterprise Architect whose role is more strategic and cross project. The list here under is applicable to the first category, the once actively participating in software projects.

Project Tasks

  • Take final responsibility for the quality of the software produced
  • Take the important technical decisions taking all quality aspects of the project into consideration (Cost, Maintainability, Availability, Scalability, Security and all other ty’s)
  • Mesure the quality through metrics and code reviews
  • Enforce changes if quality is not in line with standards
  • Select, in the context of the project, what the software engineering practices should be and how they are applicable for the concrete project
  • Define code coverageCode quality and encouragement of best practices
  • Coaching of and adherence to "Agile" software development principles for module design and implementation
  • Take the lead when building prototypes, common tools, general solutions and reusable components
  • Engage the teams in both defining and executing on such tasks
    The people often best suited to build prototypes are those with knowledge of the intimate details
  • Building reusable components comes from first building usable components
  • Management of common libraries
  • Define based on stakeholders requirements what the Performance, scalability and high-availability requirements are.
  • Interface between technical teams (Service team, Project, Development Center, Quality Assurance…), business stakeholders and development teams on technical matters
  • Coordination with QA and release engineers
  • Integration, performance and load testing strategies
  • Release, migration and upgrade strategies
  • Coaching and working directly with development teams on a regular basis
  • Technical customer to the agile development teams
  • Able to add technical user stories to the product backlog
  • Works directly with the product owners to prioritize and trim the product backlog

Responsabilities

  • Participate in the design of the software engineering process(es)
  • High-level technical vision, planning and documentation
  • Automate and define the tools that support the software engineering process(es)
  • Technical oversight in specific technical domain
    EAI, .Net, Java, Packages, ….

I would like to underline what in my opinion differentiate an agile software architect from how our role is usually perceived.
An Agile software architect doesn’t take part to a project only at its beginning, but during the whole project’s lifecycle to ensure a right implementation of the design and architecture. He also has to keep listening to new requirements to adapt solutions if needed.
Even if I acknowledge that an architect has to step back from his work and the teams’ work, I don’t conceive an architect who would remain far from the implementation team. Indeed, an architect can have to write lines of code (at least to create prototypes), but also has to get his hands dirty with code to validate the quality of what is produced or resolve a particularly technically difficult situation.

Further I would like to refer to some toughts of Martin Fowler expressed in the article by Martin Fowler “Who needs Architect”

http://martinfowler.com/ieeeSoftware/whoNeedsArchitect.pdf

“architecture is the decisions that you wish you could get right early in a project, but that you are not necessarily more likely to get them right than any other”

“A guide is a more experienced and skillful team member who teaches other team members to better fend for themselves yet is always there for the really tricky stuff.”

I would also recommend the following webcast with again Martin Fowler on the role of an architect in an Agile Organization:http://www.infoq.com/presentations/agilists-and-architects

GVD

View Geoffrey Vandiest's profile on LinkedIn