Pierre Greborio.NET

Talking about .NET world

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:

Comments

rajeshpr said:

Hi Pierre

Do you have a drop of this for .Net framework 3.0. I am developing WCF applications using the 3.0 framework and not the CTP'S

When I tried to make use of the above addin in VS2005, but it gives me the following error message.

Could not load file or assembly ' System.ServiceModel 2.0.0.0 or one of its dependencies. The system cannot find the file specified.

Can you please help me out with this.

Could you please let me know as to how do I make use of this tool with .Net framework 3.0 and VS 2005.

Also this tool also has an option to generate NUnit test cases for the WCF proxy.

Incase if this tool is not applicable for the .Net 3.0 is there any similar tool provided by Microsoft.

I am aware of the SvcUtil, but do not want to make use of it.

# January 9, 2007 3:32 AM

PierreG said:

Hi,

Unfortunately my tools works only with WCF betas and I didn't had too much time to upgrade the code to the RTM. Since the API changed significantly I started to refactor the whole tool…but I need some time to end it.

Thanks,

Pierre

# January 12, 2007 1:37 AM