File ->New-> Project-WCF Service Library
Delete the files IService and Service
Go to App.Config and Delete the Following lines which is written in
<services>
</services>
Add New Item and Add WCF Service name as Jobs.cs
And click on Add , then two files will be created.
IJobs.cs
Jobs.cs
Add New Item->Job.cs
Write the following code in IJobs.cs
Write the following code in Job.cs
Write the following code in Jobs.cs
Click on Start Debugging
Now double on GetJobs() to check the method is working or not.
The following screen appears, Next click on Invoke
And check the check box and click on Ok .
And click on the button beside View and see the data.
Copy the Url of the Service Application
.
Client application
Select windows application
First add the service Reference
And paste the URL
And click on Go button
And click on Ok Button.
And design the form as follows
Write the following code in Form1.cs
private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.JobsClient obj = new ServiceReference1.JobsClient();
DataSet ds = new DataSet();
ds = obj.GetJobs();
dataGridView1.DataSource = ds.Tables[0];
}
private void button2_Click(object sender, EventArgs e)
{
ServiceReference1.JobsClient obj = new ServiceReference1.JobsClient();
ServiceReference1.Job jobobj = obj.GetJobInfo(int.Parse(textBox1.Text));
textBox2.Text = jobobj.employeename;
textBox3.Text = jobobj.address;
}
Run and see the output
Windows Hosting in WCF
File-new Project-windows Service
Next Right Click on Add Installer
We have to add the reference of our WCF Service Library DLL in this service.
To do this, right click on the project in Solution Explorer and select Add Reference. For adding the dll in reference, select browse tab in the window that appears, and navigate to the folder where our WCF service library is located. In that location, the dll can be found inside the bin-> debug folder. When this dll is added, it is imported into the Windows service app.
Similarly we need reference of
System.ServiceModel
namespace because it is not available by default in Windows Service.We will get two processes
Service process installer
Service installer
Service.cs
public partial class Service1 : ServiceBase
{
ServiceHost sHost;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
sHost = new ServiceHost(typeof(WcfServiceLibrary3.Jobs));
sHost.Open();
}
protected override void OnStop()
{
sHost.Close();
}
}
Registering the Service with Windows OS
Go to the design view of the Windows service, right click and click on Add Installer option.
When this is clicked, we get two processes – ServiceProcessInstaller and ServiceInstaller.
Click on ServiceProcessInstaller and set its account property to local system.
Now click on Service Installer properties
Now build the service once again. This will create an executable file.
Copy the path of this exe file location and open the containing folder from command prompt.
In that path write the command
InstallUtil WindowsService.exe
Which registers the service with Windows OS?
Once it is registered with Windows, we can see it in the services list.
(Control Panel > Administrative Tools > Services)
No comments:
Post a Comment
Thank you for visiting my blog