TechHui

Hawaiʻi's Technology Community

Windows Communication Foundation is a framework for building service-oriented applications.  Over the next several weeks, I intend to write about WCF and share some of my experience working with it.  This specific post covers a very simple getting started scenario.  It does not assume you know anything about WCF, or have ever seen it before.  It is a step-by-step guide to creating a service and a test case for it.  I do assume that you have a version of VS2010 that includes the test framework.

There are many pictures, and not much detail.  I am very intentionally keeping this light.  If there are any problems getting through this, please message me.  Future postings will assume that the functionality list here works for you, and each posting will build on the previous postings.

The empty solution

This project will start with an empty solution file.  In visual studio, without any projects open:

File->New-Project:

This will give you a very empty solution:

Creating the test project

Right click on the solution and select add->New Project:

VS2010 will think about your request for a little while, and then create the project and related files.  Press ctrl-r, t to run the created test case.  If the test passes then you are on your way.  Now that you know you have a working test case, delete the file “UnitTest.cs” you do not need it.

Creating the WCF project

Right click on the solution and select add->New Project.  This time, create a WCF service library project:

This will create for you a very simple WCF project.  Your solution so far should look like this:

Driving the tests using the service

Under the covers, VS2010 has already made configuration changes that will open up the service library in a test host, even when you are running tests.  What we need to do now is make a service reference in the test project.  To do this right click on the ServiceTesting project and select Add Service Reference.  It will open the Add Service Reference dialog.  Click the Discover button, the result should look like this:

Click OK and visual studio will do the work required to make the service reference.  When it is done, VS will look like this:

Testing the service

With the service reference in place, we can now create a test case.  It is worth noting that it is possible to write a complete set of test cases without ever making the service reference, exactly how that is done will be covered in a future post.  At this time right click on the ServiceTesting project and select Add->New Test:

And create a test to call the GetData method.  Here is the code:

namespace ServiceTesting

{

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using ServiceTesting.ServiceReference1;

 

    [TestClass]

    public class Service1Test

    {

        [TestMethod]

        public void GetDataTest()

        {

            IService1 service = new Service1Client();

            string result = service.GetData(5);

            Assert.AreEqual("You entered: 5", result, "The service did not send the expected string");

        }

    }

}

 

At this point, press F5.  The test will compile, WcfSvcHost will start, and then the test will run:

Conclusion

There you have it.  Everything you need to create a WCF service and a test to drive it.  WCF is very powerful and very extensible so do not let the simplicity of this walk-thru fool you.  Next week we will take this project and create our own service.

Views: 165

Comment

You need to be a member of TechHui to add comments!

Join TechHui

Sponsors

web design, web development, localization

© 2024   Created by Daniel Leuck.   Powered by

Badges  |  Report an Issue  |  Terms of Service