Part 24 Self hosting a wcf service in console application
Link for code samples used in the demo
http://csharp-video-tutorials.blogspo...
Link for all dot net and sql server video tutorial playlists
http://www.youtube.com/user/kudvenkat...
In this video, we will discuss hosting a wcf service using a console application. Hosting a wcf service in any managed .net application is called as self hosting. Console applications, WPF applications, WinForms applications are all examples of managed .net applications.
Advantages of self hosting a wcf service in a console application
1. Very easy to setup. Specify the configuration in app.config file and with a few lines of code we have the service up and running.
2. Easy to debug as we don't have to attach a separate process that hosts the wcf service.
3. Supports all bindings and transport protocols.
4. Very flexible to control the lifetime of the services through the Open() and Close() methods of ServiceHost.
Disadvantages of self hosting a wcf service in a console application
1. The service is available for the clients only when the service host is running.
2. Self hosting does not support automatic message based activation that we get when hosted within IIS.
3. Custom code required.
In general, self-hosting is only suitable during the development and demonstration phase and not for hosting live wcf services.
Closed Caption:
this part 24 of WTF video series in this
video we'll discuss self-hosting a WCF
service using a console application this
is continuation to part 23 so please
watch for 23 before proceeding hosting a
WCF service in any managed to dart
application is called self-hosting
console applications
WTF applications windows forms
applications are all examples of manage
the dart applications
let's create a simple WCF service and
then host it using the console
application let slip the visual studio
let's create a new project and we want
to create a class library project and
let's call it
hello service click ok let's delete this
class 1 dot CSS file that's
auto-generated and to this project we
want to add a WCF service and let's name
the service as hello service click Add
so that should add a reference to system
. service model assembly and then
generate these two files for us
hello service . CS I hello Sylvie stop
CS so this file contains the service
contract
let's get it off this do work method and
you know let's make this method actually
return a string back and let's call it
get message and we want this method to
take a string parameter and let's call
the parameter name so this I hello
service contract has got a single
operation contract get message and this
service contract is implemented in this
file hello service . CS so let's go and
delete this do work method and then we
want to implement this interface and the
easiest way to do that is to simply
click on that I hello service and then
plus control . we get the option to
implement the interface will let select
this option and this should
automatically generate and method stubs
for us and all this method is going to
do is can catenate the word hello with
whatever is coming into
mattered you know using this parameter
alright so we here we have a simple WCF
service we are not going to specify any
configuration within the WCF service
itself so we can get rid of this app
config file within the service so let's
go ahead and delete that and now we want
to host this WCF service using a console
application so here we are self-hosting
the WCF service so to this solution we
need to add a console application so i
click on that add a new project and we
want to add a console application and
let's call it hosts click ok so this
should add a console application the
first thing that we need to do here is
to add a reference to this project hello
service because we want to host that
service and then we also need to add a
reference to system . service model
assembly that's the WCF assembly that
contains all the important classes so
let's go in that those references a
difference
first let's go ahead and add a reference
to system . service model assembly and
let's add a reference to hello service
project that sounds ok now the next step
is to specify the configuration for the
WCF service that we want to host so
let's add an application configuration
file so by default app config file
should be added to this host project now
we need to read the configuration here
for this service we basically need to
expose an end . we need to enable meta
data exchange so that the clients can
generate the proxy classes using the
metadata ok now we can write that code
in literally hand type this within this
configuration file or we can make use of
WCF service configuration editor tool
now when I click on this app that
conflict file we should actually get an
option here edit WCF configuration but
for some reason visual studio doesn't
show that and I think that's a bug in
visual studio
especially i'm using visual studio 2008
for that option to show up we have to
open that contact file using the WCF
service configuration editor at least
once and after that when we right-click
you know comes in the context menu will
have that option so what I'm going to do
now click on tools and then here you
have the option WCF service
configuration editor select that option
and the tool should fire up and then
we're going to use that tool basically
2.2 this file and then open that using
that tool so that should be launched in
just a minute
ok so let's go and open the conflict
file and the conflict file is present in
C Drive hello service and with you know
this hello service for dissolution
folder and within that we have this host
project which contains the app config
file so now it should load you know the
contents of this apt contract file now
we want to create you know a new server
so services folder is selected select
this option create new service and what
is the service type so that service type
is present in C Drive hello service so
within the bin debug directly now we
didn't compile the solution at least one
so that's the reason why we don't have
the assembly generated so let's cancel
these things and then let's build our
solution ones so build succeeded
now let's go ahead and right click on
app config file and notice that we get
the option now edit WTF configuration
select that option click on file open
and we want to open the contract file
which is present in hollow service host
ab conflict so now let's create a new
service so the service type assembly is
present in the sequel and hello service
folder within that hello service project
folder within been debug we have hello
service to dl so let's open that and
within that we have hello service .
hello service that's the service that we
want to explore so let's open that so
that's this is ty let's go ahead and
click next now it's asking us the
contract you know the contract is
nothing but hello service start
I hello service that's the service
contract that for implementing click
next and then what is the binding that
we want to use we want to use a tcp
based communication
ok so TCP binding basically so let's
select that and click Next
the next important thing is the address
for the end . now here you can either
specify the fully-qualified Adler's are
you can use a valid email address and
then specify a base address so let's use
a reality wedges now so I lose service
is going to be delayed your address in
order to reach this service click next
now here we have the videos that we have
provided so this is the service type and
the contract is hello service that I
hello service
we're going to use neck t CP binding and
we're not going to configure customize
the binding in anyway so the binding
configuration at the moment is default
which is an empty string and then the
end . address basically hello so is that
relative address so let's click finish
so at this point it has added you know I
service endpoint for us
ok now we need to specify the base
address as well because we are using the
relative address here so it especially
the base address click on host and then
here we can add these addresses so
basically we need to add to base
addresses one base addresses for the
service endpoint itself and the other
base addresses is for the metadata to be
available so first let's add a base
address for metadata HTTP colon for /
for / localhost colon let's have the
metadata available at port 8080 so
that's the base address for metadata
exchange now let's add a base address
for the service endpoint and that's
going to be net . tcp calling for / for
/ localhost colon 1890 that's deport
so let's add that so now for this
service you know the actual end . this
is going to be met . tcp calling for /
for / localhost colon 1890 /hello
syllabus
ok so we have the base addresses and we
have specified the you know service
endpoint as well and another important
thing that we need to do is we need to
enable behavior you know service
behaviour basically to enable metadata
accident so that the clients can
generate proxy classes and in order to
do that
expand advanced folder and here you have
service behaviour option so we want to
create a new service behavior
configuration select that option and we
want to add a service behavior and what
is that service behavior for to Eckstein
service metadata so we're going to add
that and then once we select the service
metadata here that this option actually
be gettin able we need to turn that to
true and then let's give this behavior
and name instead of new behavior so
let's actually call this max behavior
ok so we have a service behavior
specified but then we need to associate
this behavior but this service so let's
select that service and then here within
the behavior configuration select max
behavior that we have specified that's
that we're all done so close this it
will ask us to save the changes click
yes
so now when i click yes
look at that you know it basically
writes the entire configuration for us
and sort of three handwriting it
ok so basically here within system . so
this model we have specified behaviour
so we have the service behavior maybe
are enabling the you know meta data
exchange and then here we have
associated this behavior with this
service using this behavior
configuration attribute the name of the
services this one and then here is the
end . that is the address
that's the binding and that's the
contract the ABC of an endpoint is
specified and then here we have the base
addresses one for the service endpoint
itself the other 100 for the metadata
exchange
ok so we have specified the required
configuration the next step is to write
code to open up the communication
channels
ok so let's get rid of these namespaces
that we don't need we need system .
service model namespace so let's go and
bring that in and then within the main
method we need to construct an instance
of service host so let's create service
host instance and let's call it hosts
and then we are creating a service horse
you know we need to specify the type of
the service step your hosting and in
order to get the type of the service we
use the type of keyword and the service
that we are hosting is hello service .
hello service so that is the service we
are going to host and then host . open
ok so basically what is this method
going to do this method is going to look
at the conflagration that we have
specified within the app . conflict five
you know what binding we have at what
address the services available
what contract has implemented its going
to read all this configuration from the
configuration file and then open up the
communication channels for the clients
to consume more WCF service
ok so let's go ahead and write a message
to be consoled stating that you know the
service hostess started so let's say
host started at whatever date and time
so daytime . now . to string
and to keep it alive
let's use consoled or Treat line alright
with all these changes let's go ahead
and run you know the host project by
pressing ctrl f5 now we get an error
that's basically because at the moment
hello service which is a class library
project is set as the starter project so
that's the reason we need to set up a
whole-step this type of project and then
when the solution by pressing ctrl f5 so
this should load in our host so when who
start open is called it's going to open
up the communication channels now to
test the service is actually working we
can fire up a browser and then browse to
this reality madras HTTP call on
localhost colon 8080 ok instead of that
lets go and create a client for WCF
service so let's fire up another
instance of visual studio and then
create a new project
let's use windows forms application is
the client
ok so windows forms and let's call this
windows client
click OK
so now we need to design a farm so let's
get to the toolbox we need to label
controls a text box and a button control
so let's get a button control and we
needed to label controls and then a
textbox control and that silence this so
we have the text box there this label is
going to display the result for us and
this label is just going to say name so
that the user can enter a name and let's
get to the properties of this button and
then let's specify the that's actually
sort this by alphabetical order
let's call this get message the text on
the button and then let's get it off
default text and label to and let's
change the text on label 12 something
like name
alright so when we click get message
that's when we want to invoke the WCF
service
ok so let's go to the solution explorer
and then add a reference to a WCF
service so right-click on the references
folder a disservice reference and if you
remember the base address that we have
specified for metadata exchange is
localhost colon 8080 so let's go to
specify that address within you know the
address text box click go
so now it should identify that
hello service that's running on the
machine so the host is running so the
communication channels are opened so the
client should be able to find that
service and then let's specify the names
PS hello so this year
click ok so this should you know
generate the proxy classes based on the
metadata ok so you know the class level
let's go ahead and create
an instance of the proxy class hello
service client and you know when the
form constructor is called let go ahead
and create a new instance of the proxy
class and then within the button click
event all we're going to do is in walk
the getmessage method so get message and
we want to pass that the name that is
going to be typed into this text box and
then obviously this get messages going
to return a string back which we want to
display and label to control so label to
the text equals whatever comes back from
the WCF service
alright but all that let's go ahead and
run this client as well so + ctrl f5 so
let's enter for example protein click
this get message button and we should
get a response back from the WCF service
look at that hope regime and hello
regime to alright so here in this video
we have demonstrated in operating a
simple WCF service and self-hosting
that's the list using a console
application now let's understand the
advantages and disadvantages of
self-hosting a WCF service so what are
the advantages of self-hosting WCF
servers now first of all it's very easy
to set up all you need to do to
self-hosted WCF service is to specify
the configuration you know that's
required for the service within the app
conflict fire and then write a few lines
of code you know to have the service up
and running and all we have done you
know to host the service is you know we
have constructed an instance of the
service host specify the type of the
service and then you in work and are
open method which is going to read the
configuration from the application
configuration file and then open up the
come you know communication channels for
the clients to consume or WCF service
ok so it's very easy to set up basically
and it's easy to debug as well because
you know we don't have to attack a
separate process that hosts the WCF
service
you know if we have host of other WCF
service invento service in a windows
service then to be bucket we have to
attach that windows service in order to
the debugger but you know when
self-hosting we don't have to do that
what i mean by that is let's actually
run board service and the client in
debug mode so let's run this hello
service so we are running the service
within debug mode right now let's go
ahead and do the same thing with our
windows client and look at this now if i
want to debug let's type and plug-in for
example if I want to deal with the WCF
service all i need to do is you know run
the service within debug mode and then
stick in a break . okay so we actually
want to debug the WCF service so let's
go to the service implementation so
let's go to stick in a break . yeah and
then click this get message button so
look at that you know the debugger you
know drops into this one and I can + f10
inspect what is coming into this
variable look at that protein so it's
really easy to debug you know when we
have the WCF service self-hosted and
another greatest advantage with
self-hosting is it's going to support
all bindings and transport protocols for
example if we host WCF service within is
then I supports only http-based bindings
non-hd based bindings are not supported
but that self-hosting we get support for
all the bindings and all the transport
protocols
ok and it's very flexible to control the
lifetime of the servers to the open and
close methods of service horse
okay now in order to make the service
host available for public consumption
all we have done as in what the open
method on the service host instance if
you want to close this service horse all
you need to do is host . close we'll
discuss this India you know when we
discussed self-hosting AWC of servers
are using vendors happened
asian will do that in our next video
session
ok so those are the advantages of
self-hosting WCF service using the
console application there are
disadvantages as well so what are the
disadvantages the disadvantages are the
service is available for the clients
only when the service host is running ok
so if you stop this service host and
then if the client tries to basically
think it time down so let's detach this
debugger so basically if the service
host is not learning and then the client
? the WCF service obviously and we're
going to get a communication exception
so if you want the WCF service to be
available then the service host has to
be rounding ok and self-hosting does not
support something called automatic
message based tax activation that we get
then be hosting WCF service within is ok
so when we have hosted the WCF service
within is you know we don't have to
manually started in on the is always a
passive listener so whenever a message
comes in this service is going to start
automatically so it's an automatic
message based activation we don't get
that with self-hosting we have to
manually start and stop the service host
and obviously can be self hosted WCF
service we need to cut right
custom cord ok but in general
self-hosting is only suitable during the
development and demonstration face you
know when we develop the WT of servers
but in real time for life WCF services
we don't really host them using you know
either console application of Windows
application we don't really sell force
WCF service in real time
ok in real-time be the host WCF services
you know in windows services are you
know using the is with Windows
activation so this is enabled will
discuss all these options and our
upcoming videos
that's it for today thank you for
listening
have a great day
Video Length: 23:06
Uploaded By: kudvenkat
View Count: 46,223