Pierre Greborio.NET

Talking about .NET world

January 2006 - Posts

First drop of WCF client addin for Visual Studio 2005

Ok, here it is. I uploaded the first beta of WCF Client tools Addins for Visual Studio 2005. After the setup (available here) you'll fine two new context menus items on the project:

Context menu
  • Add WCF Service Reference...
  • Create WCF Proxy Unit Test...

The first one permits to create the CF proxy class and it's configuration section. As you can see in the following picture you can set a lot of attributes to the code generator (more or less as svcutil.exe tool does)

Add service proxy

 

The second addin generates also the NUnit (version 2.2 or above) code in order to have unit testing on the selected service:

Add Unit Testing

For example, if you want to test the SelfHost service (available on WinFX SDK) you can add the service address (see picture above) and set the test class name. Three files will be generated (app.config if already present will be updated only):

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="
http://schemas.microsoft.com/.NetConfiguration/v2.0">
    <system.serviceModel>
        <client>
            <endpoint address="
http://localhost:8000/servicemodelsamples/service"
                bindingConfiguration="WSHttpBinding_ICalculator" binding="customBinding"
                name="WSHttpBinding_ICalculator" contract="TestClient.ICalculator">
                <identity>
                    <userPrincipalName value="
pierreg@PEWAY.loc" />
                </identity>
            </endpoint>
        </client>
        <bindings>
            <customBinding>
                <binding name="Secure conversation bootstrap binding dda1dd0a-f6e4-4686-8210-90f381484fe3">
                    <security />
                    <textMessageEncoding />
                </binding>
                <binding name="WSHttpBinding_ICalculator">
                    <security authenticationMode="SecureConversation" bootstrapBindingConfiguration="Secure conversation bootstrap binding dda1dd0a-f6e4-4686-8210-90f381484fe3"
                        bootstrapBindingSectionName="customBinding" />
                    <textMessageEncoding />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
    </system.serviceModel>
</configuration>

Note: during the configuration file generation I correct the contract attrbute with the correct class namespace and add the name attribute to the endpoint element (this doesn't happens with svcutil.exe).

contract interface:

namespace TestClient {    
    
    [System.ServiceModel.ServiceContractAttribute(Namespace="
http://Microsoft.ServiceModel.Samples")]
    public interface ICalculator {
       
        [System.ServiceModel.OperationContractAttribute(Action="
http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
        double Add(double n1, double n2);
       
        [System.ServiceModel.OperationContractAttribute(Action="
http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
        double Subtract(double n1, double n2);
       
        [System.ServiceModel.OperationContractAttribute(Action="
http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
        double Multiply(double n1, double n2);
       
        [System.ServiceModel.OperationContractAttribute(Action="
http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
        double Divide(double n1, double n2);
    }
}

and finally the test class:

namespace TestClient
{
    using System;
    using System.Text;
    using System.ServiceModel;
    using System.Collections.Generic;
    using NUnit.Framework;
   
   
    [TestFixture()]
    public class ICalculatorServiceTest
    {
       
        public ICalculatorServiceTest()
        {
            /// TODO: Add constructor logic here
        }
       
        [Test()]
        public virtual void TestAdd_WSHttpBinding_ICalculator()
        {

            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("WSHttpBinding_ICalculator");

            ICalculator channel = factory.CreateChannel();
            // TODO: Initialize to an appropriate value
            double n1 = default(double);
            // TODO: Initialize to an appropriate value
            double n2 = default(double);

            double expected = default(double);
            double actual = default(double);


            actual = channel.Add(n1, n2);

            Assert.AreEqual(expected, actual, "Add did not return the expected value.");

            ((IChannel)(channel)).Close();
            ((IChannel)(channel)).Dispose();
        }
       
        [Test()]
        public virtual void TestSubtract_WSHttpBinding_ICalculator()
        {

            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("WSHttpBinding_ICalculator");

            ICalculator channel = factory.CreateChannel();
            // TODO: Initialize to an appropriate value
            double n1 = default(double);
            // TODO: Initialize to an appropriate value
            double n2 = default(double);

            double expected = default(double);
            double actual = default(double);


            actual = channel.Subtract(n1, n2);

            Assert.AreEqual(expected, actual, "Subtract did not return the expected value.");

            ((IChannel)(channel)).Close();
            ((IChannel)(channel)).Dispose();
        }
       
        [Test()]
        public virtual void TestMultiply_WSHttpBinding_ICalculator()
        {

            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("WSHttpBinding_ICalculator");

            ICalculator channel = factory.CreateChannel();
            // TODO: Initialize to an appropriate value
            double n1 = default(double);
            // TODO: Initialize to an appropriate value
            double n2 = default(double);

            double expected = default(double);
            double actual = default(double);


            actual = channel.Multiply(n1, n2);

            Assert.AreEqual(expected, actual, "Multiply did not return the expected value.");

            ((IChannel)(channel)).Close();
            ((IChannel)(channel)).Dispose();
        }
       
        [Test()]
        public virtual void TestDivide_WSHttpBinding_ICalculator()
        {

            ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("WSHttpBinding_ICalculator");

            ICalculator channel = factory.CreateChannel();
            // TODO: Initialize to an appropriate value
            double n1 = default(double);
            // TODO: Initialize to an appropriate value
            double n2 = default(double);

            double expected = default(double);
            double actual = default(double);


            actual = channel.Divide(n1, n2);

            Assert.AreEqual(expected, actual, "Divide did not return the expected value.");

            ((IChannel)(channel)).Close();
            ((IChannel)(channel)).Dispose();
        }
    }
}

Obviously, you need to have WCF installed as well as NUnit.

Any comment are welcome. Thanks!

Posted: Jan 05 2006, 11:51 AM by PierreG | with 2 comment(s)
Filed under:
More Posts