Exposing WCF Services using xfNetLink .NETJuly 1, 2011
Exposing WCF services using Synergy .NET InteropSeptember 2, 2011
There are several ways that you can use Synergy/DE tools to expose WCF services, but regardless of which way you decide to create a service, the next decision you have to make is how to host it. By hosting a WCF service you expos the service and make it possible to use it from other applications. In this post I will introduce you to one way that WCF services may be hosted, via an ASP.NET web application and an IIS web server. In a later post I will show you how to host WCF services without this requirement for an IIS web server.
This is the third in a series of posts relating to the various ways in which Synergy developers can use of Windows Communication Foundation (WCF) when building their applications. The posts in the series are:
- Building Distributed Apps with Synergy/DE and WCF
- Exposing WCF Services using xfNetLink .NET
- Hosting WCF Services in an ASP.NET Web Application (this post)
- Exposing WCF Services using Synergy .NET Interop
- Self-Hosting WCF Services
- Exposing WCF services using Synergy .NET
A WCF service is simply a collection of one or more classes in an assembly, but the whole point of implementing a WCF service is to make those classes available to be used by other applications. And those applications will often be located on different networked systems from the service itself. So, in order for a WCF service to be useful, it needs to be “hosted” somewhere, and that somewhere needs to be available on a network.
There are three basic ways that WCF services can be hosted:
- Inside a Web application located on a Web server.
- Using Windows Process Activation Services (WAS). This mechanism was introduced in Windows Server 2008 and provides a mechanism for hosting WCF services without requiring a Web server.
- Services can be “self” hosted by some custom .NET application. For example, developers often host WCF services inside a Windows Service application.
In this post I will show you the basics of hosting a WCF service in an ASP.NET web application. In a future post I will go on to show how to self-host services.
Setting up basic hosting for a WCF service inside an ASP.NET Web application is very easy, but as Synergy .NET doesn’t have support for creating Web applications. You’ll have to use another .NET language, either C# or VB.NET. However, don’t worry about that too much, because there won’t actually be any code in the Web application!
- Start Visual Studio 2010 and create a new ASP.NET application:
- From the menu, select File > New > Project.
- Under Installed Templates, select Visual C# > Web > ASP.NET Empty Web Application.
- Chose the name and location for your new Web application.
- Click the OK button to create the new project.
- Add a new WCF Service to the project:
- From the menu, select Project > Add New Item.
- Under Installed Templates, select Visual C# > Web > WCF Service.
- Enter the name for your new service. The name will be part of the service endpoint URI that the client connects to, so pick something meaningful. For example, if your service allows orders to be placed then you might name the service something like OrderServices.svc.
- Click the Add button to add the new service to the project.
You will notice that three files were added to the project:
In addition to the service (.svc) file that you named, you will see two C# source files. These files were added because Visual Studio provided all of the files that you would need in order to expose a new WCF service that would be manually coded in these files. But what we’re trying to do is host a WCF service that already exists in another .NET assembly.
- Delete the two C# source files by right-clicking on each and selecting delete.
In order to expose a WCF service that exists in another assembly, the first thing we need to do is add a reference to that assembly, to make the classes it contains available within the new Web application.
- From the menu select Project > Add Reference and then select Browse.
- Locate and select the assembly containing your WCF service and add a reference to your project.
- If the assembly you selected was created using xfNetLink .NET then you will also need to add a reference to the xfnlnet.dll assembly, as is usual when using xfNetLink .NET
You’ll need to know that namespace and class name of the WCF service in your assembly. If you can’t remember this that double-click on the assembly that you just referenced and then use Object Browser to drill into the assembly and figure out the names. All that remains is to “point” the service file at your WCF service class.
- Double-click on the service (.svc) file to edit it. You should see something like this:
Edit the file as follows:
- Change Language=”C#” to Language=”Synergy”.
- Change Service=”…” to Service=”YourNamespace.YourWcfClass” (obviously replacing YourNamespace and YourWcfClass with the namespace and class name of your WCF service).
- Remove the CodeBehind=”…” item … there is no code-behind, the code is all in the other assembly.
- Save the file.
So you should now have something like this:
If your service is based on an assembly created using xfNetLink .NET then you have one final configuration step to perform. You need to tell xfNetLink .NET how to connect to xfServerPlus. You can do this by using the Synergy xfNetLink .NET Configuration Utility (look in the Windows Start Menu) to edit the new web applications Web.config file. If your xfServerPlus service is not on the same machine as your new web application then you must specify a host name or IP address, and if xfServerPlus is not using the default port (2356) then you must also specify a port number. When you’re done, the Web.config file should contain a section like this:
That’s it, you’re done. You should now have a working WCF service!
- To see if the service has really been hosted, right-click on the service (.svc) file and select View In Browser.
You should see the service home page, which will look something like this:
If you see this page then your service is up and running. By the way, the URI in your browsers address window is the URI that you would use to add a “Service Reference” to the service in a client application … but we’ll get on to that in a later post.
Of course, all we have actually done here is set up a web application to host a WCF service, but we’re using Visual Studio’s built in development web server (Cassini) to actually host the service. If you were doing this for real then you’d need to deploy your new web application onto a “real” IIS web server.
We’re not done with the subject of hosting WCF services; in fact we’ve only really scratched the surface. One step at a time!
In my next post in this series I will show you how to Expose a WCF services using Synergy .NET Interop.