Wednesday, September 30, 2015

A good way to manage WCF connections

A typical problem with WCF connections is that programmer can not relay on using statement to manage a connection. A following code is bad:
using (var client = new SomeWCFServiceClient()) 
{
    //Do something with the client 
}
There are few stackoverflow threads about how to do it. But not a single one of them satisfy my typical needs.

This is how I do it.
In a service class:

IBuildServiceDisposable GetBuildWebService()
{
   return _buildWebService.GetBuildWsClient();
}

private WcfDisposableWrapper<IBuildServiceDisposable> WcfService
{
   get
   {
      return new WcfDisposableWrapper<IBuildServiceDisposable>();
   }
}

public Brochure GetBrochures(BrochureRequest param)
{
  return WcfService.Use(
                GetBuildWebService(),
                client => client.GetBrochures(param));
}
Where the service interface inherits from an interface that was auto generated by client proxy (IBuildService) is defined as:
public interface IBuildServiceDisposable : IBuildService, IDisposable, ICommunicationObject {}
The most important part is implementation of WcfDisposableWrapper class itself:
public class WcfDisposableWrapper<T> where T : ICommunicationObject, IDisposable
{
  public TReturnType Use<TReturnType>(T clientProxy, Func<T, TReturnType> codeBlock)
  {
    bool success = false;
    try
    {
       var returnValue = codeBlock(clientProxy);
       clientProxy.Close();
       success = true;
       return returnValue;
    }
    finally
    {
      if (!success)
      {
         clientProxy.Abort();
         clientProxy.Dispose();
      }
    }
  }
}

Saturday, August 8, 2015

Euler was down again

I don't know why anyone would like to attack sites like EulerProject but it was third time the site was hacked - or maybe it was hacked more times but I don't visit it that often to be aware of it.

Wednesday, July 15, 2015

Jenkins: ERROR: Failed to update

My dev machine was building code correctly, and everything was running. Unfortunately CI environment was throwing following exception.
[iTROPICS-TRUNK] $ "C:\Program Files\TortoiseHg/hg" --config auth.jenkins.prefix=* --config ******** --config ******** --config "auth.jenkins.schemes=http https" update --clean --rev default
abort: C:\Jenkins\Workspace\iTROPICS-TRUNK\src/TTC.iTropics.ServiceReferences/Service References/iTropicsBrokerBuildWS/TTC.iTropics.ServiceReferences.iTropicsBrokerBuildWS.com_tropics_webservice_iTropics_vo_current_common_WSGetAssociatedMiscProductsResponse.datasource: The system cannot find the path specified
ERROR: Failed to update
ERROR: Failed to update
It is because machine hit the maximum path length – 260 chars.

Tuesday, June 23, 2015

gitignore file repository

github hosts a repository for .gitignore files. C# developers are interested in VisualStudio template.
To make NCrunch ignored I had to add additional line:
*ncrunch*

Git remove files that were pushed but are added to .gitignore file

git rm -r --cached . 
git add .
git commit -am "Remove files ignored in .gitignore"

IEnumerable Sum extension for complex type

I was not able to find a simple/working example in internet. This is how to do it:
public static Money Sum<T>(this IEnumerable<T> data, Func<T, Money> selector)
        {
            var money = new Money(0);
            money = data.Select(selector.Invoke).Aggregate(money, (current, selectedMoney) => current + selectedMoney);
            return money;
        }

Saturday, April 18, 2015

WinDbg strikes back

Typical problems with winDbg:

.loadby sos mscorwks
Unable to find module 'mscorwks'


for .NET framework 4+ use
.loadby sos clr

SOS does not support the current target architecture.


I see this exception when I run multiple commands because dump was created on 64 bit arch for 32 bit process (I still recommend taking dump using 32 bit Task Manager that is located under C:\Windows\SysWOW64).
!wow64exts.sw
Helps to run some commands like
!clrstack