Part 25 Self hosting a wcf service in winforms application

Part 25 Self hosting a wcf service in winforms 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 windows forms application. Hosting a wcf service in windows forms application is very similar to hosting it in a console application. We discussed hosting a wcf service in a console application in Part 24. We will continue with the same example we worked with in Part 24.

The advantages and disadvantages of hosting a WCF service in a windows application is the same as hosting the wcf service in a Console application.
Closed Caption:

r-25 of WTF video series in this video
we'll discuss self-hosting a WCF service
in windows forms application this is
continuation to part 24 so please watch
x 24 before proceeding hosting a WCF
service enables forms application is
very similar to hosting it in a console
application we discussed hosting a WCF
service in their console application in
part 24 in this video we'll be working
with the same example that we worked
with in the previous session so we want
to design a windows form that looks like
this with the start and stop button when
we click the start button we want the
service to start and when we click start
button we won the cell is to stop the
first let's go to design a form that
looks like this
let's flip the visual studio so this is
a project that we worked with in the
previous session to this solution let's
go ahead and add a new windows forms
application so click windows and install
and installed templates windows forms
application and let's name the project
as Windows host click ok so this should
add event those forms application now we
need to design the user interface that
looks like this so let's go ahead
drag-and-drop to button controls and a
label control so we want two buttons and
a label so here we have the label button
1 + button to and let's change the
properties of button1 first of all let's
call let's set the name of the button to
be TN start and then the text to
start along the same lines let's set the
name of button 22 BTW and stop and the
text on that to stop and let's delete
this default text within the label that
says label1 and let's set the name of
label 12 lbl message and then let's also
set formed size 2 10 points and let's
also turn this bold flag to true alright
so we're done
design the form now in order to host the
WCF service first of all we need to add
a reference to the WCF service so let's
go and do that within this Windows host
project so i click on preferences folder
add a reference and we want to add a
reference to hello service project which
has a WCF service so add that reference
and then we also need to add a reference
to the WC of assembly which is system .
service model so let's go ahead and add
that as well
all right now when we click the start
button that's when we want the WCF
service to start okay
and one more important thing that we
need to do is to add the configuration
file the application configuration file
which is going to contain the
configuration for our WCF service so
first of all we need to add that
application configuration file so add
new item and we want to add application
configuration file and now here we need
to specify the configuration you know
the endpoints the base addresses know
whether we want to exchange metadata or
not and if you remember we have
specified the configuration that in the
app config file of the console
application the previous session so we
are going to copy this conflict from
there to the apt conflict file for
Windows host project right
so we have the configuration as well
right now so let's get the form double
click the start button to generate a
click event handler and then now let's
bring in system . service model
namespace so at the class level that is
at the farm level
let's go ahead and create a private
variable of type service horse this
domain last that we basically use you
know to host or WCF service so let's
call it hosts and when we click the
start button
let's create a new instance of this
service host and when we create a new
instance of the service course we need
to specify the type of service that we
want to host and it is nothing but a low
service which is present in a low
service namespace and what we want to do
we want to open the communication
channels so invoke the open method of
service host object
ok so at this point we have the service
host open okay and what else do we need
to do now when we click the start button
you know the Saudis should start and
automatically the button should be
disabled
ok and start button should be enabled
and we want to display this message so
we started within the label control so
let's go and do that so BTN start .
enable equals falls and let's set button
stopped enabled to true and within the
label control
we want to say service started okay now
let's actually copy this code and then
paste it within the constructor of this
form one so what is this going to do
when we run the windows forms
application obviously this poem one will
be loaded so when the constructor of
this form one is called it automatically
the service is going to start so
whenever we run the project you know
even at that point of time we want the
service to start automatically so we're
calling the code within the constructor
which is going to start the service by
default when we run the you know program
alright similarly when we stopped when
we click on the start button what should
happen we should be able to stop the
service and to stop the service all you
do is invoke the close method which is
going to close the communication
channels
ok so once the services stopped what you
need to do need to enable start button
and we need to disable start button and
here within the label they say
service stopped ok so very simple code
here to start and stop a WCF service and
this cord you can find on you know the
same code here on the slide
ok and then if you notice the code here
there's one more even-handed method
called form1 underscore farm close so
when is this even handle in work
whenever somebody close this day the
windows to this host what we want to do
in that cases and only want to close the
communication channels so we're going in
with the hosts are closed so basically
we're going to have the same piece of
cord within form1 I mean basically
formed closed even a square so let's go
ahead and generate the even handler for
forum post and in order to do that click
on the form anywhere and then get to the
properties of the phone by pressing f4
and then click on this even second and
then there within the u.s. you should
see form a closed event
ok double click on the event so the
even-handed
should method should be automatically
generated and hear what we're going to
do is copy and paste this chord and then
since the application is closed we don't
really have to set the text in the label
control because nobody gets to see that
anyway so let's go and delete that
alright so we're done now let's set of
Windows host has this type of project
and then run this program by pressing
ctrl f5 so look at that it says this
started
ok and look at that start button is
disabled start button is enable when we
click stop it is service top start the
service so the host itself is working as
expected
let's quickly test if the WCF services
working as expected so in the previous
session we have also created a client
project which is called Windows client
ok so here the Vandals services already
running ok so let's go ahead and run the
client right now okay and let's pass you
know name is pledging click get message
this you know the WCF service should
respond and it should work in the same
bay
okay so look at that we get the response
back let's say hello protein-1 to get
message we get the message back
ok now let's go to the service host and
stop the service
ok so basically you know it's taking a
little bit of time here to stop the
service it's going to gracefully stop
this cell is there any outstanding
requests
it's going to service them and then stop
gracefully let's go back to the client
and then click get message now obviously
we will get an exception
you know the socket connection was
aborted and if you look at details
basically you know it should mention
somewhere the service this is not able
to handle the requested has refused it
ok that's basically because we have
stopped the service so let's go ahead
and start the service
and click get message and see what's
going to happen
look at that instant get an exception
okay and why is that you know because
look at this the services started but we
still get an exception
ok that's basically because look at what
it says the communication object cannot
be used for communication because it is
a faulted state so the client proxy has
actually been faulted let's actually
look at that you know by debugging so
here the service has started now let's
go ahead and run the vendors client in
debug more and then they break point
right here within the button click event
ok now when we say let's bring in the
service hostess one so here we have the
service costs and the client now
obviously when we pass name and then
click get message you know we are going
to get the response back so we have the
response back that's going to stop the
syllabus so it takes a second to close
the service and then once the services
stopped when we click get message we
will get an exception so here and we
plus a 5 we get an exception and that's
understood and that's basically because
you know the service has been stopped
ok let's close that
that's actually stop debugging so let's
start the service now here what we need
to do is we need to check the state of
the client so if client . state equals
communication state . fall Ted ok if it
is fall Ted then what we want to do is
we want to create a new instance of the
client a proxy because of the client
proxy is for all that you cannot use the
same instance of the proxy class for
communicating with your WCF service so
if it's in a folded state we're creating
a new instance of the proxy and let's
also try
I mean wrap all this inside a try-catch
block so that if there is any exception
we will have a chance to display that
within the label control so let's catch
the exception and display that within
the labels label to the text equals e .
message
alright so with all these changes let's
go ahead and other services already
learning
let's go and run over windows client
the builders let's look at what i do is
a local variable named he cannot be used
ok that's because you already have
another object in the same name
so let's call this exception TX
alright let's go and this once again ok
so here the services running let's get
to the client back to let's disable this
break points so let's pass the name
click get message so it should work hope
regime that stop the service now it
takes a second to stop the service and
let's go click get message look at that
we get an error
ok and that's because the services talk
now let's start the service and then
let's click this get message look at
that now it works as expected
ok so basically the previous time it
didn't work when we start and stopped
and started the service that's because
we are using the same instance of the
proxy class to communicate with the WCF
service well it has already been faulted
ok so here what i'll be doing we're
checking the client state if it is
folded create a new instance of the
proxy class which will then be used to
communicate with the WCF service all
right
the Atlantis and disadvantages of
hosting a WCF service in abundance
application is very much similar to you
know hosting the WCF service in a
console application not the only
difference between a console application
windows forms application is that within
windows forms application you have a
graphical user interface basically to
start and stop your services but the
console application we don't get that
but in other cases to be really use
these forms of you know hosting the WCF
service and real-world now these are
ideal for in our testing your WCF
services but not for you our production
live WCF services in a later video
session we'll see
how to actually host the WCF service in
a windows service you know which is one
of the options for live WCF services and
the most common way today is actually
hosting the WCF services within is where
we have in our windows activation
services enabled we will discuss both of
these options in order upcoming videos
that's it for today thank you for
listening have a great day

Video Length: 15:30
Uploaded By: kudvenkat
View Count: 31,747

Related Software Products
Application As Service
Application As Service

Published By:
ELTIMA Software GmbH

Description:
Application as Service is an advanced system utility which lets you run any application as Windows 2000/XP/2003 Service. You can easily configure Application as Service using its intuitive GUI interface or command line configurator directly from your software. Application as Service features very low CPU usage, custom environment variables, monitoring and detailed logging as well as many other features. You can use Application as Service software to: Launch any application as Windows ...


Related Videos
Creating and Using a RESTful Web Service in Application Express 4.2
Creating and Using a RESTful Web Service in Application Express 4.2

The purpose of this video is to show you how to create a RESTful web service declaratively using Oracle Application Express SQL Workshop with the Application Listener, and then consume that same service using Oracle Application Expresses support for declaratively creating clients based on RESTful web services. hr / bClosed Caption:/b the purpose of this video is to show how to create a restful webservice declaratively using application Express sequel ...
Video Length: 07:34
Uploaded By: Oracle Learning Library
View Count: 57,707

Part 24   Self hosting a wcf service in console application
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. br ...
Video Length: 23:06
Uploaded By: kudvenkat
View Count: 46,223

JBOSS Application Server 7.1.1 on Windows 8
JBOSS Application Server 7.1.1 on Windows 8

Walk through of Installing JBOSS Application Server 7.1.1 on Windows 8 as a Service and connecting to Internet Information Server 8 (IIS8) via AJP protocol and BonCode Connector. We also walk through on how to deploy sample WAR archive (Railo http://getrailo.org) on JBOSS AS. BonCode connector download site has changed: Please go to http://www.boncode.net and click the BonCode Connector Link hr / bClosed Caption:/b hello and welcome to Keith's recording of ...
Video Length: 19:14
Uploaded By: boncode
View Count: 43,824

28c3: Effective Denial of Service attacks against web application platforms
28c3: Effective Denial of Service attacks against web application platforms

Download hiqh quality version: http://bit.ly/rKwW58 Description: http://events.ccc.de/congress/2011/Fa... Alexander 'alech' Klink, Julian | zeri: Effective Denial of Service attacks against web application platforms We are the 99% (CPU usage) This talk will show how a common flaw in the implementation of most of the popular web programming languages and platforms (including PHP, ASP.NET, Java, etc.) can be (ab)used to force web application servers to ...
Video Length: 56:57
Uploaded By: 28c3
View Count: 32,090

Web Service with DB   Java Web Application   netbeans 7 3   Java DB
Web Service with DB Java Web Application netbeans 7 3 Java DB

Web Service with DB - Java Web Application - netbeans 7 3 - Java DB http://ttaiit.blogspot.com
Video Length: 14:11
Uploaded By: Dev Bananaer
View Count: 30,811

Part 40   How to retrieve the sessionid in WCF service and in the client application
Part 40 How to retrieve the sessionid in WCF service and in the client 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 retrieving the sessionid in WCF service and in the client application This is continuation to Part 39. Please watch Part 39 before proceeding. We will work with the same example, that we worked with in Part 39. In order to send ...
Video Length: 10:17
Uploaded By: kudvenkat
View Count: 25,198

Publish MVC API Web Service to IIS
Publish MVC API Web Service to IIS

MVC 4 Web API Publish to IIS Step by step: - Publish your web application - File to IIS - Add a website in IIS, point the physical path to published folder - Change Application Pool Identify to LocalSystem for access database - Test in browser :) hr / bClosed Caption:/b Right-click on project and select Publish.. Select profile: File to IIS Publish method: File System Browse your Target folderbr ...
Video Length: 01:57
Uploaded By: Son Nguyen
View Count: 24,338

Using a Security Token Service with an ASP.NET Application | Pluralsight
Using a Security Token Service with an ASP.NET Application | Pluralsight

SharePoint 2010 Security Part 2 | http://www.pluralsight-training.net/m... Want to learn how to hook up a Windows Identity Foundation based security token service to an ASP.NET 4.0 application? In this video excerpt from Sahil Malik's new course SharePoint 2010 Security Part 2, you'll see how to do just that as a precursor to establishing your own sophisticated claims based security for a SharePoint 2010 site. In the full course Sahil covers other topics such as using the WIF SDK, ...
Video Length: 11:46
Uploaded By: Pluralsight
View Count: 20,808

CES2011: Sony Qriocity Service with iPad remote app
CES2011: Sony Qriocity Service with iPad remote app

CES2011: Sony Qriocity Service with iPad remote app hr / bClosed Caption:/b just alright so I'm here at CES 2011 Sony booth with some pretty cool new stuff this is actually I'm an example one in the new applications Milly Dowler if you have an Apple iPad iPhone and iPod Touch and even if you have a Google Android phone like an Xperia x10 you will be on the download this app for his wellness allow you to ...
Video Length: 01:07
Uploaded By: Sony Canada Training
View Count: 18,905

Service Level Agreements SLA Application Performance Management APM
Service Level Agreements SLA Application Performance Management APM

During this webinar you will learn why service level agreements (SLA) are broken today and how you can close the expectation and satisfaction gap between information technology (IT) teams, vendors and the all important users of your enterprise applications. Learn about: •The expectation and satisfaction gap that enterprise application users have with the service level agreement metrics reported by information technology •What is this expectation and satisfaction gap, why does it ...
Video Length: 44:27
Uploaded By: ExpertView2012
View Count: 18,177

Copyright © 2025, Ivertech. All rights reserved.