Part 27 Hosting a wcf service in a windows service

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part 27 of wucf video series in this video well disk is hosting a WCF servers in a Windows service this is continuation to part 26 so please watch for 26 before proceeding we'll be working with the same example that we worked with in part 25 so let's flip to visual studio so we're going to host this hollow service which is a WCF service in a Windows service so first to this solution let's go ahead and add a Windows service project select windows under install the templates and then choose windows service template and then let's call this Windows service host click OK and now we want to host this hollow service you know within this window service and in order for us to be able to do that we need to add a reference to that hollow service project so right click on references folder add a reference and then let's select hello service and we also need to add a reference to system dot service model assembly so let's go and add system dot service model and one more thing that we need to do is add the application configuration file so right click on windows service host add a new item and we want to add application configuration file now here within this config file we need to specify the configuration for our WCF service which we have done in the console application and windows forms application in the previous sessions of this video series so in the previous sessions we have discussed hosting this hello service within a console application as well as the windows forms application so let's copy the configuration from one of the apt or config files of those projects and then paste that configuration within this app that config file in the windows service project now let's change the name of the service dot service one dot cs2 hollow windows service and then once we press enter it's going to ask us if we want to change all the references of that service one click yes and then double click on this hello windows service dot CS file and then right click on the designer and select this option view code so here we need to write code to host our WCF service so first of all let's go ahead and bring in system dot service model namespace and then at the class level we are going to create an instance of our service host class and let's call it host so on start so when the service starts what we want to do we want to have our WCF service started as well so we are going to create a new instance of service host and then specify the type of the service that we want to host we want to host hello service and in order to open the communication channels all we need to do is invoke the open method on the instance of the service host class and we also need to implement on stop method so when we stop the service what should happen we should stop on the WCF service as well so in work the close method that's that we are done now there's one little step that we have to do so open this hollow windows service file and on the designer right-click and select this option add installer and this is going to add in a project installer dot CS file and on this file you can say service installer and service process installer so select service install of one right click on that and select properties within the properties window here we can specify a meaningful name for our service instead of calling it as service one let's call this hello vendo service and then let's also specify the start type now if we set the start type to manual which is the default you know the service will not automatically start when the operating system starts and the computer starts if we want the service to start automatically when the system starts then we need to set the start type to automatic okay and select service process installer right click on that get to the properties of set and here we need to specify an account so here you have the option to specify you know the account using which you want this Windows service to basically install and run so let's actually select local system here you can select any account that you want alright so we are done with you know configuring the project Installer so let's go ahead and build a solution just to make sure that everything compiles so notice within the status bar it says been succeeded now all that is left is to actually install this Windows service and in order to install and uninstall the vendor service we can make use of this utility called the install yuto and we need to run this utility from visual studio command prompt so click on start all programs Microsoft Visual Studio 2010 and then from there Visual Studio Tools and then Visual Studio command prompt then we need to run it as an administrator so right click on it run as administrator click yes navigate to see driver pressing CD backslash and then let's make use of this utility install yuto and if you don't know the switches that you want to use with this utility you can simply say install you 204 slash question mark so it's going to basically specify the options that you can use with this install utility ok now if we want to install a Windows service using this install utility we specify the switch you know - I for install and then we need to specify the path for the executable you know that contains our Windows service and where is that present you know the project is Windows service host so right click on the project open folder in Windows Explorer and then within the bin directory and then within debug we have the you know project executable this is Windows service host project so this contains the code for the windows service that we wants to install so we need to specify this path there so let's copy that part and then let's navigate to visual studio command prompt and paste it right there and then we also need to specify the name of the executable which is Windows service host dot exe so let's copy that and let's paste it here and then press Enter so you know look at that it says the commit phase completed successfully the transacted install has completed so it should have installed our you know our holo Windows servers and to confirm that you know let's get to the services window and in order to do that click on start type run within the run window type services dot MSA and then click enter and here you know press H and we should see hello windows service there ok and look at the startup type is automatic since we have just installed this service it has not started automatically but then if we reboot of a machine this service will be started automatically ok now if I want to start this service simply right click on it and select this option start ok and look at the status it says started let's actually stop this service for a bit so to install a Windows service we use the switch - I - uninstall it we use - you for uninstall and when we + enter you know it should be uninstalled from the machine let's actually confirm that so it's still doing the cleanup and now when we actually refresh this window look look at that hello windows service is gone from there so let's actually install it once more so this survey should be installing now let's refresh it look at that hello windows service let's go ahead and start the service ok this service has started now so the windows service has started which means our WCF service is hosted within that and the WCF service has also started so let's open up the windows client project that we have been working with so let's fire up another instance of visual studio and then open the windows client project that we have been working with so we want to open the project windows client so let's quickly test this to make sure you know the WCF service is still working as expected all right now before we do that let's update the service reference so it's going to communicate with the WCF service that is hosted within our windows service and then the you know configuration information will be used basically to create the proxy classes and update the configuration information all right now let's go ahead and run this client and see if it works as expected ok let's pass presume as the name and then click get message if everything is working as expected it should get a response back okay so we have discussed hosting or WCF service in Windows service in our next video we'll discuss the advantages and disadvantages of hosting the WC of servers in a Windows service that's it for today thank you for listening have a great day
Info
Channel: kudvenkat
Views: 97,735
Rating: 4.9245281 out of 5
Keywords: hosting, wcf, service, windows service
Id: jnFj4AXn3Eo
Channel Id: undefined
Length: 10min 40sec (640 seconds)
Published: Sat Feb 08 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.