Friday, June 4, 2010

How to Use WinMerge as the Diff tool for Mercurial

Here is how you can setup TortoiseHg to use WinMerge for visual diffs and 3-way merges (instead of using KDiff):
  1. Open Mercurial.ini config file. On my WIN7 system, this file exists at C:\Users\MyUser\Mercurial.ini. If you are on an XP system, it's probably in C:\Documents and Settings\<your user name>\Mercurial.ini.
  2. Add the following lines:
    [extdiff]
    cmd.winmerge = C:\Program Files (x86)\WinMerge\WinMergeU.exe
    opts.winmerge = /e /x /ub /wl

    [merge-tools]
    winmergeu.executable = C:\Program Files (x86)\WinMerge\WinMergeU.exe
    winmergeu.priority= 1
    winmergeu.fixeol=True
    winmergeu.checkchanged=True
    winmergeu.args= /e /ub /dl other /dr local $other $local $output
    winmergeu.gui=False
    winmergeu.binary=True

    [tortoisehg]
    vdiff = winmerge
  3. Now run TortoiseHg's Global Settings tool. On the TortoiseHg tab, select winmerge for the Visual Diff Command, and winmergeu for the Three-way Merge Tool options, apply, close.
Sources:
http://superuser.com/questions/23576/how-to-use-winmerge-as-the-diff-tool-for-mercurial
http://stackoverflow.com/questions/2073543/use-winmerge-as-tortoisehg-merge-tool

Tuesday, April 27, 2010

Add binary serialization to a WCF webservice using UsernameOverTransport security

In order to enable this we have to add a new binding to our service and specify which endpoint to use. Add the following binding to the bindings section in the web.config on the service and client side:

<customBinding>

        <binding name="binaryHttpBinding">

          <security authenticationMode="UserNameOverTransport" requireSecurityContextCancellation="true" >

            <secureConversationBootstrap authenticationMode="UserNameForSslNegotiated" />

          </security>         

          <binaryMessageEncoding maxReadPoolSize="2147483647" maxSessionSize="2147483647" maxWritePoolSize="2147483647">

            <readerQuotas maxDepth="32" maxStringContentLength="5242880"

            maxArrayLength="200000" maxBytesPerRead="4096" maxNameTableCharCount="16384" />

          </binaryMessageEncoding>         

          <httpsTransport maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"></httpsTransport>         

        </binding>       

      </customBinding>

Specify an endpoint for it like this, and you are ready to go:

<endpoint address="" binding="customBinding" bindingConfiguration="binaryHttpBinding" contract="ServiceLibrary.MyService" />   

Hope this helps, I searched a while for the solution and it wasn't on the web.

Starting to love this WCF thingy :)

Saturday, March 20, 2010

How to properly close WCF connections

    Seems that reading InfoQ is really a good thing, even though the content related to .NET is pretty weak, it has really good content on Architecture and Languages.While doing the weekly swoop I stumbled across this article which triggered the alarm/sleepy memory in my head. The not so funny thing is that just last week I implemented the Dispose pattern for some WCF connections to a web service,  because I’m a programmer and programmers are lazy, and lazy means less code to do more, which kind of forces you to use the Dispose pattern and the “using" statement to automatically close your connections.
    If you don’t have the time to read the article and the references, the summary is that using the Dispose pattern is a bad practice for WCF connections, because well, someone at MSFT screwed up the design and ICommunicationObject.Close() throws a bunch of exceptions, which goes against the specifications for IDisposable which should NEVER throw an exception on Dispose(). So let’s see what code I have before:

   17 public partial class IFooServiceClient : IDisposable
   18 {
   19     public void Dispose()
   20     {
   21         Close();
   22     }
   23 }
    This is not good code, but not really bad either as long as Close doesn’t throw, which does, so slap in the face and let’s fix this. Someone proposed something like this which make things worse:
   25 public partial class IFooServiceClient : IDisposable
   26 {
   27     public void Dispose()
   28     {
   29         try
   30         {
   31             Close();
   32         }
   33         catch (Exception)
   34         {
   35             if (State == CommunicationState.Opened)
   36             {
   37                 Close();
   38             }
   39             else
   40             {
   41                 Abort();
   42             }
   43         }
   44     }
   45 }
    This is bad because you lose the exception context and this has the possibility to deadlock as well, checking the channel state is an anti pattern as well. This is the solution I came up with:
class IFooServiceClientHelper
{
    internal T CallServiceMethod<T>(ICommunicationObject service,    Func<T> serviceMethod)
    {
        T returnedInstance = default(T);
        try
        {
            returnedInstance = serviceMethod();
            service.Close();
        }
        catch (FaultException<IFooServiceFault> iof)
        {
            service.Abort();
            throw new IFooServiceException("Executing" + serviceMethod.Method.Name + " has failed.", iof);
        }
        catch (CommunicationException e)
        {
            service.Abort();
            throw new IFooServiceConnectionException("There was a communication problem. " + e.Message, e);
        }
        catch (TimeoutException e)
        {
            service.Abort();
            throw new IFooServiceConnectionException("The service operation timed out. " + e.Message, e);
        }
        catch (InvalidMessageContractException e)
        {
            service.Abort();
            throw new IFooServiceConnectionException(
                "The DataContract for the web service is not up-to-date. " + e.Message, e);
        }
        catch (Exception e)
        {
            service.Abort();
            throw new IFooServiceException("Executing" + serviceMethod.Method.Name + " has failed." + e.Message, e);
        }
        return returnedInstance;
    }
}
    This helper method has multiple overloads in order to call Func with parameters but the idea is the same. Calling this is pretty easy:
public Foo GetFoo()
{
    var service = new FooServiceClient();
    var serviceHelper = new FooServiceHelper();
    return serviceHelper.CallServiceMethod<Foo>(service, service.GetFoo);
}

This is better than we had before, but maybe I can improve this more. Any ideas? 
Develop with passion, 
Adrian

Wednesday, January 27, 2010

IPad

     Today Apple announced their next offering: IPad. From what it seems, this is going to be the real competitor to Kindle DX, and Amazon even has to do some catch-up in a field which until now they were leading.
     The IPad has some serious downsides,  maybe the best outcome is Amazon releasing a cheaper/better Kindle soon?
     IPad is interesting, but I still favor a netbook/PC + e-Reader, a lot more openness and possibilities.

Sunday, January 24, 2010

Building my own website

    I've started to read Professional ASP.NET MVC 1.0 , the first chapter is free. After working with Web Forms for a short period (a year and a half) the MVC framework seems like a breeze of fresh air. I don't really understand the whole debate on Web Forms vs. MVC, it's pretty clear which one is cleaner, more robust and easy to use. And no, the lack of controls is not a minus, the web is based on HTML and JavaScript served through HTTP, then why bury all those inside a bunch of web controls when we can use HTML directly?
   Giving the fact that at work I will continue working with Web Forms for the next year I think, I need to start some home project to learn Asp.Net MVC. I'm thinking for some time at creating my own website, so this seems to be a great opportunity to do something useful while learning. I know that there are a bunch of blogging engines for .NET, but I think I will do it from scratch anyway.
   I wonder how soon I will have something usable...