WCF Services—Hosting on IIS


Jump to: navigation, search
Visual C# Tutorials
.NET Framework Tutorials

WCF Services

© 2007 Chris Peiris, Dennis Mulder

Hosting on IIS

The previous examples are all included in the downloadable source code with an automated script for creating the virtual directory on IIS. This section shows you how to both create a .NET 3.0 WCF service and host it in IIS.

The first step to take is to create an empty solution file. This provides total control over the location of the file. If you bypass this step, Visual Studio 2005 creates the project where you want it to, but the solution file is put in your default location for your Visual Studio projects.

To generate an empty solution file that you’ll add your projects to, from within Visual Studio select File -> New Project -> Other Project Types -> Visual Studio Solutions -> Blank Solution. Be sure to specify both the name and the location for the blank solution.

After creating the empty solution file, the next step is to add a .NET 3.0 (WCF) service web project. If you immediately add the project to the solution, the project files are created in the default c:\inetpub\wwwroot subdirectory. To gain a little more control, you can create the IIS virtual site before adding the project location to the solution file.

The next step is to just create a subdirectory where the solution file is located, as shown in Figure 4-6.



Figure 4-6. Creating a subdirectory in solution folder


Then, you need to create a mapping in IIS. You can navigate through IIS Manager; for simplicity, just right-click the folder, and select Properties.

Once the Properties dialog box appears, click the Web Sharing tab, as shown in Figure 4-7. Simply click the radio button Share This Folder, and the Edit Alias dialog appears. You can enable directory browsing to make it easier to view and click items in the website. Generally, this is a setting only for development.


Caution This setting allows users to browse all files on the site, just like Windows Explorer. Although it’s a nice feature, be careful with it in production.



Image:7028f0407.jpg
Figure 4-7. Web Sharing tab’s Edit Alias dialog box


At this point, click OK several times to dismiss the dialog boxes. The site should now be available through the URL http://localhost/Example04Web. However, you still need to check the version of ASP.NET that is set for this site. If you have only .NET 2.0 installed—that is, .NET 1.1 was never installed—there should be nothing else to do; however, it doesn’t hurt to just check.

So, launch IIS Manager (Start -> Control Panel -> Administrative Tools -> Internet Information Services). Once you see the Properties dialog box, click the ASP.NET tab, and then switch the version of ASP.NET using the drop-down list to the .NET 3.0–supported version, which is 2.0.50727 (the RTM version).


Tip If you don’t see this icon, then either you have limited access on the machine or IIS is not installed. Please ensure IIS is installed and you have the appropriate permissions.


This example has one additional step—to provide access to resources known as anonymous requests. Anonymous requests are any requests that have no identity or Windows principals associated with the HTTP request.

Click the Directory Security tab, and then click Edit under the Anonymous Access and Authentication Control section of the dialog box. Ensure that the option Anonymous Access is enabled. This will allow the example to run without stepping into how to provide authentication credentials on the requests.

Dismiss all open dialog boxes by clicking the OK buttons. At this point, you now have a solution directory with a child project that has, or will have, all its resources (source code files and content) located in a directory of your choosing (that is, not in the default c:\inetpub\wwwroot directory).

Now you’re ready to add the project to the solution you created earlier. In Visual Studio, select the solution in Solution Explorer, right-click, and then select Add -> New Web Site.

At this point, ensure you select the .NET 3.0/WCF service template, set HTTP as the location, use the URL that was set on the folder alias using web sharing in Windows Explorer, and set the language of your choice. Click OK, and the Visual Studio .NET 3.0 template system creates a project starting point for your service, giving you a complete project.

Notice that the project template leverages the special folder names for the application code and application data. In the prior example, the source code was hosted directly in the *.svc file. The project shown here, which is generated from the .NET 3.0 template, has a distinct source directory along with a *.cs file that contains the interface and class implementation.


Tip In the real world, it’s best to separate the service interface (contract) and implementation types into their own assemblies (DLL), which translates to projects in Visual Studio. Having the structure as shown previously is a nice feature for ease of use, but from a physical separation perspective, it’s better to provide distinct assemblies for the tiers.


If you look at the file system using Windows Explorer or using a command prompt and view the directory you started in, you’ll see the same set of files listed under the project in Visual Studio Solution Explorer.

At this point, if you browse to the location http://localhost/example04Web/ using Internet Explorer, you’ll see a directory listing (as long as the settings are like those in Figure 4-7).

If you click service.svc, you are then brought to the default help screen generated by System.ServiceModel.Activiation.HttpHandler for the *.svc extensions, as shown in Figure 4-2.

At this point, you follow the same steps in a client application, either generating a proxy class directly through the use of the SvcUtil.exe utility or right-clicking the project and generating the proxy through the Add Service Reference add-in feature, as shown previously.

The accompanying solution for this example has a complete console client that makes a call into the WCF service you just created.


Previous_Page_.gif Next_Page_.gif

Share this page
  • del.icio.us
  • Facebook
  • Google+
  • StumbleUpon