Press Me
Press Me
1. How to create a WCF service with netTcpBinding ?
2. How to host the WCF service in Windows service ?
3. How to start windows service ?
4. How to consume the service ?
You can see video of this article Here
You can refer my Blog for more articles
Create WCF Service
Step 1
Create a WCF Library project.
Step 2
Create Contract. Delete all the default code generated by WCF for you. And create a contract as below,
Step 3
Implement the Service
Step 4
Configure End Point for the service on netTcp Binding.
1. Configure Service Behavior
2. Configure end point with netTcpBinding
3. Configure the meta data exchange end point
Note: Make sure, you are providing name of the endpoints. Else you will catch up with run time exception while adding the service at client side
Full App.Config of WCF library project will look like
App.Config
Create Windows Service
Step 1
Right click and add a new project in same solution. Choose the project type Windows Services from windows tab.
Step 2
Right click on Windows service project and add reference of
1. System.ServiceModel
2. Project reference of WCF Library project created in previous step
Step 3
Copy App.Config from WCF Service Library project and paste in Windows service project. To do so right click on App.config in WCF Library project and select copy then right click on Windows Service project and click on paste.
After copy and paste of APP.Config , we can see App.Config file in Windows Service Project also.
Step 4
Add Service Installer. To do so right click on Service1.cs in windows service project and click on view designer.
Once designer page is open, right click on designer page and click on add installer
Once we will right click on design surface of Service.cs file and click add Installer, it will add ProjectInstaller.cs file.
Right click on the ProjectInstaller.cs file and select view designer.
On designer page, we can see there is ServiceProcessInstaller1 and serviceInstaller1 is there.
Right click on ServiceProcessInstaller1 and select properties. In properties set the Account attribute to NetworkServices
Right click on ServiceInstaller1 and select properties. In properties set the Start Type attribute to Automatic
Step 5
Modify Window service to host the WCF service. To do, open Service1.cs file in Windows service project.
1. Add the below name spaces
2. Declare a variable
3. On Start method of Window service
4. On Stop method of Window service
5. On the event of back ground worker
So full source code of Service1.cs in windows service project will be like below,
Service1.cs
Step 6
Build the solution. After successful built of solution, you can see exe file in debug folder of bin folder. After building the solution, right click on solution and select open folder in Windows Explorer. Once folder is open Windows service project folder. Inside that open bin folder. Inside that open Debug folder. Inside Debug folder you should able to see windows service exe file.
Click on the address bar of the folder and copy the full path of this exe file.
Step 7
Install the windows service.
1. First search what is the path of InstallUtil in your machine. To search this path , click on start and open find and if you are using Windows 7 then browse to your C drive and type InstallUtil , in top right corner search text box.
2. Once you get the full path of InstallUtil , Open your command prompt ad change directory to full path of InstallUtil.
3. Now just type InstallUtil.exe and press enter, you will get help details on how to use InstallUtil.exe.
4. Now to install window service we created, type the below command on your command prompt. Make sure you are giving correct and full path of window service exe.
5. After successful installation, you will get below message.
6. Click on Start and type run and in run window type Service.msc
It will open all the services. We can see Service1 is listed there. If you remember Service1 is name of the service class in Windows service project.
This service is automatic started. Right click on the service and click on the Start.
Now our WCF Service is up and hosted in Windows Services.
Step 8
Create the client.
1. To create the client right click on solution and add a new project of type Console application.
2. Right click on the console application and add service reference. Copy base address from App.config of windows services and paste it here.
3. Now simply create the instance of Service1Client and call the service
Programs.cs
Press F5 to run the console application. Make sure client console application is set as startup project.
Press Me
1. How to create a WCF service with netTcpBinding ?
2. How to host the WCF service in Windows service ?
3. How to start windows service ?
4. How to consume the service ?
You can see video of this article Here
You can refer my Blog for more articles
Create WCF Service
Step 1
Create a WCF Library project.
Step 2
Create Contract. Delete all the default code generated by WCF for you. And create a contract as below,
Step 3
Implement the Service
Step 4
Configure End Point for the service on netTcp Binding.
1. Configure Service Behavior
2. Configure end point with netTcpBinding
3. Configure the meta data exchange end point
Note: Make sure, you are providing name of the endpoints. Else you will catch up with run time exception while adding the service at client side
Full App.Config of WCF library project will look like
App.Config
01 | <!--?xml version="1.0" encoding="utf-8" ?--> |
02 | < configuration > |
03 | < system.web > |
04 | < compilation debug = "true" > |
05 | </ compilation ></ system.web > |
06 | < system.servicemodel > |
07 | < services > |
08 | < service name = "NetTcpServicetoHostinWindowsServices.Service1" behaviorconfiguration = "MyBehavior" > |
09 | < host > |
10 | < baseaddresses > |
11 | < add baseaddress = "net.tcp://localhost:9999/Service1/" > |
12 | </ add ></ baseaddresses > |
13 | </ host > |
14 | < endpoint name = "NetTcpEndPoint" address = "" binding = "netTcpBinding" contract = "NetTcpServicetoHostinWindowsServices.IService1" > |
15 | </ endpoint > |
16 | < endpoint name = "NetTcpMetadataPoint" address = "mex" binding = "mexTcpBinding" contract = "IMetadataExchange" > |
17 | </ endpoint ></ service > |
18 | </ services > |
19 | < behaviors > |
20 | < servicebehaviors > |
21 | < behavior name = "MyBehavior" > |
22 | < servicemetadata httpgetenabled = "False" > |
23 | < servicedebug includeexceptiondetailinfaults = "False" > |
24 | </ servicedebug ></ servicemetadata ></ behavior > |
25 | </ servicebehaviors > |
26 | </ behaviors > |
27 | </ system.servicemodel > |
28 | </ configuration > |
Create Windows Service
Step 1
Right click and add a new project in same solution. Choose the project type Windows Services from windows tab.
Step 2
Right click on Windows service project and add reference of
1. System.ServiceModel
2. Project reference of WCF Library project created in previous step
Step 3
Copy App.Config from WCF Service Library project and paste in Windows service project. To do so right click on App.config in WCF Library project and select copy then right click on Windows Service project and click on paste.
After copy and paste of APP.Config , we can see App.Config file in Windows Service Project also.
Step 4
Add Service Installer. To do so right click on Service1.cs in windows service project and click on view designer.
Once designer page is open, right click on designer page and click on add installer
Once we will right click on design surface of Service.cs file and click add Installer, it will add ProjectInstaller.cs file.
Right click on the ProjectInstaller.cs file and select view designer.
On designer page, we can see there is ServiceProcessInstaller1 and serviceInstaller1 is there.
Right click on ServiceProcessInstaller1 and select properties. In properties set the Account attribute to NetworkServices
Right click on ServiceInstaller1 and select properties. In properties set the Start Type attribute to Automatic
Step 5
Modify Window service to host the WCF service. To do, open Service1.cs file in Windows service project.
1. Add the below name spaces
2. Declare a variable
3. On Start method of Window service
4. On Stop method of Window service
5. On the event of back ground worker
So full source code of Service1.cs in windows service project will be like below,
Service1.cs
01 | using System; |
02 | using System.Collections.Generic; |
03 | using System.ComponentModel; |
04 | using System.Data; |
05 | using System.Diagnostics; |
06 | using System.Linq; |
07 | using System.ServiceProcess; |
08 | using System.Text; |
09 | using System.ServiceModel; |
10 | using NetTcpServicetoHostinWindowsServices; |
11 |
12 | namespace HostingWindowsService |
13 | { |
14 | public partial class Service1 : ServiceBase |
15 | { |
16 | internal static ServiceHost myHost = null ; |
17 | BackgroundWorker worker; |
18 | public Service1() |
19 | { |
20 | InitializeComponent(); |
21 | } |
22 |
23 | protected override void OnStart( string [] args) |
24 | { |
25 | |
26 | worker = new BackgroundWorker(); |
27 | worker.DoWork += new DoWorkEventHandler(worker_DoWork); |
28 |
29 | } |
30 | void worker_DoWork( object sender, DoWorkEventArgs e) |
31 | { |
32 |
33 | if (myHost != null ) |
34 | { |
35 | myHost.Close(); |
36 | } |
37 |
38 | myHost = new ServiceHost( typeof (NetTcpServicetoHostinWindowsServices.Service1)); |
39 | myHost.Open(); |
40 |
41 |
42 |
43 | } |
44 |
45 | protected override void OnStop() |
46 | { |
47 |
48 | if (myHost != null ) |
49 | { |
50 | myHost.Close(); |
51 | myHost = null ; |
52 | } |
53 |
54 | } |
55 | } |
56 | } |
Step 6
Build the solution. After successful built of solution, you can see exe file in debug folder of bin folder. After building the solution, right click on solution and select open folder in Windows Explorer. Once folder is open Windows service project folder. Inside that open bin folder. Inside that open Debug folder. Inside Debug folder you should able to see windows service exe file.
Click on the address bar of the folder and copy the full path of this exe file.
Step 7
Install the windows service.
1. First search what is the path of InstallUtil in your machine. To search this path , click on start and open find and if you are using Windows 7 then browse to your C drive and type InstallUtil , in top right corner search text box.
2. Once you get the full path of InstallUtil , Open your command prompt ad change directory to full path of InstallUtil.
3. Now just type InstallUtil.exe and press enter, you will get help details on how to use InstallUtil.exe.
4. Now to install window service we created, type the below command on your command prompt. Make sure you are giving correct and full path of window service exe.
5. After successful installation, you will get below message.
6. Click on Start and type run and in run window type Service.msc
It will open all the services. We can see Service1 is listed there. If you remember Service1 is name of the service class in Windows service project.
This service is automatic started. Right click on the service and click on the Start.
Now our WCF Service is up and hosted in Windows Services.
Step 8
Create the client.
1. To create the client right click on solution and add a new project of type Console application.
2. Right click on the console application and add service reference. Copy base address from App.config of windows services and paste it here.
3. Now simply create the instance of Service1Client and call the service
Programs.cs
01 | using System; |
02 | using System.Collections.Generic; |
03 | using System.Linq; |
04 | using System.Text; |
05 | using ConsoleClient.ServiceReference1; |
06 |
07 | namespace ConsoleClient |
08 | { |
09 | class Program |
10 | { |
11 | static void Main( string [] args) |
12 | { |
13 | Service1Client proxy = new Service1Client(); |
14 | Console.WriteLine(proxy.GetData(9)); |
15 | Console.Read(); |
16 | } |
17 | } |
18 | } |
Press F5 to run the console application. Make sure client console application is set as startup project.
No comments:
Post a Comment