Monthly Archives: October 2010

Receiving unsecured response with WCF – Revisted

A while back I blogged about how to deal with unsecured responses in WCF (see this post). I’ve had several people ask me for the code that you can use in case you can’t use the mentioned hotfix. Attached is the code for a message encoder that intercepts the MessageSecurityException thrown by the original encoder. The encoder wraps around the actual encoder you want to use, so you’ll have to configure it like this:

<system.serviceModel>
  <extensions>
    <bindingElementExtensions>
      <add name="unsecureResponseEncoding"
           type="UnsecureResponseEncoder.InterceptingMessageEncodingElement, UnsecureResponseEncoder,
                 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </bindingElementExtensions>
  </extensions>
  <bindings>
    <customBinding>
      <unsecureResponseEncoding>
        <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                             messageVersion="Soap11WSAddressing10">
          <readerQuotas maxDepth="32" maxStringContentLength="8192"
                        maxArrayLength="16384" maxBytesPerRead="4096"
                        maxNameTableCharCount="16384" />
        </textMessageEncoding>
      </unsecureResponseEncoding>
    <!-- removed for briefity -->
    </customBinding>
  </bindings>
  <client>
    <!-- removed for briefity -->
  </client>
  <behaviors>
    <!-- removed for briefity -->
  </behaviors>
</system.serviceModel>

The main thing the UnsecureResponseEncoder does is override the ReadMessage methods of the message encoder. All else is just plumbing to wrap the original encoder and use it as is. In ReadMessage the received message is retrieved and place in a local variable. If a MessageSecurityException occurs, this is caught, and an UnecureResponseException is thrown, which includes the original message. Higher up in the call chain you can parse the original message and extract the fault information. The example below shows an altered proxy call so the proxy actually returns a FaultException<>.

SomeResponseMessageContract ISomeService.SomeServiceMethod(SomeRequestMessageContract request)
{
    try
    {
        return base.Channel.Aanleveren(request);
    }
    catch (UnsecureResponseException exception)
    {
        if (String.IsNullOrEmpty(exception.ResponseMessage) == false)
        {
            // Put original response message in XmlDocument so you can manipulate it
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(exception.ResponseMessage);

            // Add namespaces concerning faults, including your custom fault schema
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
            nsmgr.AddNamespace(“soapenv”, “http://schemas.xmlsoap.org/soap/envelope/”);
            nsmgr.AddNamespace(“fault”, “http://somefaultschema/”);

            // Retrieve the fault node and extract information.
            XmlNode faultNode = xml.SelectSingleNode(“/soapenv:Envelope/soapenv:Body/soapenv:Fault”, nsmgr);
            if(faultNode != null)
            {
                SomeFault fault = new SomeFault()
                {
                    SomeFaultInfo = faultNode.GetNodeStringValue(“detail/fault:SomeFault/fault:SomeFaultInfo”, nsmgr)
                };
                if(String.IsNullOrEmpty(fault.SomeFaultInfo) == false)
                {
                    throw new FaultException(fault, exception.Message);
                }
            }
        }
        throw; // If not properly handled, rethrow original exception
    }
}

Competence Mapping with Visual Studio Ultimate

I’ve been trying to find a way in which I can easily map competences, books, certifications etc. The goal here is to be able to determine what you need to do to reach a certain competence or certification. Now, there may be some fancy tool out there to do this, or you could just whip up Visio and start drawing, but a) I don’t want to buy a new tool, and b) I want this to be interactive.

I just finished a demo that uses the Dependency Graph functionality in Visual Studio Ultimate. I’ve used similar tooling before (from NDepend) for auditing and reviewing, but this time around I’m using it as part of my solution rather than to review a solution. I’ve expressed compentencies and books in classes, and if a competence is required for another, I’m adding a reference to it in a prerequisites list. The same happens with books you could read to learn about the competence.

After letting Dependency Graphing do its thing, you end up with a graph such as the one below. The great thing is that it’s interactive. You can delete/colapse in the diagram what you don’t need, and you can select the items for which you want to see more detailed dependencies. In the graph below, I’ve selected several constructors to show me all the dependencies.

With everything in code, I could now create a user interface that enables you to query the map for what you still need to do to get to the competence/certification you want.