// Open the host and start listening for incoming messages.
serviceHost.Open();
// Keep the service running until the Enter key is pressed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press the Enter key to terminate service.");
Console.ReadLine();
}
}
If you now run this application, you will find that the host is alive in memory, ready to take
incoming requests from remote clients (see Figure 25-6).
Figure 25-6.
Our host, ready for external calls via basic HTTP binding
Host Coding Options
Currently, we are creating our ServiceHost using a constructor that simply requires the service’s
type information. However, it is also possible to pass in an array of System.Uri types as a construc-
tor argument to represent the collection of addresses this service is accessible from. Currently, the
address is found via the *.config file; however, if we were to update the using scope as so:
using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService),
new Uri[]{new Uri("http://localhost:8080/MagicEightBallService")}))
{
...
}
we would be able to define our endpoint as so:
binding="basicHttpBinding"
contract="MagicEightBallServiceLib.IEightBall"/>
Of course, too much hard-coding within a host’s code base decreases flexibility, so for the pur-
poses of this current host example, I’ll assume you are creating the service host simply by supplying
the type information as we did before:
using (ServiceHost serviceHost = new ServiceHost(typeof(MagicEightBallService)))
{
...
}
One of the (slightly frustrating) aspects of authoring host *.config files is that you have a num-
ber of ways to construct the XML descriptors, based on the amount of hard-coding you have in the
code base (as you have just seen in the case of the optional Uri array). To show yet another way to
author *.config files, consider the following reworking:
C H A P T E R 2 5
■
I N T R O D U C I N G W I N D O W S C O M M U N I C AT I O N F O U N D AT I O N
890
8849CH25.qxd 10/16/07 10:51 AM Page 890
-->
binding="basicHttpBinding"
contract="MagicEightBallServiceLib.IEightBall"/>
In this case, the address attribute of the element is still empty, and regardless of the
fact that we are not specifying an array of Uri objects in code when creating the ServiceHost, the
application runs as before as the value is pulled from the baseAddresses scope. The benefit of stor-
ing the base address in a ’s region is that other parts of a *.config file (such
as MEX, described shortly) also need to know the address of the service’s endpoint. Thus, rather
than having to copy and pass address values within a single *.config file, we can isolate the single
value as shown here.
Do'stlaringiz bilan baham: |