1.2 Maths Host
In the ‘MathHost’ project there is also a form which enables the host to start and stop, this form was created to enable the WCF to self host, for it to be self hosting, its own code has to be provided to initialise the hosting environment, which has been achieved in these examples. The code for this is shown below:
Again a number of namespace catalogues are imported (not all shown here). Code is written to allow the program to use a specific computer port (8001). The service that is created is hosted when the ‘start’ button is clicked and stopped with the ‘stop’ button. When it is running the client will be able to use the calculations. In this form there is also code for each of the individual buttons, not shown here but can be seen in the design of the system. It hosts the WCF by creating a ‘Service Host’ and is made available when started through the local host of the computer managed application under port 8001.
Figure 2: Maths Host – Main Form
Figure 3: Form design: Host
After creating the WCF which is now self hosting, a client was made for the user of the system to use. This is what each individual computer would connect to. For this there would have to be some sort of link between the client and WCF. In the visual Studio 2008 command prompt application the following code was typed:
Svcutil: language:vb config:app.config
This command then created two files which were found in the visual studio file in the computers program file. These files were then added to a new project in visual basic which was going to be used as the client. Other ways of hosting the same application would not need to have this prompt typed as they do it for you. The files added were
- A new app.config file to replace the outstanding one in the client
- A class called MathService.vb
These files contained the relevant links to the self hosted WCF and the information that would input data into the SQL database as well as working out the calculations. The following show the design for the ‘Maths’ forms.
1.3 Maths Client
Figure 4: ‘Maths’ form design
This form would be the interface for anyone using the application. The code that was implemented behind this form is on the following page:
Figure 5: Client Form Code
The code implements each of the functions under the button click commands. They are taken using the ‘WCFConn’ which is a variable of the ‘MathsServiceContractClient´ which can be found within the app.config and service files that were produced from the WCF.
1.4 Database
When the application is running it calculates what the user wants and also automatically adds the function they have used along with the username to a an SQL database with the following field structure: -
Figure 6: Database Structure
1.5 Testing: Test 1, Database Interactivity
These are the current records in the database; this test shows the use of the database.
Figure 7: Database Row View
When the application is run the following information is entered within the form :
Figure 8: Test 1 Filling Form - Pythagoras
A Pythagoras Calculation
Figure 9: Test 1 – Trigonometrical Functions
A calculation using the Trigonometrical function Sine.
When each of the calculate buttons are pressed the data that has been entered, the ‘Username’ and the ‘Function’ used will be entered in the database as shown next:
Figure 10: Test 1 – Information entered into the database
Test 2: Turning off the Host
When the host is turned off the application shouldn’t work: This proves that the host is actually running the calculations in the Client. The WCF service host is ‘stopped’:
Figure 11: Test 2 - Stopping the Host
The client is run and the calculate button is clicked: the following error occurs:
Figure 12: Test 2 – Error occurred
This proves that the WCF is not currently running on its self host and is not allowing the computer port to use the connection WCF-WCF. The connection is refused and there fore doesn’t work; when the Host is turned back on it works fine. This proves that the client and Host are interacting correctly.
1.6 The Search Application
Apart from the Math application shown above another small system was developed using an SQL query that returns data to a client from the database. This was also created using a self hosting WCF with the query contained within the WCF.
As shown below a new host was created for the search. Once started it allowed the WCF to become self hosted and to interact with the client. Below shows the host in ‘Start’ and ‘Stop’ mode. When the host is switched off the client that is below will not work.
Figure 13: Search Host
Figure 14: Error When Search Host is not running
The same error occurs if the WCF is not hosted.
When running, the client is designed to retrieve all the data from the table in order to be viewed by the user. In the host a class called ‘SearchService.vb’ contains the following code to where the query lies. Not all the code follows, bits have been taken out to make it shorter such as the ‘import’ statements but the important bits are there.
Figure 15: SearchService.vb code in the WCF Host
This code builds a connection to the ‘Northwind’ Database through its data source. It then creates a dataset and table/rows to fetch the data in the table ‘JulianWCF’. It uses an SQL statement to retrieve all the fields from the table. It is implemented in an interface and built to self hosting configuration. The Host is then run and started to host the WCF written in the code. Two files are created from the command prompt screen in the visual studio command prompt by using a ‘svcutil’ command which gives service files to link the client with the host :
Figure 16: svcutil command
This links to the SearchService.vb class that was shown previously. Once the two files:
- App.config
- SearchService.vb
Are added to the client’s library files the client will run as long as the host is started and the table details are shown in a gridview.
Figure 17: Grid view of retrieved database information
Summary
The two examples in the ‘Math’ system give a good understanding into WCF service’ and they are also shown to how one can be self hosting. Self hosting is a good way to get an application up and running quickly especially when designing one. If a system was going to be using a WCF more long term then there would be other ways of hosting that may be needed to look at.
2. Hosting A WCF Service by Microsoft’s IIS Server
A quick explanation of a WCF is a framework that is used to build applications which inter-communicate; it is part of the .net framework and is usually made up of three parts, a service class, host environment and an end point. All interaction and communication with WCF services happens via the end point. It is a growing technology in which services can be created and a major advantage is that they can be used in different places via a network with only one host.
So the example contained on the CD and shown previous helps us to understand a WCF, it shows the calculations are provided in the service class on the WCF and is then is used by a client outside of that service, the WCF is self hosting with the WCF contained in the same file as the ServiceHost, through references in Visual Studio the client and host work together.
How could the WCF that is created however be hosted on Microsoft’s IIS server?
There are a few different ways to host a WCF when creating one in Microsoft’s Visual Studio:
- Self Hosting in the .net application (a managed application)
- Hosting in a windows service
- Hosting using the variety of IIS options
The easiest way of hosting is by self-hosting it. This is shown in the example created on the disk provided. To self host two things are needed, the WCF runtime, also there needs to be a managed .net application in which can host the ‘ServiceHost’ this again was shown previously. To self host there must be lines of code written to start and stop the host which we built previously. There are many advantages and disadvantages of self hosting shown below taken from Microsoft’s support site [2]:
The following are the advantages of self-hosting:
-
Is easy to use: With only a few lines of code you have your service running.
-
Is flexible: You can easily control the lifetime of your services through the Open() and Close() methods of ServiceHost<T>.
-
Is easy to debug: Debugging WCF services that are hosted in a self-hosted environment provides a familiar way of debugging, without having to attach to separate applications that activate your service.
-
Is easy to deploy: In general, deploying simple Windows applications is as easy as xcopy. You don't need any complex deployment scenarios on server farms, and the like, to deploy a simple Windows application that serves as a WCF ServiceHost.
-
Supports all bindings and transports: Self-hosting doesn't limit you to out-of-the-box bindings and transports whatsoever. On Windows XP and Windows Server 2003, IIS limits you to HTTP only. [2]
The following are the disadvantages of self-hosting:
-
Limited availability: The service is reachable only when the application is running.
-
Limited features: Self-hosted applications have limited support for high availability, easy manageability, robustness, recoverability, versioning, and deployment scenarios. At least, out-of-the-box WCF doesn't provide these, so in a self-hosted scenario you have to implement these features yourself; IIS, for example, comes with several of these features by default. [2]
There seems to be disadvantages relating to applications that would be used in enterprise. Self hosting is a good solution in the development stages of the application. So what other option do we have? Well hosting using Internet Information Services.
The first step to this is to ensure that the Internet Information Service (IIS) server is correctly installed on the machine that is being used. To host a WCF then in IIS there needs to be a new physical file added with the .svc extension. This file is the means to allow the IIS to create a service file for you. IIS takes over the interaction then between the service and host meaning that the host doesn’t need to be started manually, or the link between the host client and command prompt doesn’t need to be started.[3]
When creating the WCF it needs to be created as a WCF service in the new website page [3] This creates all the base files for the service, a few changes now need to be made and features added to enable the host on IIS.
By creating the files through the WCF service a basic service is created and it can be tested by pressing F5 on the keyboard.
What can also be done in this service is turn on the meta data to see the service description, to do this the following code gets added to the web.config file :
Figure 18: Code for enabling meta data.
This exposes the end points by allowing meta data, a windows form can be created that returns the data which is exposed in this service.
A windows form is added to the project and then a reference is added to the created service. So the service is then started and then on the solution explorer in the current windows form a new service is added by clicking ‘add service reference’.
The URI of the service is then added and by clicking ok the SVCUTIL that was seen earlier it is run automatically, this will contact the running service and return the service information and configuration files needed to connect the Windows Form application. [4]
The references are then added to the project in the client that is being created. Into the form load event the code is entered to connect to the service through the local host.
Summary
This way of building a WCF using IIS to host, the code on the WCF is created through the form load and links automatically to the local host using IIS.
In IIS, you have to define your endpoints in the Web.config file. Using IIS means that the WCF configuration must be set up in the web.config file of the application that wants to host the service. The service configuration in this file is very similar to that of the self hosting service. By adding a reference to the solution in the explorer the SVCUTIL cn be done automatically by IIS.
Bibliography
[1] , date accessed, 16.5.08
[2] , date accessed, 16.5.08
[3] , date accessed, 17.5.08
[4] , date accessed, 18.5.08