February 9, 2005
@ 05:56 AM

using System;
using System.ServiceModel;

namespace IndiHello
{
      [ServiceContract]
      public class Hello
      {
            [OperationContract]
            public string SayHello(string name)
            {
                  return "Hello " + name;
            }
      }

      class Program
      {
            static void Main(string[] args)
            {
                  ServiceHost<Hello> host = new ServiceHost<Hello>(new Uri("http://localhost/hello"));
                  host.AddEndpoint(typeof(Hello), new BasicProfileHttpBinding(), "ep");
                  host.Open();
                  Console.WriteLine("Press ENTER to quit");
                  Console.ReadLine();
                  host.Close();
            }
      }
}

I am told that I can talk, so I do ;-)  Here’s a simple Indigo server. If you looked at the PDC 2003 Indigo bits, you will notice that the programming model changed quite a bit. I think that in fact, every single element of the programming model changed since then. And all for the better. The programming model is so intuitive by now that I am (almost) tempted to say “Alright, understood, next technology, please”.

So up there you have a class with an implicit service contract. An explicit service contract would be a standalone interface (that’s the proper way to do it, but I wanted to keep the first sample simple) with a [ServiceContract] attribute. Here, [ServiceContract] sits right on the class. Note that the class doesn’t derive from any special base class. Each method that you want to expose as an endpoint operation is labeled with [OperationContract]. These and a set of other attributes (along with a bunch of options you could set, but which I am not doing for the moment) control how the class contract is exposed to the outside world via Indigo.

In the Main method, you have a ServiceHost, which hosts the service (the class is parameterized with the implementation type) and which is initialized with the base-adress at which the service shall be hosted. The base address here is “http://localhost/hello” and with that maps into the namespace of http.sys at port 80. The endpoint can exist alongside any IIS-hosted websites, even though this particular app is hosted in its own little console-based app.

Into this host, I map the service contract with a BasicProfileHttpBinding() to the endpoint address “ep”, which means that messages to that particular service that flow through HTTP using the WS-I Basic Profile 1.0 shall be directed to the “http://localhost/hello/ep” endpoint. Once I have a binding in place (that could also be done in config), I Open() the service and the service listens. Once I am done listening, I Close() the service.

Isn’t too hard.

Friday, February 11, 2005 5:35:58 AM UTC
Neat...

To me this seems like a nice combination of .NET remoting simplicity (just create a class based on MarshalByRefObject and register it in a host process) and WebServices connectivity (I assume that the demo service is invoked by sending a SOAP message, right?)

I saw the code snippet where you presented the [ServiceContract] applied to an interface. Is it simply enough to create a class which implements the interface and register it in the same way as above or do you need to "decorate" the implementing class as well?
Tadej Mali
Sunday, February 13, 2005 5:00:47 PM UTC
Easy enough - couple of questions though:

In the example above does the Console app host the HTTP logic or does it delegate to IIS in some way? What happens if IIS is running on port 80 and you have this Console app also listening on Port 80?
Comments are closed.