.NET Development Examined

Creating a Service Layer Part II

Friday, October 10, 2014

In Part One of our WCF services series we looked at how to create the contracts and implementation of our WCF/CODE Framework service. In this part we will look at how to instantiate our service end points during development using a CODE Framework Development Host.

Exercise 2: Create CODE Framework Development Host

In this exercise you will create a CODE Framework Development Host (CFDH) project. Standard WCF projects have a Service Test Harness which allows you to interact with your service, but it is limited in its functionality. The CFDH allows the developer to interact with your CODE Framework service during development, without having to deploy it and displays exceptions right in the UI. Additionally the CFDH provides service endpoints so that your client project can access the services. This provides for a highly interactive development environment.

Task 1 – Create the Development Host Project

  1. Add a new Implementation Project. Right click on the solution (not a project) and select Add/New Project.
  2. Expand the templates listing and click on CODE.Framework.
  3. Select the CODE Framework Service Development Host Project and give it a name of <your project>.Services.DevelopmentHost and click OK.
  4. Select the Development Host Options. The development host project wizard will examine your solution an present you with the option to expose endpoints on any service implementations found. You may select one or more of the services endpoints (Http Basic/WS*, TCP/IP, REST JSON/XAML). Additionally you can choose to allow cross-domain calls from JavaScript and whether or not to generate WSDLs for SOAP web services. For now leave all the default options and click OK.
  5. Adjust the service endpoints. Open the Program.cs file, you will see several lines calling methods on the TestServiceHost object. You can comment out any endpoints not desired (the check boxes presented in the option selection control the creation of these options, but you can modify them at any time directly in the Program.cs file). For this exercise, we recommend commenting everything except NetTcp as the rest use http and may cause contention issues with apps that use port 80 such as IIS and Skype.
    //host.AddServiceHostBasicHttp(typeof(CustomerService), true);
    //host.AddServiceHostWsHttp(typeof(CustomerService), true);
  6. Right click on the Development Host project and select Debug/Start New Instance
  7. If presented with a Windows Firewall notice allow it, you should see the Development WCF Service Host windows, listing all endpoints defined in the Program.cs file.
  8. The Domain, Base Port, and Path shown here are the default values of the development host. If desired you can adjust these values in the App.config file, but for the lab we will leave the defaults.
      <!-- The following settings can be set optionally (
           defaults usually suffice for development hosts) -->
      <add key="ServiceBaseUrl" value="localhost"/>
      <add key="ServiceBasePath" value="dev"/>
      <add key="ServiceBasePort" value="50000"/>
  9. Double click on the ICustomerService endpoint and select the GetCustomerList method.
  10. Click on the Invoke button. This will call the implementation of the GetCustomerList method and display the response object. Expand the response to examine the details of the response object.
  11. Click on the GetCustomer item on the test harness. Here we can see that when a request object requires parameters the test harness allows for entry. At this point we can invoke this method with an empty Guid and it will return a customer record. Later when the implementation of GetCustomer is updated to pull data from a database we will have to supply an existing Guid before information is retrieved.
  12. At this point you have a fully functional service that is exposed using the development host.

In Part 3 of this series we will illustrate how to access an SQL database using Entity Framework.


Add Comment