Thursday, June 30, 2011

WCF Service Routing Resources

Service routing is used to expose an end-point to consumers that appears as though it is the real service, but actually is routing requests elsewhere. Reasons to do this include: version based routing (older versions go to a different service), or application level load balancing, or protocol bridging, or dynamic message content based routing etc.

Here are some resources in relation to WCF Service routing:

Wednesday, June 29, 2011

WCF Performance Comparison

A Performance Comparison of Windows Communication Foundation (WCF) with Existing Distributed Communication Technologies

WCF Network Load Balancing Resources

Useful Pages I have found while researching this topic:
Most information refers to the use of external software (mostly Microsoft NLB - part of 2008 Server), or hardware appliances that preface service interfaces.
There seems to be three options:
  1. Utilise external hardware or software NLB'ers. (This is noted by Microsoft as the best practise approach).
  2. Manually partition your clients so that they hard wired to point to one of service instances. This approach doesn't give good balance but its straight forward in some simple instances.
  3. Hand craft discovery and balancing code inside your application. This gives the widest fail over options and balancing flexibility. The significant downsides are the workload to develop, and runtime hosting training and maintenance.

    Tuesday, June 28, 2011

    WCF Extensibility Resources

    A great article in a old MSDN magazine explaining the most common extensibility points in WCF.

    Extending WCF with custom behaviors

    Overview:
    There are essentially 5 common points of extensibility in a WCF service (and the corresponding interface to use):
    1. Message inspection (IDispatchMessageInspector)
    2. Operation selector (IDispatchOperationSelector)
    3. Message formatter (deserialisation) (IDispatchMessageFormatter)
    4. Parameter Inspection (IParameterInspector)
    5. Operation Invoker (IOperationInvoker)
    These extender classes are injected into WCF using Behaviors. Options are:
    Ensure any implementation of these interfaces also inherits Attribute to allow direct decoration of a contract/operation within code.

    Wiring is best done with config or attribute use:
    <behaviors>
    <serviceBehaviors>
    <behavior name="Default">
    <serviceMetadata httpGetEnabled="true"/>
    <consoleMessageTracing/>
    </behavior>
    </serviceBehaviors>
    </behaviors>
    [ServiceContract]
    public interface IZipCodeService
    {
    [ZipCodeCaching]
    [ZipCodeValidation]
    [OperationContract]
    string Lookup(string zipcode);
    }

    Friday, June 24, 2011

    Windows Process Activation Services (WAS) Reference Resources

    WAS is a new process activation model for IIS7.  It enables you to host any WCF service using other non-HTTP protocols using IIS's robust and reliable model.

    This diagram from MSDN helps to shows the different components and their boundaries:


    Monday, June 20, 2011

    AppFabric Cache Resources

    AppFabric Cache is a caching mechanism formerly known as Velocity that has been merged into Windows Server.  There is also a Azure flavour of it as well, but this resource list focuses on Windows Server.

    Monday, June 13, 2011

    XML Validation

    Its worth documenting this somewhere even though its pretty straight forward.

    namespace XmlValidator
    {
        using System;
        using System.IO;
        using System.Linq;
        using System.Xml;
        using System.Xml.Linq;
        using System.Xml.Schema;
    
        public class EasyXmlValidator
        {
            private Action<string> output;
    
            public EasyXmlValidator(Action<string> outputWriter)
            {
                this.output = outputWriter;
            }
    
            public bool Verify(string[] schemaFilenames, string xml)
            {
                var schemaSet = new XmlSchemaSet();
                foreach (string schema in schemaFilenames)
                {
                    string content = File.ReadAllText(schema);
                    XDocument document = XDocument.Parse(content);
                    var namespaceElement = document.Root.Attributes("targetNamespace").FirstOrDefault();
                    var name = namespaceElement == null ? string.Empty : namespaceElement.Value;
                    schemaSet.Add(name, XmlReader.Create(new StringReader(content)));
                }
    
                XDocument doc1 = XDocument.Parse(xml);
    
                Console.WriteLine("Validating doc1");
                bool errors = false;
                doc1.Validate(schemaSet, (o, e) =>
                {
                    this.output(string.Format("{0}", e.Message));
                    errors = true;
                });
    
                this.output(string.Format("doc1 {0}", errors ? "did not validate" : "validated"));
                return !errors;
            }
    
            public bool Verify(string schema, string xml)
            {
                return Verify(new[] { schema }, xml);
            }
        }
    }

    Tuesday, June 7, 2011

    DTOs vs Unified Object Models

    Great blog post on Info Q discussing differing opinions on the Data Transfer Object pattern and unified object models (used for ORM, business logic, as wire transfer objects, and as Models in Model-View-Controller pattern).

    http://www.infoq.com/news/2011/06/DTOs-vs-Objects

    In regard to unified models, I have always felt this was forcing to many concerns on a class. In my opinion at least it breaks the single responsibility principle.

    Friday, June 3, 2011

    Wcf Wsdl Generation and Including Schemas

    For some reason, when you browse to the WSDL for a ServiceContract you do not get included Schema in the WSDL Types.

    For example: Here's a basic demo Wcf Service.
     Notice the link to view the actual WSDL file for this service.

    This is what the WSDL file looks like :
     Notice how the <wsdl:Types /> element is empty.  Ideally a WSDL document should be the one stop shop for describing the service.

    To view the schemas of the data contracts and other types used by the service you need to add other parameters to the URL as follows:




    Xsd0 shows the service actions, the methods and their parameters.

    Xsd1 shows the standard scalar types used, ie strings, arrays etc.

    Xsd2 shows the custom data contract types defined and used in the service.

    Xsd3 (not shown above) will show any enumeration definitions that you have included in operation contracts.

    All these schemas can be copied into the <wsdl:types /> element or imported into the element as follows:

       <wsdl:types>

    <xsd:schema targetNamespace="http://wcf.rees.biz/CalculatorDemo/2011/06/Imports">
          <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd0" namespace="http://wcf.rees.biz/CalculatorDemo/2011/06" />
          <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
          <xsd:import schemaLocation="http://localhost:55401/CalculatorService.svc?xsd=xsd2" namespace="http://wcf.rees.biz/CalculatorDemo/2011/06/" />
        </xsd:schema>
      </wsdl:types>




    As an alternative to using a browser to explore the WSDL for  a service you can use the SVCUTIL to extract the WSDL and XSD and save them locally.

    SVCUTIL /t:metadata http://url_to_service_here.

    This will extract the WSDL and all related XSD files onto your local disk in the current folder.

    As a courtesy to any third party you are engaging with and co-developing a service always ensure you give them schema's in the WSDL.