asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search










Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Hosting Indigo Web Services
By Chris Peiris
Rating: 4.0 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

  • download source code
  • Abstract

    Chris Peiris explains how to host an Indigo web service in his second of three articles. We will examine the IIS, self hosting and Windows Activation Service hosting options available in Indigo. Then he will detail step by step instructions to build sample code for an IIS hosted and self hosted Indigo web service.

    In the first article in this series, we discussed the basics of Indigo web services. We also explored the programming structures available in Indigo and built a small web service application. This article concentrates on hosting our sample application on multiple hosting environments.

    The traditional ASMX web services were hosted on Internet Information Server (IIS). However, Web Services Enhancements (WSE) 2.0 extended this model by providing additional hosting options. WSE 2.0 introducedSoapClient and SoapService classes to implement SOAP calls that can execute on non IIS environments. We could implement console applications using SoapClient, Soapservice, SoapSender and SoapReceiver classes utilizing WSE 2.0. The WSE 3.0 extended this concept to Windows Services, COM+ applications. Let's look at Indigo's view on hosing web services.

    Indigo web services can be hosted on multiple environments. These environments can be console applications, IIS (version 5.1 and higher), Windows Services and Windows Activation Service (WAS). Indigo web services execute with same performance criteria with all these hosting environments. Therefore, it is an application design decision to choose the hosting option. Let's discuss all the hosing options available in Indigo.

    Web Hosted Application (IIS)

    The developers are already familiar with the IIS hosting from ASMX days. We can also utilize the high availability and scalable process management features of IIS to our benefit. (We can use features such as process recycling, health monitoring, idle shutdown, and message based activation that comes with IIS). We can utilize an Indigo web service to run on an .aspx page using this mechanism. However, it also has some limitations. (Since IIS is closely coupled with HTTP). Here are some important notes on IIS implementation on different versions of IIS.

    • IIS 5.1 on Windows XP: IIS manages port 80 according to this model. Therefore self hosted indigo applications will not be able to use port 80. We will investigate this scenario later in the article.

    • IIS 6.0 on Windows 2003: IIS 6.0 runs on the worker process / application isolation model. It use kernel mode HTTP.sys to manage port 80 communications. This 'worker process' model is also supported by Indigo. Therefore Indigo web services can co-exist on port 80 with other ASP.NET applications and self hosted Indigo applications.

    • IIS 7.0: There are only few details available on IIS 7.0. This will be in Longhorn and will be built on Windows Activation Services (WAS). WAS is discussed later in the article.

    Self Hosted Applications

    We can host Indigo web service in any app domain that we can instantiates a ServiceHost <T>. (Please refer to this MSDN article if you are not familiar with C# generics). The most simplistic usage of this scenario is the windows console application. The 'console application' is a great teaching and debugging utility. However, we will not use console application in a production environment. (We will create a console application to host an Indigo web service later in this article.) Some practical implementations of self hosted applications are:

    • Windows services: This is a server based model to run the Indigo application as a windows service. We will be able to manage the operations (start, stop and restart) through control panel under this model. We can also configure it to start on machine restart and utilize the rest of windows service's advantages. These web services can run for long lengths of time without being activated by any messages. These can be implemented on Win XP, 2003 and Longhorn platforms. However there are some limitations. (Since they are not hosted on IIS and will not be able to utilize process management and health management features in IIS)

    • Winform / Avalon client: The Avalon (Longhorn UI component) clients will be able to host indigo applications. This will enhance the peer to peer programming model immensely. Currently we need to make a request to an IIS hosted web service to get a reply to implement SOA principals in smart client application. However with Indigo, we can eliminate this extra 'mediator' to enhance performance. (However, we will loose the health and process management benefits of IIS under this model.)

    Windows Activation Service

    This is a 'Longhorn only' feature. This model will provide all the IIS functionality without a web server. Therefore, it will provide process management, health, and activation features. WAS can host HTTP, TCP, IPC and MSMQ solutions. IIS 7.0 will also use WAS capabilities to achieve message based activation for HTTP.

    Let's put this knowledge into action now. In the last article we built an Indigo web service. We went through three steps to build our service. Let's consider step four to be hosting this web service. You may also remember Figure 1 from our last article.

    Figure 1: Scope of Second Article - Hosting Indigo Web Service

    I have grayed out the scope of this article. Let's look at the technical implementation now. (Please refer to the previous article to learn about steps 1 to 3)

    Step 4 - Host the Web Service

    We will build two modules to illustrate hosting options in Indigo. We will host the web services in IIS and will build a self hosted console application. However we will be exposing these two modules on different ports (as described in Figure 1). IIS application will be exposed using port 80. The self hosted application will be exposed using port 8000. We will tackle the IIS hosting option first.

    Host the Web Service in IIS

    Here are the steps,

    1. Save our Notepad web service file as "StockQuoteService.svc". Please note that the extension is ".svc". This is the Indigo standard. Save this file into the root web site directory. (<DriveName>:\inetpub\wwwroot). You can create your own virtual directory as an alternative. If so please remember to change the following line in the web.config file

      <endpoint address="<Virtual Directory Name>" bindingSectionName="wsProfileBinding" contractType="IStockQuote"/>

      Note: Please refer to the StockQuoteService.svc file in the download zip file.

    2. Confirm that the ".svc" extension is mapped to ASPNET process. Here are the steps to confirm this (otherwise IIS will not know how to handle files with ".svc" extension).

      1. Open IIS management console (right click on 'My computer' in Desktop and select 'Manage'. Navigate to "Services and Applications" and then to "Internet Information Server")
      2. Navigate to the correct web site. (This will be "Default Web Site" for our illustration.)
      3. Click on "Properties" and then select "Home Directory" tab.
      4. Click on the "Configuration" button and the "Application Configuration" window will appear. If the ".svc" file extension is associated to iis_aspnet DLL you should have an entry similar to the following image.

        Figure 2: Application Configuration Window

        If the ".svc" extension is missing you need to create a mapping. Please click on the "Add" button and enter the details. The ASPNET process (the aspnet_isapi.dll") can be found at the root directory of the current .NET framework folder. The following image details the add extension screen.

        Figure 3: Add File Extension in IIS

        Click on the "OK" button to register the ".svc" file extension. Now IIS is equipped to handle files with ".svc" extensions.

    3. Save the web.config file to the same directory.

    4. Open a browser window and try "http://localhost/StockQuoteService.svc". If all goes well you should see the following image. (Please remember to change the URL if you decided to copy the files into another virtual directory.)

      Figure 4: IIS Hosted Indigo Service

    That's it! You have hosted your Indigo service on IIS. Now let's look at self hosting scenario.

    Self Hosted Indigo Services

    We need to do bit of coding to self host a web service. The complete code can be found in the download zip file.

    1. First we code the web service implementation. (A step by step approach is available in my previous article.) The code should look like this:

      using System.ServiceModel;
      using System.IO;
      using System;
      using System.Runtime.Serialization;


      [ServiceContract (FormatMode = ContractFormatMode.XmlSerializer)]
      public interface IStockQuote
      {
          [OperationContract]
          int GetCurrentPrice(string stockCode);

          [OperationContract]
          string GetCompanyName(string stockCode);

          int GetCompanyLastClosingPrice(string stockCode);

          [OperationContract (IsOneWay=true)]
          void SetCompanyName(string companyName);

          [OperationContract]
          StockInfo GetCompanyDetails();
      }

      [DataContract]
      public class StockInfo
      {
          private string companyName;

          [DataMember]
          public string CompanyName
          {
              get {return companyName; }
              set {companyName = value; }
          }

          [DataMember]
          private string price;

          private string address;
      }

      public class StockQuoteService : IStockQuote
      {
          private StockInfo stock;

          public int GetCurrentPrice(string stockCode)
          {
              return 10; // remember price is a private field.
          }

          public string GetCompanyName(string stockCode)
          {
              return stock.CompanyName;
          }

          public int GetCompanyLastClosingPrice(string stockCode)
          {
              // This code will not be called by the web service consumers
              // Becuase [OperationContract] is not defined in the interface.
              return 0;
          }

          public void SetCompanyName(string companyName)
          {
              stock.CompanyName = companyName;
          }

          public StockInfo GetCompanyDetails()
          {
              return stock;
          }
      }

      I am running IIS 5.1 on my Windows XP machine. IIS 5.1 execution process consumes port 80 exclusively. Therefore, I will not be able to use port 80 for my Indigo application (this is different in IIS 6.0. IIS 6.0 uses Kernel mode HTTP.sys to manage port 80. Therefore we can share port 80 between Indigo and other applications). I will need to assign a new port for my Indigo application. Microsoft Service Pack 2 for Win XP will block all the ports by default. Therefore, I need to open port 8000 using the firewall settings.

      Open Extra Ports on Win XP - SP2

      1. Select the "Run" command from the Start menu of XP (Start - Run)
      2. Type "firewall.cpl" to invoke the windows firewall.
      3. Navigate to the "Exceptions" tab. Your screen should be similar to figure 5.

        Figure 5: Exceptions Tab of the Firewall

      4. Click and "Add Port" and the following window will appear to enter the port information.

        Figure 6: Add a Port Exception in the Firewall

      5. We will call our exception as "Indigo Samples 8000" and it will be associated to port number 8000. (Click "OK" to finish the process). These actions will grant us action to use port 8000 to execute the self hosted application. Therefore, the clients need to communicate to port 8000 to interact with this service.

       

    2. Now let's add the code for the hosting functionality. We are building a C# console application. Therefore we will put the execution logic in the Main function.

      // Host the service.
      Static class StockQuoteApp
      {
          static void Main(string[] args)
          {

          ServiceHost<StockQuoteService> serviceHost = new ServiceHost<StockQuoteService>();
          System.ServiceModel.WSProfileBinding b = new System.ServiceModel.WSProfileBinding();

          serviceHost.AddEndpoint(typeof(IstockQuote),b,"http://localhost:8000/StockQuoteService/wsEndpoint1");

      We will first create a ServiceHost object that is type of StockQuoteService. Then we create a WSProfileBinding as the preferred binding mechanism. This is followed up by creating the endpoint on port 8000.

      Note: In this scenario I have coded the binding information. We can alternatively read it form the web.config file. (However, I have used this scenario to educate the user on the coding options available in Indigo. The IIS hosting is web config file driven and the self hosting environment is managed by explicit code.)

              // Opening the host sets up the communications infrastructure.
              serviceHost.Open();

              // Exiting the application would bring down the Service.
              Console.WriteLine("Stock Quote web service is running. Press enter to exit");
              Console.ReadLine();
              serviceHost.Close();
          }
      }

      We invoke the Open method in serviceHost to expose the self hosted web service. Then we display instructions on the screen and wait for the user input to terminate the web service interactions. This completes our coding. The next step is to save the file and compile the code with the C# compiler.

    3. Save the file on the hard drive. (I have stored the file at C:\Inetpub\wwwroot\IndigoCode\Self_Hosted directory. The file does not need to reside under wwwroot directory.)

    4. Compile the web service with the C# compiler (csc.exe). You may also want to add references to System.ServiceModel.dll and System.Runtime.Serialization.dll. This is done by utilizing "/r:" option. We would also explicitly create an ".exe" file as the output. (This is defined by the "/target:exe" option. The command should look like this,

      C:\Inetpub\wwwroot\IndigoCode\Self_Hosted>csc /target:exe StockQuoteServiceSelfHosted.cs /r:"C:\Program Files\Microsoft Indigo Preview\System.ServiceModel.dll" /r:"C:\Program Files\Microsoft IndigoPreview\System.Runtime.Serialization.dll

      Your execution environment should be similar to figure 7. (Please ignore the warning messages. They are due to design rules that were used to explain the Indigo basics in the previous article.)

      Figure 7: Compile Indigo Code

    5. Finally we have come to the execution stage. Let's run the StockQuoteServiceSelfHosted.exe and you should see the following outcome.

      Figure 8: Running Self Hosted Indigo Console Application

    We will write clients to these Indigo web services in our next article.

    Code

    You can download the code for both sample applications from here and try it out. The environment information can be found in my previous article. Some notes:

    • Please make sure the IIS hosted application is under wwwroot or a virtual directory. (You need to change the web.config file if you are using a virtual directory.)
    • Please make sure that you set the path for "csc.exe" to compile self hosted application. (The safe path will be to use "Start | All Programs | Microsoft WinFX SDK | Debug Build environment")
    • The self hosted application (the target .exe file) can reside in any directory.

    Reference

    About the Author

    Chris Peiris (.NET MVP 2003, MIT, BComp, BBus) currently works for Avanade as a Principal Development Consultant. Chris has co-authored many books on Web Services, UDDI, C#, IIS, Java and Security topics. He has written many articles, reviews and columns for various online publications. He is also a frequent speaker at professional developer conference on Microsoft technologies. Chris's current passions include Indigo, WinFX, IBM Message Broker and EAI. He lives with his family in Conder, ACT, in Australia. He can be reached at www.chrispeiris.com

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Jun 8, 2005 - Indigo Programming Model
    In the first part of his series on Microsoft Indigo, Chris Peiris examines the basics of SOA, explains how Indigo fits into the picture and the problems it solves. He then introduces Indigo's programming model and finishes by building a sample Indigo web service using the Microsoft .Net Framework 2.0.
    [Read This Article]  [Top]
    Nov 10, 2004 - Business Intelligence with Microsoft SQL Server Reporting Services - Part 3
    Adnan Masood concludes his discussion of Microsoft SQL Server Analysis services and Microsoft SQL Server Reporting services. In the final part, he discusses Reporting Server web services and using custom code in reports.
    [Read This Article]  [Top]
    Jul 8, 2004 - Using IE's Web Service Behavior To Create Rich ASP.NET Applications
    This article explains the features of the IE Web service behavior and shows how to asynchronously communicate with an ASP.NET Web service directly from the client.
    [Read This Article]  [Top]
    Jul 6, 2004 - Using .NET and Excel 2003 To Validate E-Mails
    Calvin Luttrell shows how to validate e-mail addresses stored in Excel 2003 and provides a special function for solving that pesky problem Yahoo! mail servers cause.
    [Read This Article]  [Top]
    Jun 9, 2004 - Modifying Web Services Documentation
    This short article describes a quick and easy way to provide some security to an ASP.NET Web service by modifying its associated documentation file.
    [Read This Article]  [Top]
    Jun 2, 2004 - Kerberos Authentication with Web Services Enhancements 2.0
    Kerberos authentication is the cornerstone of Windows operating system authentication architecture. Web Services Enhancement 2.0 (WSE 2.0) extends Kerberos support to ASP.NET Web services. Chris Peiris explains the support for this new feature in WSE 2.0.
    [Read This Article]  [Top]
    Dec 15, 2003 - Realizing a Service-Oriented Architecture with .NET
    Chip Irek examines the architectural issues and component design issues of building a .NET application in a service-oriented architecture.
    [Read This Article]  [Top]
    Nov 24, 2003 - Consuming Asynchronous Web Services
    Thiru Thangarathinam shows how to use asynchronous Web services, Windows Service applications, server-based timer components and .NET XML API classes to create high-performance, scalable, and flexible applications.
    [Read This Article]  [Top]
    Nov 12, 2003 - Implementing Paging and XSLT Extensions Using XSLT in .NET - Part 2
    Part one showed how to transform XML data into HTML by using an XSL stylesheet from within a .NET application. This part explains how to make use of XSLT Extension objects and invoke a C# class method from an XSL stylesheet.
    [Read This Article]  [Top]
    Nov 5, 2003 - Implementing Paging and XSLT Extensions Using XSLT in .NET - Part 1
    Learn how to transform XML data into HTML by using an XSL stylesheet from within a .NET application, and then implement a paging solution by declaring and supplying paging parameters to the stylesheet.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry



    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers