-Balaji Ramasamy
As I
was working on project, it demanded to run the WCF service during
development which was a challenging task for a novice developer. We
successfully developed the WCF service by implementing the service
contract interface & service behavior class. The above developed
WCF service needs to be consumed by the client application, which
will interact with the end point of the services and provides the
requested response. To successfully build and test the application,
the WCF service needs to be exposed, which will be consumed by client
application with the help of svcutil tool. Since the WCF service is a
class library, we can’t run it as service.
So
here comes the question of how to run the WCF service to generate and
consume the service proxy in my client windows application during
development. We are very much aware of hosting the service via the
console application, winforms application, windows service, IIS, WAS
to host the WCF service. Initially we ran the WCF service as windows
service, assuming it as a right decision, which resulted in multiple
installation and un-installation of windows service. To avoid such
painful process during development, we planned to run the WCF service
in console application. But how to run in console application? Here
comes the ServiceHost runtime, which will enable to the WCF service
to load the service class, set up the endpoints and channel
listeners, which provides your service class an “ecosystem” to
live and operate in.
You
can either instantiate a ServiceHost class yourself in a console app,
a Windows service, or even a Winforms app, and thus make your WCF
service class available to the outside world - or you can delegate
that work to IIS or WAS. Even IIS or WAS will use a ServiceHost to
host your WCF service - they just do it automatically behind the
scenes.
The
following is the console application code to run the WCF service
internal
class
Program
{
private
static
void
Main(string[]
args)
{
try
{
StartService();
Console.WriteLine("Service
is running...");
Console.WriteLine("Press
key to end...");
Console.ReadLine();
StopService();
}
catch
(Exception
ex)
{
//Log
the Exception
}
}
internal
static
ServiceHost
myServiceHost = null;
internal
static
void
StartService()
{
try
{
myServiceHost
= new
ServiceHost(typeof([namespaceName.ServiceBehaviourClassName]));
myServiceHost.Open();
}
catch
(Exception
ex)
{
//Log
the Exception
}
}
internal
static
void
StopService()
{
try
{
if
(myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
catch
(Exception
ex)
{
//Log
the Exception
}
}
}
And the Application
configuration includes be the following
<system.serviceModel>
<services>
<service
name="[namespaceName.ServiceBehaviourClassName]"
behaviorConfiguration="metadataSupport">
<host>
<baseAddresses>
<add
baseAddress="[EndPoint
Address]"
/>
</baseAddresses>
</host>
<endpoint
address=""
binding="[Binding
Name]"
contract="[namespaceName.ServiceContractName]"
"/>
<endpoint
address="mex"
binding="mexNamedPipeBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior
name="metadataSupport">
<serviceMetadata
/>
<serviceDebug
includeExceptionDetailInFaults="true"
/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Note: Application
Configuration may vary from one binding to another binding. Please
check Microsoft MSDN site for more details about bindings and its
related configuration
Now run the service and
its ready to consume in the client application during development
stage of our application
References: