Tuesday, October 4, 2011

‘The file exists’ and Path.GetTempFileName Exception

I was working with Acapela (Text to Speech) ActiveX control. My webservice is using this control to synthesize speech. The problem on production suddenly came with an error message.

System.IO.IOException: The file exists. 
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIOError()
   at System.IO.Path.GetTempFileName()

I was thinking about the issue why temp file is not created and it wrongly says ‘The file exists’.
In actual machine’s Temp folder (c:\windows\temp) contains a large number of temp files. The GetTempFileName method will raise an IOException if it is used to create more than 65535 files without deleting previous temporary files.  Each voice generation call by web service generates two temp files without deleting them. So there is no temp file sweep policy defined and this problem occurred when quota exceeded.
The GetTempFileName method will raise an IOException if no unique temporary file name is available. To resolve this error, delete all unnecessary temporary files.

Luckily it was not end user product, otherwise we have to provide the fixes to clients. I just fixed it on production server.

Thursday, September 15, 2011

TFS 2010 and Team Foundation Sidekicks 2010 - View all pending changes for all users

It is very useful using Team Foundation Sidekicks 2010 with TFS 2010. I installed the package (Version 3.0.4) from following link:

I was much interested in knowing “status sidekick” to get report of TFS project. Want to be sure that all users have checked-in pending changes for my project. It’s pretty cool.

Tuesday, August 23, 2011

Configure TFS2010 to enable Continuous Integration (CI) – How to Build and deploy a WCF service application?

Many development teams have adopted "agile" methodologies to manage change and to improve software quality. These methodologies promote continuous integration as a practice to build and test software products incrementally as new features are included, bugs are fixed, and code is refactored. Hence time to market is reduced.
Continuous Integration
Continuous integration is a simple, powerful technique that Martin Fowler promoted as a best practice in software development, and a technique that fits easily into an agile development process (see Continuous Integration). Continuous integration takes the notion of a daily build a step further by building a software product each time any unit of the software changes. In most cases, a build is created each time a developer checks any code or configuration change into a source control.  

My Build Environment
The situation is such that I was running a CI system with Visual Source Safe (VSS2008), CruiseControl.NET, FxCop, MsBuild, and in some projects with NAnt also. Then our onshore team decided to replace VSS2008 with TFS 2010. We managed migrate all source code as first step to TFS and decided not to touch existing CI process to keep migration simple. We just installed VSTS plugin for CrusieControl.NET from CodePlex, and wired up TFS2010 in CCnet.config projects. So far so good, TFS 2010 is smoothly working with CruiseControl.NET. Buts wait, it’s not the final goal, and we want to eliminate Cruise Control from CI process so that TFS2010 work using power of MSBuild and Team Build Service.
We thought about advantages/disadvantage of using TFS Build, topology should we prefer and security needs etc. After all we will utilize the power of TFS2010.
As first step, I will deploy a WCF service on remote machine say TARGETPC.  My TFS2010 is installed on Remote machine placed in Europe say TFSPC.
  1. Download and install the Microsoft Web Deployment Tool (or the 32bit equivalent) on the deployment machine TARGETPC (where the WCF service will ultimately be deployed).
  2. Create the IIS Web Site on the deployment server. Create a virtual directory (if needed) under website, and website should be started.
  3. Create a TFS Build Agent on the TFS server (See MSDN article Create and Work with Build Agents for details)
Visual Steps of Creating a new Build:

 Specify a workspace to get latest TFS code in Build Agent Folder specified (on TFSPC).

Select Clean Workspace = None so that only the files that are changed are uploaded to your IIS instance when we deploy. Copy Outputs to Drop Folder should set to false because don’t need a drop folder, so turn that off under Build Defaults by unchecking the box as well. I am using Agile methodology, so the Default Template will serve the purpose,
MSBuild Arguments are as follows:

/p:DeployOnBuild=True /p:DeployTarget=MsDeployPublish /p:MSDeployServiceURL=http://<RemoteServerIP> /p:DeployIISAppPath="<RemoteIISAppPath>" /p:CreatePackageOnPublish=False /p:MsDeployPublishMethod=RemoteAgent /p:AllowUntrustedCertificate=True /p:UserName=<yourusername> /p:Password=<yourpassword>

Here is parameter description:
1.    <RemoteServerIP> a simple IP address of TARGETPC machine on which “Web deployment agent service” should be installed.
2.      RemoteIISAppPath : I defined a new web site and a virtual directory in it. Because My virtual directory run under specific AppPool so I configure it once. So I specified following value precisely: "AutDist_Websites/Autodist.Server.CI.Deployment"
3.      User name and password of user having built rights in TFS server

When I use option “Queue New Build” I got following error message:

(3588): Web deployment task failed.(Remote agent could not be contacted.  Make sure the remote agent service is installed and started on the target computer.) The requested resource does not exist, or the requested URL is incorrect. Error details: Remote agent could not be contacted.  Make sure the remote agent service is installed and started on the target computer. An unsupported response was received. The response header 'MSDeploy.Response' was '' but 'v1' was expected. The remote server returned an error: (404) Not Found.

 Solution is as follows: The Web Deployment Tool does not install the Remote Agent Service by default. And I forgot to select the option in installation Wizard. If the Remote Agent Service is not configured correctly, the deployment activities outlined later in this past will result in an error message “The response header ‘MSDeploy.Response’ was” but ‘v1′ was expected”.
Steps to solve the problem are: go to Add/Remove program, rerun setup “Web Deployment Tool” and select option “Remote Agent Service”.

Now I can see the service entry under control manager.  The Web Deployment Agent Service is configured to start manually by default. It is necessary to reconfigure this to start automatically.

After starting the service I can see I can access following URL. Although display nothing but it asks for user credentials on Target machine.
Now on trying again I get following error:
Web deployment task failed. An error occurred when the request was processed on the remote computer.) An error occurred when the request was processed on the remote computer. Application 'ROOT' does not exist in site 'Automated.AutodistServer'.

Problem is I was providing virtual directory path directly and process was expecting is as IIS Website that it could not find. So I fixed it by creating a separate website and a virtual directory under it. RemoteIISAppPath = “Website Name\Virtual Dir Name”.
Now I retried queue new build and it is compiled smoothly, providing each build result, and finally WCF is deployed in target IIS Virtual directory. Cool, is not it. 
In future, I have to find out the ways to perform
- Code Analysis and Report merging
- Code Coverage and Unit Tests
- Merge Reports (needed?)
- Configure Emails to specific Team Members

Tuesday, June 28, 2011

Build XML based Business Rule Engine in .NET 4 – Post 3 (write runtime condition predicate)

It is continuation of first post - << click here to read
and second post - < click here to read

Dynamicity is of great advantage. First of all let us move forward step wise, define delegate to a separate method;
rule.AddCondtion(po => po.NetProductPrice >= 0);
should tends to

public static Predicate<PriceObject> IsPriceGreaterOrEqualToZero()
    return delegate(PriceObject po) { return po.NetProductPrice >= 0; };

Let us consider even more generic function to write. Point is every condition returns  Predicate<PriceObject> and internally the condition result is a Boolean variable (true/false). Why not create runtime generic delegate to handle Equals operator for example. We will make it compatible to other operators later.

public static Predicate<T> GetConditiPredicate<T>(string fieldName, string operatorName, string value)
    Type itemType = typeof(T);
    ParameterExpression predParam = Expression.Parameter(itemType, "po");
    PropertyInfo propertyInfo = itemType.GetProperty(fieldName);
    Expression left = Expression.Property(predParam, propertyInfo);
    Expression right = Expression.Constant(ObjTypeConverter.ChangeType(value, propertyInfo.PropertyType), propertyInfo.PropertyType);
    Expression equality = Expression.GreaterThanOrEqual(left, right);
    Func<T, bool> function = (Func<T, bool>)Expression.Lambda(equality, new[] { predParam }).Compile();
    return new Predicate<T>(function);

And caller can deliver a call like this

rule.AddCondtion(Dynamic.GetConditiPredicate<PriceObject>("DRM", "Equals", "2"));

Well compile and see if it works fine. It should :)

Now let us replace the statement with more general one….
//Expression equality = Expression.GreaterThanOrEqual(left, right);
Expression result = GetExpression(operatorName, left, right);
Func<T, bool> function = (Func<T, bool>)Expression.Lambda(result, new[] { predParam }).Compile();


public static Expression GetExpression(string operatorName, Expression left, Expression right)
    Expression result = null;
    switch (operatorName.ToLower())
        case "equals":
            result = Expression.Equal(left, right);
        case "greaterorequal":
            result = Expression.GreaterThanOrEqual(left, right);
        case "lessorequal":
            result = Expression.LessThanOrEqual(left, right);
        case "greaterthan":
            result = Expression.GreaterThan(left, right);
        case "lessthan":
            result = Expression.LessThan(left, right);
        case "add":
            result = Expression.Add(left, right);
        case "subtract":
            result = Expression.Subtract(left, right);
        case "multiply":
            result = Expression.Multiply(left, right);
        case "divide":
            result = Expression.Divide(left, right);
            throw new Exception("Unknown Operator Name");
    return result;
 This was simple to handle isolated conditions. How to handle Actions that may depend one another give us new dimension.

If want to play with Rule Engine source code - I extracted from my business case for demo purpose. download from skydrive. (FileName: ConfigurableRuleEngine.zip)
Comments please   

Saturday, June 25, 2011

Build XML based Business Rule Engine in .NET 4 – Post 2 (Design a Model)

It is continuation of first post - << click here to read

Let us start thinking about Rule Engine model.  
Entity 1: Price Object
To keep it simple I should define a Price Object, on which whole calculation will be performed to evaluate Transaction Fee.
public class PriceObject
    public decimal NetProductPrice { get; set; }
    public int DRM { get; set; }
    public decimal TransactionFee  { get; set; }
This object could be any thing in your scenario, we very much like it feed to the Rule Engine. I would rather say it 'Communication Channel or DTO'. Rule Engine apply defined rules on Channel Entity and provide results (in my case calculate and fill up TransactionFee).  
In some cases, it makes sense to have one input channel entity and rule engine return another channel entity.

Entity 2: Rule Engine
To keep rule engine simple, at high level, it contains a list of rules defined in XML per distributor. When caller request to run the rule engine, it supplies distributor identification and price object in question. Rule engine just pick the rules defined in xml in top-down order and run each one of them.
public class RuleEngine
    IDictionary<int, IRule> Rules = new Dictionary<int, IRule>();

    public void AddRule(IRule rule)
        Rules.Add(Rules.Count(), rule);

    public void Run(string distributorKey, PriceObject obj)
        var rules = Rules.Where(dr => dr.Value.DistributorName == distributorKey)
        foreach (var r in rules)


Entity 3: Rule Definition
Each rule is based on interface IRule that defines:
  1. -          IRule must know Distributor Name / identification whom it belongs to
  2. -          Can add conditions as runtime delegate (Predicate) and logical operation to inference using forward chaining mechanism
  3. -          Can add actions taken on Price Object type at runtime delegate (Custom one defined by me)
  4. -          Run the rule on actual Price Object
public interface IRule
    string DistributorName { get; }
    void AddCondtion(Predicate<PriceObject> condition, LogicalOperation operation = LogicalOperation.AND);
    void AddAction(ActionPredicate<PriceObject> action);
    void Run(PriceObject obj);

Then comes IRule implementation named ConcreteRule. It encapsulates a list of Conditions and List of Actions. When running a rule, it uses forward chaining and infers the final result (Boolean true or false) after evaluating each condition. Once get a conditions result it apply simple rule: 

IF conditions_result is true THEN execute each action sequentially END.

Here is simple code:
public class ConcreteRule : IRule
    IList<ICondition> conditions = new List<ICondition>();
    IList<IAction> actions = new List<IAction>();

    public string DistributorName { get; private set; }

    public ConcreteRule(string distributorName)
        DistributorName = distributorName;
    public void AddCondtion(Predicate<PriceObject> condition, LogicalOperation operation = LogicalOperation.AND)
        conditions.Add(new RuleCondition(condition, operation));
    public void AddAction(ActionPredicate<PriceObject> action)
        actions.Add(new RuleAction(action));

    public void Run(PriceObject obj)
        bool result = true;
        foreach (var c in conditions)
            var res = c.Validate(obj);

            if (c.Operation == LogicalOperation.AND)
                result = result && res;
                result = result || res;               

        if (result)
            foreach (var a in actions)


Entity 3: Condition Definition
Each condition is based on interface ICondition that defines:
  1. -          ICondition must know Logical Operator to infer final flag (forward chaining)
  2. -          ICondition can operate on actual Price Object for validation
public interface ICondition
    LogicalOperation Operation { get; }
    bool Validate(PriceObject obj);

And here is concrete Condition Implementation class named RuleCondition. You should focus on Validate method how simple it executes the delegate?
public class RuleCondition : ICondition
    readonly Predicate<PriceObject> Conditions;
    public LogicalOperation Operation
        private set;

    public RuleCondition(Predicate<PriceObject> conditions, LogicalOperation operation)
        this.Conditions = conditions;
        this.Operation = operation;

    public bool Validate(PriceObject priceObject)
        return Conditions(priceObject);

Entity 4: Action Definition
Each action is based on interface IAction with simple concept:
  1. -          IAction can execute a command on Price Object at runtime.
 Just I need a custom delegate named Action Predicate to return decimal. I cannot go more generic as I always deal with Transaction Fee calculation so limit it to return decimal value. Note T parameter in constructed type is contravariant.     
public delegate decimal ActionPredicate<in T>(T obj);

public interface IAction
    decimal Execute(PriceObject obj);

And here is concrete Action Implementation class named RuleAction. You should remember executes method and the delegate usage.
public class RuleAction : IAction
    readonly ActionPredicate<PriceObject> Action;

    public RuleAction(ActionPredicate<PriceObject> action, string key = null)
        this.Action = action;
        this.Key = key;


    public string Key
        private set;

    public decimal Execute(PriceObject priceObject)
        return Action(priceObject);          

Caller Module: Let us first write a static module and define hard coded rules for execution. J
private void DoWork()
    var engine = new RuleEngine();
    IRule rule = new ConcreteRule("AMAZON");
    rule.AddCondtion(po => po.NetProductPrice >= 0);
    rule.AddAction(po => po.TransactionFee = Math.Round(po.NetProductPrice * 0.05M, 2));

    rule = new ConcreteRule("AMAZON");
    rule.AddCondtion(po => po.DRM == 2);
    rule.AddCondtion(po => po.TransactionFee < 4M);
    rule.AddAction(po => po.TransactionFee = 4M);

    rule = new ConcreteRule("AMAZON");
    rule.AddCondtion(po => po.DRM == 3);
    rule.AddCondtion(po => po.TransactionFee < 8M);
    rule.AddAction(po => po.TransactionFee = 8M);

    rule = new ConcreteRule("AMAZON");
    rule.AddCondtion(po => po.TransactionFee > 10M);
    rule.AddAction(po => po.TransactionFee = 10M);

    var obj = new PriceObject
        NetProductPrice = 100,
        DRM = 3

    engine.Run("AMAZON", obj);


You can execute and ensure how beautifully rule engine is providing correct results.
Next is to think about possibility how to define runtime delegate so that I can wire up XML inputs to define dynamic conditions and actions.
My short target is to replace statement
rule.AddCondtion(po => po.NetProductPrice >= 0 )
with more dynamic one (assuming values come from XML):
rule.AddCondtion(Dynamic.GetConditiPredicate<PriceObject>("NetProductPrice", "GreaterOrEqual", "0"));

 I will discuss these possibilities in Next Post.  click here to read>>
Your comments are welcome.