Frank's profileFrank de GrootBlogLists Tools Help

Blog


    January 29

    Using F#'s async workflows to invoke a WCF service

    In addition to Ted Neward's posts on creating a WCF service in F# I'd like to share a howto on calling a WCF service asynchronously from F# using the infamous async workflows.
     
    I'll start off with a simple data contract. This is not required, you could use only primitive type parameters, but I like data contracts. The easiest way is to create a record type. Unfortunately the serializer requires mutable fields, but I can live with that. Please note I'm skipping the open System.Runtime.Serialization and open System.ServiceModel.
     

    [<DataContract>]
    type public MyRequest = {
        [<DataMember>]
        mutable MyParameter:string;
    }

    [<DataContract>]

    type public MyResult = {
        [<DataMember>]
        mutable MyResultParameter:string;
    }

    Then I'll define an interface for my service in F#:

    [<ServiceContract>]

    type public IMyService =
        [<OperationContract>]
        abstract MyOperation: Request:MyRequest -> MyResult

    Now to invoke a service asynchronously I need to create a new interface with a slightly different signature. For WCF this new interface is equivalent to the one above, except that it allows asynchronous invocation. Because they need to have the same name I put them in a different assembly so I have one assembly for the contract at the server side and another assembly for the client side. We can reuse the data contract though.

    [<ServiceContract>]

    type public IMyService =
        [<OperationContract>]
        abstract MyOperation: Request:MyRequest -> MyResult
        [<OperationContract(AsyncPattern =
    true)>]
        abstract BeginMyOperation: Request:MyRequest * AsyncCallback * obj -> IAsyncResult
        abstract EndMyOperation: IAsyncResult -> MyResult

    Notice the Begin... and End... methods. By adding the AsyncPattern = true to the OperationContract WCF assumes that it's the asynchronous version of MyOperation. As you can see the signature is slightly different: BeginMyOperation expects an additional callback delegate and an optional state object and returns a 'handle' that is passed to EndMyOperation to get the result of invoking MyOperation.

    Note that here too I need to supply a name for the first parameter, in this case 'Request'. As Ted described WCF requires a name for the parameters specified in the interface definition. This is common in C# and VB so that's probably the reason why it wasn't noticed by the WCF team. Luckily it's easy to add in F#.

    The above code is enough to invoke a service asynchronously. But if we want to use it in async workflows we require another method named AsyncMyOperation. I decided to create an extension method that adds this method to the interface we just created:

    module

    public Async =
        type IMyService with
        member s.AsyncMyOperation(request) =
            Async.BuildPrimitive(request, s.BeginMyOperation, s.EndMyOperation)

    Essentially I'm creating a new method named AsyncMyOperation by calling Async.BuildPrimitive. Async.BuildPrimitive requires the Begin... and End... methods that are common in the .NET Framework for asynchronous operations. It then creates an Async<'a> that we can use in an async workflow. UPDATE: I previously created a local function to capture the request and handed that to Async.BuildPrimitive but I just noticed the overloads of BuildPrimitive that take additional arguments. You can supply up to three arguments. When you need to pass more (unlikely I'd say) you can always create a local function to capture additional arguments.

    Mind you, the module name I'm using in the code above really doesn't matter as long as you add an open Async where you want to use it.

    Right, now all we need to do is to create the async workflow to actually invoke the service:

    let CallMyService =
        async {
            let request = { MyParameter = "Test" }
            use factory = new ChannelFactory<IMyService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:12345/MyService.svc"))
            let channel = factory.CreateChannel()
           
    let! result = channel.AsyncMyOperation(request)
            printf "%A" result.MyResultParameter
       
    }

    Please notice the exclamation mark in let! result = ... If you add a Debug.WriteLine(Thread.CurrentThread.ManagedThreadId.ToString()) before and after the call you'll see that the code below the service call runs on a different thread than the lines above it.

    Then, finally, you can run your async workflow with Async.Spawn CallMyService. You can use Async.Run but it would still block to wait for the result so that's probably not what you want.

    Well, please let me know if this code works for you or not or any comments and questions you might have.

    January 04

    Combining WCF and AzMan authorization using EntLib

    The code below is a class that authorizes a WCF web service call using EntLib's AzMan authorization provider:

    public sealed class AzManAuthorizationManager : ServiceAuthorizationManager
    {
        private const string OperationContextPrefix = "O:";

        protected override bool CheckAccessCore(OperationContext operationContext)
        {
            try
            {
                // Obtain the requested action from the context.
                string action = operationContext.RequestContext.RequestMessage.Headers.Action;
                // Remove the namespace part from the action.
                action.Substring(action.LastIndexOf('/') + 1);

                // Get the windows claim set that contains the windows identity
                // and use that to authorize.
                foreach (ClaimSet claimSet in operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets)
                {
                    WindowsClaimSet windowsClaimSet = claimSet as WindowsClaimSet;
                    if (windowsClaimSet != null)
                    {
                        WindowsPrincipal principal = new WindowsPrincipal(windowsClaimSet.WindowsIdentity);
                        IAuthorizationProvider provider = AuthorizationFactory.GetAuthorizationProvider();
                        return provider.Authorize(principal, OperationContextPrefix + action);
                    }
                }
            }
            catch (Exception exception)
            {
                if (ExceptionPolicy.HandleException(exception, "Exception in Security"))
                {
                    throw;
                }
            }
            return false;
        }
    }

    Notes:

    • This won't work with basicHttpBinding, you'll at least have to use wsHttpBinding.
    • I simply remove the namespace portion of the SOAP action. This is because AzMan doesn't allow characters like : and / in an operation name.
    • When calling Authorize prefix operations with "O:". Without prefix it will instead try to find a task of the supplied name.
    • EntLib's WCF Exception Shielding won't work here, any exception is converted to a SecurityAccessDeniedException so you probably want a specific exception policy that also logs the actual exception (which is what I did).

    I hope you find this useful, if not let me know!

    November 15

    Combining Validation and Exception with WCF and EntLib integration

    Both WCF/EntLib integration for Validation and Exception handling are cool, but combining them is a bit tricky.

    EntLib's Exception handling allows you to send a generic SOAP fault to a client if any Exception occurs within your service. This allows you to hide exception details from the client. Unfortunately a ValidationFault also gets translated to this generic service fault.

    The solution: in the EntLib configuration, under your exception policy for your WCF service (usually named "WCF Exception Shielding" add an entry for the System.ServiceModel.FaultException and set it to NotifyRethrow besides catching System.Exception and throwing a custom SOAP fault in its place. The specific exception to rethrow would be FaultException<ValidationFault> but I'm not sure how to configure EntLib for this.

    Fortunately the order is not important: the EntLib configuration tool orders the added exception types alphabetically so it automatically puts the FaultException after the generic Exception handler, but it works just the same.

    November 09

    WCF, EntLib Exception Application Block and SecurityException

    Today I tried to handle a SecurityException through EntLib's Exception Application Block. I though it would be useful to rethrow it as a custom type name SecurityFault. Silly me, apparently a SecurityException gets translated to a System.ServiceModel.Security.SecurityAccessDeniedException.

    Lessen: don't try this at home (or at work for that matter)...

    November 08

    WCF & EntLib's Validation Application Block MessageContract Caveat

    I've seen some blog entries of people having trouble using VAB in WCF when a web service has a message contract. Apparently any contained DataContracts aren't validated.

    After some fidgeting I found the problem: apparently the validation starts at the MessageContract. Just decorate the message body property (decorated with [MessageBody]) with [ObjectValidator].

    The ObjectValidator is a validator that allows you to validate properties of a class containing properties decorated with validators. In other words; it allows validation of subtypes.

    October 27

    Virtual Machine steps

    Here's my list of things I do when I create a new virtual machine. Or at least I should do but I generally forget to do at least one of them:
     
    * Install CCleaner to remove log files and other junk.
    * Disable the annoying beep: net stop beep, sc config beep start= disabled
    * Disable shutdown event tracker: gpedit.msc / Computer COnfiguration / Administrative Templates / System / Display Shutdown Event Tracker / Disabled
    * Enable Shutdown without logon: Administrative Tools / Local Security Policy / Local Policies / Security Options / Shutdown: Allow system to be shut down without having to log on / Enabled.
    * Install TweakUI and enable Logon / Autologon
    * Disable screen saver
    * Install Bginfo and show the computer name on the desktop.
     
    There! Now I should stop forgetting these steps.
     
    June 03

    Microsoft Popfly

    Yay! I've been invited to Microsoft Popfly! Popfly is an experimental site allowing you to build web sites and mash-ups. Especially the way mash-ups can be created interactively is quite stunning.
     
    Unfortunately my web site building skills have been in the fridge since some time. At least I have a good sample case, redesigning our percussion band's web site.
    May 14

    EntLib 3.0 Hand-on labs

    The EntLib 3.0 Hand-on labs haven't been updated for the april 2007 release. You may have to delete and add the references and update the App.config by replacing "2.0.0.0" with "3.0.0.0" and "PublicKeyToken=null" with "PublicKeyToken=b03f5f7f11d50a3a". Other than that, they're a handy intro. I finally have the chance to try these things out since the separate application block era.
    April 29

    UI Smell

    Just like code smell I recently thought of the term UI smell. Apparently the term is already sort of used but not very much.
     
    The system I'm currently participating on has a few:
    • System message "Please press button ...": If the system already knows what button should be pressed, why ask me to do it?
    • System message "Would you like to save the changes?" - "Yes" - "Can't save changes because of ...": Why don't you check if you can save before you ask me?
    • System message "Message sent." - "Can't send message because of ...": Why tell me you performed the action while in fact you haven't already?
    • And the best: a form that can open in 9 different modes which make the form act subtly different while there are no visual clues telling the user what mode the form is in.

    I really feel sorry for the future users.

    .NET 2.0 Stream Compression & Encryption

    A while ago I created a little application to store my passwords compressed and encrypted using XML serialization. I created it in .NET 1.1 so I could use it a my customer's site (poor me). It's compressed and encrypted XML in an XML container so I can use XmlSerializer all the way.
    I handcrafted the streaming of the encryption and compression which was fun but took some time.
    In .NET 2.0 this should be easier with CryptoStream and DeflateStream. The only tricky bit is how to get the compressed & encrypted data into and out of a MemoryStream properly. If you don't close the Crypto & Deflate stream they don't finalize their output properly and if you do close them they close the underlying MemoryStream. There's a way around this but it differs between the CryptoStream and DeflateStream. The sample code below shows how to do this (and these aren't my real passwords  of course):
     
    The Save method required some tweaking, in short it does the following:
    • create a MemoryStream and a CryptoStream,
    • create a DeflateStream with leaveOpen set to true in the constructor, serialize the secret information into it and close it,
    • call CryptoStream.FlushFinalBlock(),
    • reset the MemoryStream to the beginning,
    • create the containing object (Vault) and put in the contents of the MemoryStream,
    • serialize the containing object to a file.

    So, the CryptoStream has an extra method FlushFinalBlock() to tell the stream to finish its work. On the other hand DeflateStream is instructed to leave the underlying stream open. If the DeflateStream is then closed it will also finish its work.

    namespace Spikes
    {
        public class Credential
        {
            [XmlAttribute]
            public string Site;
            [XmlAttribute]
            public string User;
            [XmlAttribute]
            public string Password;
        }

       
    public class Secrets
        {
            public Credential[] Credentials;
        }

       
    public class Vault
        {
            [XmlAttribute]
            public int Hashes;
            [XmlAttribute]
            public byte[] IV;
            [XmlText]
            public byte[] Data;
        }

       
    class Program
        {
            static void Main(string[] args)
            {
                Credential credential1 = new Credential();
                credential1.Site = "Info Support";
                credential1.User = "frankg";
                credential1.Password = "bl@h'1";
                Credential credential2 = new Credential();
                credential2.Site = "Live Mail";
                credential2.User = "fjtdg(at)hotmail.com";
                credential2.Password = "Y0!";
                Secrets secrets = new Secrets();
                secrets.Credentials = new Credential[] { credential1, credential2, };
                RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
                byte[] iv = new byte[16];
                rng.GetBytes(iv);
                const string path = @"C:\Users\Frank\Documents\vault.xml";
                const string password = @"P@$$\/\/w0rd";
                const int hashes = 16; //1048576;
                Save(secrets, path, password, hashes, iv);
                secrets = Open(path, password);
                Console.WriteLine(secrets.Credentials.Length);
            }

            static void Save(Secrets secrets, string path, string password, int hashes, byte[] iv)
            {
                using (MemoryStream dataStream = new MemoryStream())
                {
                    Rijndael rijndael = Rijndael.Create();
                    byte[] key = Key(password, hashes);
                    using (CryptoStream cryptoStream = new CryptoStream(dataStream, rijndael.CreateEncryptor(key, iv),
    CryptoStreamMode
    .Write))
                    {
                        using (Stream zipStream = new DeflateStream(cryptoStream, CompressionMode.Compress, true))
                        {
                            XmlSerializer secretsSerializer = new XmlSerializer(typeof(Secrets));
                            secretsSerializer.Serialize(zipStream, secrets);
                        }
                        cryptoStream.FlushFinalBlock();
                        dataStream.Position = 0;
                        Vault vault = new Vault();
                        vault.Hashes = hashes;
                        vault.IV = iv;
                        byte[] data = new byte[dataStream.Length];
                        dataStream.Read(data, 0, data.Length);
                        vault.Data = data;
                        using (FileStream vaultStream = File.Open(path, FileMode.Create))
                        {
                            XmlSerializer vaultSerializer = new XmlSerializer(typeof(Vault));
                            vaultSerializer.Serialize(vaultStream, vault);
                        }
                    }
                }
            }

           
    static Secrets Open(string path, string password)
            {
                using (FileStream vaultStream = File.Open(path, FileMode.Open))
                {
                    XmlSerializer vaultSerializer = new XmlSerializer(typeof(Vault));
                    Vault vault = (Vault)vaultSerializer.Deserialize(vaultStream);
                    byte[] key = Key(password, vault.Hashes);
                    using (MemoryStream dataStream = new MemoryStream())
                    {
                        dataStream.Write(vault.Data, 0, vault.Data.Length);
                        dataStream.Flush();
                        dataStream.Position = 0;
                        Rijndael rijndael = Rijndael.Create();
                        using (CryptoStream cryptoStream = new CryptoStream(dataStream, rijndael.CreateDecryptor(key,
    vault.IV),
    CryptoStreamMode.Read))
                        {
                            using (Stream zipStream = new DeflateStream(cryptoStream, CompressionMode.Decompress))
                            {
                                XmlSerializer secretsSerializer = new XmlSerializer(typeof(Secrets));
                                return (Secrets)secretsSerializer.Deserialize(zipStream);
                            }
                        }
                    }
                }
            }

           
    static byte[] Key(string password, int hashes)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(password);
                HashAlgorithm hasher = new SHA256Managed();
                for (int i = 0; i < hashes; i++)
                {
                    bytes = hasher.ComputeHash(bytes);
                }
                return bytes;
            }
        }
    }

    January 10

    Funky Little Laptop

    The organization OLPC (One Laptop Per Child) plans to give special laptops to children. Later on they plan to sell them in developed countries where you'd have to buy two and one will go to a child, as explained in this news article. The idea is you get a chance to stay in touch with the kid for whom your buying the laptop. I'd be almost jealous, seeing what a little funky laptops these kids get.
     
    Well, connect me to a kid in Latin America! A good chance to practise my spanish...
    December 29

    My smart girlfriend

    With the advent of Vista and the urge to install Ubuntu I decided to buy another hard disk. I couldn't resist buying another 150 Gb Raptor, of course totally overkill but that's not the point I wanted to make...
     
    As I was trying figure out how to get the hard disk into the bay, my girlfriend walked in, stared at it blankly for a few seconds and then said "Can't you take out the whole bay?". When I looked more closely, it turned you she was entirely right!
     
    Looks like she saved me some serious time trying to get the motherboard out of the way. Go girl!
    December 26

    Finding your tools

    I finally found a tool to download big files from my employer's intranet through the VPN. The VPN connection gets killed after some time, so it must be restartable. Today I found Robocopy included in the Windows Server 2003 Resource Kit Tools.
     
    Then I used 7-Zip to scoop out robocopy.exe from the self-extracting .exe and the contained .msi. So no need to install anything.
     
    Thank you Microsoft and thank you 7-Zip!
    December 24

    NBear

    Browsing CodePlex I found NBear, an ORM framework for .NET 2.0. Interesting? Well, I'll be damned if I know: apparently it's a chinese product; all the documentation and web sites are in chinese! Needless to say that diminishes my interest in the product somewhat...
    December 22

    Human Methodologies 2

    In another not-so-recent ARCast Lewis Curtis and Brian Derda explain their new methodology Perspective Based Architecture. Now, I'm not an architect and I don't have any experience with this methodology but I found it an appealing methodology because:
    • The basic idea is indeed very basic (what can be more basic than asking questions?),
    • The architects they intend to force people out of their comfort zone (especially useful to technical specialists),
    • and the questions also encourage people to collaborate (instead of just throwing documents over the wall).
    • They also emphasized that a strict timebox encourages people to prioritize the questions,
    • and be honest when they just don't know the answer yet.

    Again a methodology with a strong social component. In my opinion methods like these have a promising future.

    December 21

    Human Methodologies 1

    Just recently I read the following paragraph in the book "Agile Project Management with Scrum" by Ken Schwaber.
     
    I used to teach people the theory, practices, and rules of Scrum. Now I teach them what Scrum feels like as it is implemented. I teach them how to recognize when things are going right and when they are going wrong. I provide exercises and discussions that let them experience the epiphanies so that they know what Scrum should feel like. Just as you don’t really know what it’s like to be someone else until you’ve walked however many miles in his or her shoes, you might not fully understand Scrum until you implement it yourself. But as you read this book, you will begin to understand what Scrum feels like and how you might feel using Scrum in your organization.
     
    How a project management methodology feels? That's what I like about the recent progress in software development, things like XP and Scrum I find more straightforward, pragmatic and most of all, more human than previous methodologies.
     
    This is why I'm enjoying software development more than ever, power to the people!

    Open XML in Office 2007 produces smaller files!

    In a not-so-recent ARCast I heard that the new Open XML format for Office documents actually produces smaller files. After downloading the update for Office 2003 to create Open XML files I was surprised to see that this is actually true! A few tries produced files 5x smaller!
     
    Amazing, an open format, much more accessible (for developers that is) format and smaller files too!
    December 20

    Find & replace special characters in Word

    I just pasted syntac coloured C# code from a Word document into the editor for this blog. The result was an empty line between each line of code, caused by a Paragraph tag. This in turn was caused by the Paragraph Mark which should be replaced by a Manual End of Line Mark (Shift-Enter).

     

    To replace the Paragraph Mark with a Manual End of Line Mark in Word I tried to use find-and-replace ^v to ^| but Word couldn't find the Paragraph Marks, even though the ^v is inserted by a menu option on the same dialog (More / Special / Paragraph Character).

     

    After googling I found this page explaining I should use ^13 instead of ^v. Odd, but it works!

    Easier .NET Service installation 2

    Also, removing a .NET Windows Service that is running under an account other than Local System/Local Service lingers in the list of services until a restart. Adding the snippet below in the project installer will reset the account to local system just before the actual uninstall. This neatly removes the service from the list.

    /// <summary>
    ///
    Overriden to set the user account to local service.
    /// If set to custom user on uninstall the service entry lingers until a reboot,
    /// when set to local service the entry is removed immediately.
    /// </summary>
    ///
    <param name="savedState">Only passed to base class.</param>
    protected override void OnBeforeUninstall(IDictionary savedState)
    {
          
    this.serviceProcessInstaller.Account = ServiceAccount.LocalService;
          
    base.OnBeforeUninstall(savedState);
    }