Indigo “Hello World”
using
System;
using
System.ServiceModel;
namespace IndiHello
{
[ServiceContract]
public
class Hello
{
[OperationContract]
public string SayHello(string name)
{
return "Hello " +
name;
}
}
class
Program
{
static void
{
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.