<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.dolittle.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Blog&amp;#39;A&amp;#39;Little : C#, WCF</title><link>http://www.dolittle.com/blogs/einar/archive/tags/C_2300_/WCF/default.aspx</link><description>Tags: C#, WCF</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>Programatically creating a WCF contract and exposing it - Part1</title><link>http://www.dolittle.com/blogs/einar/archive/2008/02/01/programatically-creating-a-wcf-contract-and-exposing-it-part1.aspx</link><pubDate>Fri, 01 Feb 2008 18:50:18 GMT</pubDate><guid isPermaLink="false">a744be0d-d88a-46a6-b249-55961ed4a125:1233</guid><dc:creator>Einar Ingebrigtsen</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I&amp;#39;ve been working lately a bit with WCF and find it very exciting and a true step up from .net remoting and .net WebServices.&lt;br /&gt;The scenario we&amp;#39;re having is that we have one server application and at the moment two front-ends; Windows Forms and a ASP.net Web application. For our Windows Forms based application we need to be able to host our services in-process as well as on a server. In addition we do not want to be directly coupled to WCF for our contracts, so the declarative way of doing things with attributes (ServiceContractAttribute, OperationContractAttribute and so forth) is not acceptable, or at least not the optimal solution for our services to be exposed. Another issue I ran into was the lack of surrogate support for well known types such as XmlNode and XmlDocument and others. &lt;/p&gt; &lt;p&gt;With this in mind I&amp;#39;ve been working on a prototype were we share an assembly with all our contracts and then have our own ServiceModel component that will also be shared between the projects. This will then serve as our entrypoint for our services. When we have this principal with a shared assembly with all our interfaces, it is fair to say that the contracts and it&amp;#39;s operations are implicitly exposed and available, in other words; attribute decorations should really not be necessary.&lt;/p&gt; &lt;p&gt;The picture looks something like this (finally I got to use my tablet for blogging. :) )&amp;nbsp; : &lt;/p&gt; &lt;div class="wlWriterSmartContent" id="scid:31C7882A-CF45-4fcc-A614-7A5A52E598FF:cec75b15-f47a-4f31-b00a-e2453d8638a2" style="padding-right:0px;display:inline;padding-left:0px;float:none;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;&lt;p&gt;&lt;img src="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/CreatinganabstractionfromWCF_1015E/Ink282504208623.png" title="Ink Generated with Ink Blog Plugin - http://www.edholloway.com" alt="" /&gt; &lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;In order to get this to work one need to have the ability to create service contracts programatically and be able to serialize and deserialize datacontracts without having to decorate them with different contract attributes or create surrogates. Fortunately, WCF has been made to be as flexible as possible and give 100% control to the developer, so it is pretty straight forward to do this.&lt;/p&gt; &lt;p&gt;First we start by setting up our host : &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHost&lt;/span&gt;(serviceType, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.BaseAddress);
&lt;span style="color:#2b91af;"&gt;WSHttpBinding &lt;/span&gt;binding = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;WSHttpBinding&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;SecurityMode&lt;/span&gt;.None);
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;We need to add the ServiceMetaDataBehavior and take advantage of a method on it called ExportContract(). This method will &lt;br /&gt;export the contract for the service to the endpoint.&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;ServiceMetadataBehavior &lt;/span&gt;smb = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceMetadataBehavior&lt;/span&gt;();
smb.HttpGetEnabled = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;
smb.MetadataExporter.ExportContract(contractDescription);
host.Description.Behaviors.Add(smb);
&lt;/pre&gt;
&lt;p&gt;And then we add our endpoint : &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;ServiceEndpoint &lt;/span&gt;endPoint = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceEndpoint&lt;/span&gt;(
    contractDescription, 
    binding, 
    &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EndpointAddress&lt;/span&gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.BaseAddress));

host.Description.Endpoints.Add(endPoint);
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Then we open our host and make it start listening for requests - note : this implementation pretty much turns off security, probably &lt;br /&gt;something one should look into. :)&lt;/p&gt;&lt;pre class="code"&gt;host.Authorization.PrincipalPermissionMode = &lt;span style="color:#2b91af;"&gt;PrincipalPermissionMode&lt;/span&gt;.None;
host.Open();&lt;/pre&gt;
&lt;p&gt;As you&amp;#39;ve probably noticed, we need code to generate the contract description needed for the above implementation. A quick and dirty implementation should look like this : &lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHoster
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public static readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHoster &lt;/span&gt;Instance = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHoster&lt;/span&gt;();
    &lt;span style="color:blue;"&gt;public static readonly string &lt;/span&gt;DefaultNamespace = &lt;span style="color:#a31515;"&gt;&amp;quot;http://www.dolittle.com/&amp;quot;&lt;/span&gt;;

    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;System.ServiceModel.&lt;span style="color:#2b91af;"&gt;ServiceHost&lt;/span&gt;&amp;gt; _hosts;

    &lt;span style="color:blue;"&gt;private &lt;/span&gt;ServiceHoster()
    {
        &lt;span style="color:blue;"&gt;this&lt;/span&gt;._hosts = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;List&lt;/span&gt;&amp;lt;System.ServiceModel.&lt;span style="color:#2b91af;"&gt;ServiceHost&lt;/span&gt;&amp;gt;();
        &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace = DefaultNamespace;
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ShutDown()
    {
        &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ServiceHost &lt;/span&gt;host &lt;span style="color:blue;"&gt;in this&lt;/span&gt;._hosts)
        {
            host.Close();
        }
    }

    &lt;span style="color:blue;"&gt;public string &lt;/span&gt;Namespace { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;Uri &lt;/span&gt;BaseAddress { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }

    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ContractDescription &lt;/span&gt;CreateContractDescription(&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;contractType, &lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color:#2b91af;"&gt;ContractDescription &lt;/span&gt;contractDescription = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ContractDescription&lt;/span&gt;(contractType.Name);
        contractDescription.ProtectionLevel = &lt;span style="color:#2b91af;"&gt;ProtectionLevel&lt;/span&gt;.None;
        contractDescription.ContractType = contractType;
        contractDescription.ConfigurationName = contractType.FullName;
        contractDescription.SessionMode = &lt;span style="color:#2b91af;"&gt;SessionMode&lt;/span&gt;.NotAllowed;

        &lt;span style="color:#2b91af;"&gt;MethodInfo&lt;/span&gt;[] methods = contractType.GetMethods();

        &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;MethodInfo &lt;/span&gt;method &lt;span style="color:blue;"&gt;in &lt;/span&gt;methods)
        {
            &lt;span style="color:#2b91af;"&gt;OperationDescription &lt;/span&gt;operationDescription = 
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OperationDescription&lt;/span&gt;(method.Name, contractDescription);

            &lt;span style="color:blue;"&gt;string &lt;/span&gt;messagePath = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace + contractType.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;/&amp;quot;&lt;/span&gt;;
            &lt;span style="color:blue;"&gt;string &lt;/span&gt;requestName = method.Name;
            &lt;span style="color:blue;"&gt;string &lt;/span&gt;responseName = method.Name + &lt;span style="color:#a31515;"&gt;&amp;quot;Response&amp;quot;&lt;/span&gt;;

            &lt;span style="color:blue;"&gt;string &lt;/span&gt;requestPath = messagePath + requestName;
            &lt;span style="color:blue;"&gt;string &lt;/span&gt;responsePath = messagePath + responseName + &lt;span style="color:#a31515;"&gt;&amp;quot;Response&amp;quot;&lt;/span&gt;;
            &lt;span style="color:blue;"&gt;string &lt;/span&gt;resultName = requestName + &lt;span style="color:#a31515;"&gt;&amp;quot;Result&amp;quot;&lt;/span&gt;;

            &lt;span style="color:#2b91af;"&gt;MessageDescription &lt;/span&gt;inputMessage = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MessageDescription&lt;/span&gt;(requestPath, &lt;span style="color:#2b91af;"&gt;MessageDirection&lt;/span&gt;.Input);
            inputMessage.Body.WrapperName = requestName;
            inputMessage.Body.WrapperNamespace = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace;

            &lt;span style="color:#2b91af;"&gt;ParameterInfo&lt;/span&gt;[] parameters = method.GetParameters();
            &lt;span style="color:blue;"&gt;foreach &lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;ParameterInfo &lt;/span&gt;parameter &lt;span style="color:blue;"&gt;in &lt;/span&gt;parameters)
            {
                &lt;span style="color:#2b91af;"&gt;MessagePartDescription &lt;/span&gt;messagePartDescription = 
                    &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MessagePartDescription&lt;/span&gt;(parameter.Name, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace);
                messagePartDescription.Type = parameter.ParameterType;
                inputMessage.Body.Parts.Add(messagePartDescription);
            }
            operationDescription.Messages.Add(inputMessage);

            &lt;span style="color:blue;"&gt;if &lt;/span&gt;(&lt;span style="color:blue;"&gt;null &lt;/span&gt;!= method.ReturnType)
            {
                &lt;span style="color:#2b91af;"&gt;MessageDescription &lt;/span&gt;outputMessage = 
                    &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MessageDescription&lt;/span&gt;(responsePath, &lt;span style="color:#2b91af;"&gt;MessageDirection&lt;/span&gt;.Output);
                outputMessage.Body.ReturnValue = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MessagePartDescription&lt;/span&gt;(resultName, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace);
                outputMessage.Body.ReturnValue.Type = method.ReturnType;
                outputMessage.Body.WrapperName = responseName;
                outputMessage.Body.WrapperNamespace = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.Namespace;
                operationDescription.Messages.Add(outputMessage);
            }

            contractDescription.Operations.Add(operationDescription);

            operationDescription.Behaviors.Add(&lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceOperationBehavior&lt;/span&gt;(method));


            &lt;span style="color:#2b91af;"&gt;OperationBehaviorAttribute &lt;/span&gt;operationBehaviourAttribute = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;OperationBehaviorAttribute&lt;/span&gt;();
            operationDescription.Behaviors.Add(operationBehaviourAttribute);

            &lt;span style="color:#2b91af;"&gt;DataContractSerializerOperationBehavior &lt;/span&gt;d = 
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DataContractSerializerOperationBehavior&lt;/span&gt;(operationDescription);
            operationDescription.Behaviors.Add(d);
        }

        &lt;span style="color:blue;"&gt;return &lt;/span&gt;contractDescription;
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;AddService&amp;lt;T&amp;gt;(&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;serviceType)
    {
        &lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;contractType = &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(T);

        &lt;span style="color:#2b91af;"&gt;ServiceHost &lt;/span&gt;host = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceHost&lt;/span&gt;(serviceType, &lt;span style="color:blue;"&gt;this&lt;/span&gt;.BaseAddress);
        &lt;span style="color:#2b91af;"&gt;WSHttpBinding &lt;/span&gt;binding = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;WSHttpBinding&lt;/span&gt;(&lt;span style="color:#2b91af;"&gt;SecurityMode&lt;/span&gt;.None);

        &lt;span style="color:#2b91af;"&gt;ContractDescription &lt;/span&gt;contractDescription = &lt;span style="color:blue;"&gt;this&lt;/span&gt;.CreateContractDescription(contractType, serviceType);

        &lt;span style="color:#2b91af;"&gt;ServiceEndpoint &lt;/span&gt;endPoint = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceEndpoint&lt;/span&gt;(
            contractDescription,
            binding,
            &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;EndpointAddress&lt;/span&gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt;.BaseAddress));

        host.Description.Endpoints.Add(endPoint);

        &lt;span style="color:#2b91af;"&gt;ServiceMetadataBehavior &lt;/span&gt;smb = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceMetadataBehavior&lt;/span&gt;();
        smb.HttpGetEnabled = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;
        smb.MetadataExporter.ExportContract(contractDescription);
        host.Description.Behaviors.Add(smb);



        host.Authorization.PrincipalPermissionMode = &lt;span style="color:#2b91af;"&gt;PrincipalPermissionMode&lt;/span&gt;.None;
        host.Open();
    }
}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;p&gt;As you can see it is pretty straight forward defining the contracts and its messages. The magic is located at the end of the method when all the behaviors are added. Behaviors are the real magic of WCF, they pretty much make WCF tick. :)&amp;nbsp; We add a ServiceOperationBehavior that we&amp;#39;ll see the code for below, in addition we add some behaviors that ship with WCF; OperationBehaviorAttribute and DataContractSerializerOperationBehavior. The latter of the two will most likely be removed in Part 2 of this article when I will post that. The reason is that we will have to create our own DataContract serializer in order to be able to serialize any data types as mentioned earlier.&lt;/p&gt;
&lt;p&gt;The purpose of the ServiceOperationBehavior is to intercept the handling of the request and response for the services we&amp;#39;re exposing. In this article we use the default XML serializer as you can see in the ApplyDispatchBehavior() method. Again for this example we are doing things very very simple and we don&amp;#39;t pay attention to things like security.&amp;nbsp; &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceOperationBehavior &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IOperationBehavior
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MethodInfo &lt;/span&gt;_methodInfo;

    &lt;span style="color:blue;"&gt;private &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IOperationBehavior &lt;/span&gt;_innerBehavior;


    &lt;span style="color:blue;"&gt;internal &lt;/span&gt;ServiceOperationBehavior(&lt;span style="color:#2b91af;"&gt;MethodInfo &lt;/span&gt;methodInfo)
    {
        &lt;span style="color:blue;"&gt;this&lt;/span&gt;._methodInfo = methodInfo;
    }

    &lt;span style="color:blue;"&gt;#region &lt;/span&gt;IOperationBehavior Members

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;AddBindingParameters(
        &lt;span style="color:#2b91af;"&gt;OperationDescription &lt;/span&gt;operationDescription, &lt;span style="color:#2b91af;"&gt;BindingParameterCollection &lt;/span&gt;bindingParameters)
    {
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ApplyClientBehavior(&lt;span style="color:#2b91af;"&gt;OperationDescription &lt;/span&gt;operationDescription, 
        &lt;span style="color:#2b91af;"&gt;ClientOperation &lt;/span&gt;clientOperation)
    {
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ApplyDispatchBehavior(&lt;span style="color:#2b91af;"&gt;OperationDescription &lt;/span&gt;operationDescription, 
        &lt;span style="color:#2b91af;"&gt;DispatchOperation &lt;/span&gt;dispatchOperation)
    {
        &lt;span style="color:blue;"&gt;if &lt;/span&gt;(&lt;span style="color:blue;"&gt;null &lt;/span&gt;== &lt;span style="color:blue;"&gt;this&lt;/span&gt;._innerBehavior)
        {
            &lt;span style="color:blue;"&gt;this&lt;/span&gt;._innerBehavior = 
                &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;XmlSerializerOperationBehavior&lt;/span&gt;(operationDescription) &lt;span style="color:blue;"&gt;as &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IOperationBehavior&lt;/span&gt;;
        }

        &lt;span style="color:blue;"&gt;this&lt;/span&gt;._innerBehavior.ApplyDispatchBehavior(operationDescription, dispatchOperation);

        dispatchOperation.DeserializeRequest = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;
        dispatchOperation.SerializeReply = &lt;span style="color:blue;"&gt;true&lt;/span&gt;;

        dispatchOperation.Invoker = &lt;span style="color:blue;"&gt;new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceInvoker&lt;/span&gt;(&lt;span style="color:blue;"&gt;this&lt;/span&gt;._methodInfo);
    }

    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;Validate(&lt;span style="color:#2b91af;"&gt;OperationDescription &lt;/span&gt;operationDescription)
    {
    }

    &lt;span style="color:blue;"&gt;#endregion
&lt;/span&gt;}
&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;In the ApplyDispatchBehavior method we setup an invoker to handle the invocation of the methods. The invoker looks like this : &lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;ServiceInvoker &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IOperationInvoker
&lt;/span&gt;{
    &lt;span style="color:blue;"&gt;private readonly &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;MethodInfo &lt;/span&gt;_methodInfo;

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;ServiceInvoker(&lt;span style="color:#2b91af;"&gt;MethodInfo &lt;/span&gt;methodInfo)
    {
        &lt;span style="color:blue;"&gt;this&lt;/span&gt;._methodInfo = methodInfo;
    }

    &lt;span style="color:blue;"&gt;#region &lt;/span&gt;IOperationInvoker Members

    &lt;span style="color:blue;"&gt;public object&lt;/span&gt;[] AllocateInputs()
    {
        &lt;span style="color:blue;"&gt;return new object&lt;/span&gt;[0];
    }

    &lt;span style="color:blue;"&gt;public object &lt;/span&gt;Invoke(&lt;span style="color:blue;"&gt;object &lt;/span&gt;instance, &lt;span style="color:blue;"&gt;object&lt;/span&gt;[] inputs, &lt;span style="color:blue;"&gt;out object&lt;/span&gt;[] outputs)
    {
        outputs = &lt;span style="color:blue;"&gt;new object&lt;/span&gt;[0];
        &lt;span style="color:blue;"&gt;object &lt;/span&gt;returnValue = &lt;span style="color:blue;"&gt;this&lt;/span&gt;._methodInfo.Invoke(instance, inputs);
        &lt;span style="color:blue;"&gt;return &lt;/span&gt;returnValue;

    }

    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;IAsyncResult &lt;/span&gt;InvokeBegin(&lt;span style="color:blue;"&gt;object &lt;/span&gt;instance, &lt;span style="color:blue;"&gt;object&lt;/span&gt;[] inputs, &lt;span style="color:#2b91af;"&gt;AsyncCallback &lt;/span&gt;callback, &lt;span style="color:blue;"&gt;object &lt;/span&gt;state)
    {
        &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NotImplementedException&lt;/span&gt;();
    }

    &lt;span style="color:blue;"&gt;public object &lt;/span&gt;InvokeEnd(&lt;span style="color:blue;"&gt;object &lt;/span&gt;instance, &lt;span style="color:blue;"&gt;out object&lt;/span&gt;[] outputs, &lt;span style="color:#2b91af;"&gt;IAsyncResult &lt;/span&gt;result)
    {
        &lt;span style="color:blue;"&gt;throw new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;NotImplementedException&lt;/span&gt;();
    }

    &lt;span style="color:blue;"&gt;public bool &lt;/span&gt;IsSynchronous
    {
        &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return true&lt;/span&gt;; }
    }

    &lt;span style="color:blue;"&gt;#endregion
&lt;/span&gt;}&lt;/pre&gt;&lt;pre class="code"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="code"&gt;&lt;/pre&gt;
&lt;p&gt;&lt;font color="#333333"&gt;You can download the sample VS2008 project &lt;/font&gt;
&lt;div class="wlWriterSmartContent" id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:a9c0133f-ca1d-4a45-96fb-ec83d55e9448" style="padding-right:0px;display:inline;padding-left:0px;padding-bottom:0px;margin:0px;padding-top:0px;"&gt;&lt;p&gt;&lt;div&gt;&lt;a href="http://www.dolittle.com/blogs/einar/WindowsLiveWriter/CreatinganabstractionfromWCF_1015E/SimpleServiceHoster_2.zip" target="_self"&gt;here&lt;/a&gt;&lt;/div&gt;&lt;/p&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#333333"&gt;Compile and run and open a browser and go to &lt;a href="http://localhost:8000"&gt;http://localhost:8000&lt;/a&gt;.&lt;/font&gt;&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/02/01/programatically-creating-a-wcf-contract-and-exposing-it-part1.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/02/01/programatically-creating-a-wcf-contract-and-exposing-it-part1.aspx"border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://www.gamedevkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/02/01/programatically-creating-a-wcf-contract-and-exposing-it-part1.aspx"&gt;&lt;img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/02/01/programatically-creating-a-wcf-contract-and-exposing-it-part1.aspx"border="0" alt="kick it on GameDevKicks.com" /&gt;&lt;/a&gt; &lt;img src="http://www.dolittle.com/aggbug.aspx?PostID=1233" width="1" height="1"&gt;</description><category domain="http://www.dolittle.com/blogs/einar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/.net/default.aspx">.net</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/WCF/default.aspx">WCF</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/SOA/default.aspx">SOA</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/Services/default.aspx">Services</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/Contracts/default.aspx">Contracts</category></item><item><title>VS2008 QFE Debugging on a WCF application</title><link>http://www.dolittle.com/blogs/einar/archive/2008/01/17/vs2008-qfe-debugging-on-a-wcf-application.aspx</link><pubDate>Thu, 17 Jan 2008 13:02:00 GMT</pubDate><guid isPermaLink="false">a744be0d-d88a-46a6-b249-55961ed4a125:1124</guid><dc:creator>Einar Ingebrigtsen</dc:creator><slash:comments>0</slash:comments><description>&lt;p&gt;I got all excited today and downloaded and instaled &lt;a href="https://login.live.com/login.srf?wa=wsignin1.0&amp;amp;rpsnv=10&amp;amp;ct=1200575156&amp;amp;rver=4.0.1534.0&amp;amp;wp=MBI_SSL&amp;amp;wreply=https:%2F%2Fconnect.microsoft.com%2FVisualStudio%2FDownloads%2FDownloadDetails.aspx%3FDownloadID%3D10443%26wa%3Dwsignin1.0&amp;amp;lc=1033&amp;amp;id=64416" target="_blank"&gt;VS2008 QFE&lt;/a&gt; (.net source code) and enabled debugging according to &lt;a href="http://blogs.msdn.com/sburke/archive/2008/01/16/configuring-visual-studio-to-debug-net-framework-source-code.aspx" target="_blank"&gt;Shawn Burke&amp;#39;s blog&lt;/a&gt; and fired up a WCF server application I&amp;#39;m working on. The server is using the ServerHost class for hosting it. Much to my surprise I kept getting an ConfigurationErrorsException with the message : &amp;quot;This element is not currently associated with any context&amp;quot;. The application hadn&amp;#39;t changed for a couple of days and I have no configuration file as I define everything programmatically. The only thing I had done was to install QFE and enable Debugging. &lt;/p&gt; &lt;p&gt;Turns out that the &amp;quot;Enable just my code&amp;quot; option that I turned off to enable QFE debugging caused this. I haven&amp;#39;t really bothered to figure out why yet, just thought I&amp;#39;d blog the incident straight away.&lt;/p&gt;&lt;a href="http://www.dotnetkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/01/17/vs2008-qfe-debugging-on-a-wcf-application.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/01/17/vs2008-qfe-debugging-on-a-wcf-application.aspx"border="0" alt="kick it on DotNetKicks.com" /&gt;&lt;/a&gt; &lt;a href="http://www.gamedevkicks.com/kick/?url=http://www.dolittle.com/blogs/einar/archive/2008/01/17/vs2008-qfe-debugging-on-a-wcf-application.aspx"&gt;&lt;img src="http://www.gamedevkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.dolittle.com/blogs/einar/archive/2008/01/17/vs2008-qfe-debugging-on-a-wcf-application.aspx"border="0" alt="kick it on GameDevKicks.com" /&gt;&lt;/a&gt; &lt;img src="http://www.dolittle.com/aggbug.aspx?PostID=1124" width="1" height="1"&gt;</description><category domain="http://www.dolittle.com/blogs/einar/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/.net/default.aspx">.net</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/Visual+Studio+2008/default.aspx">Visual Studio 2008</category><category domain="http://www.dolittle.com/blogs/einar/archive/tags/WCF/default.aspx">WCF</category></item></channel></rss>