Creating and Consuming the WCF Service
This session show how to create the WCF service:
this project developed in the VS 2008 in Windows 7 platform
Direct Link
This consist of 3 projects
I WcfServiceLibrary1
---------------------
1.IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract()]
string SayHello();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
2. Service1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Net;
namespace WcfServiceLibrary1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string SayHello()
{
return string.Format("Hi, Hello from {0}",Dns.GetHostName().ToString());
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
II WCF_Host_Application
Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll
Design the from

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Host_Application
{
public partial class Form1 : Form
{
ServiceHost vHost;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//start
try
{
vHost = new ServiceHost(typeof(Service1));
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
vHost.AddServiceEndpoint(typeof(IService1), b, new Uri("net.tcp://localhost/myservice/"));
vHost.Open();
btn_start.Enabled = false;
btn_stop.Enabled = true;
lbl_info.Text = "Service started";
MessageBox.Show("Service started");
}
catch (Exception ex)
{
lbl_info.Text = ex.Message.ToString();
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
vHost.Close();
btn_start.Enabled =true ;
btn_stop.Enabled = false;
lbl_info.Text = "Service stoped";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
III. WCF Client
Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Client
{
class Program
{
static void Main(string[] args)
{
IService1 vService;
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
EndpointAddress vEndPoint = new EndpointAddress("net.tcp://192.168.1.11/myservice/");
ChannelFactory cf = new ChannelFactory(b, vEndPoint);
vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}
vEndPoint = new EndpointAddress("net.tcp://192.168.1.12/myservice/");
cf = new ChannelFactory(b, vEndPoint);
vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}
Console.ReadLine();
}
}
}
Thats all
now Run the "WCF_Host_Application" and press Start
Now Run the "WCF_Client"
Enjoy Programming
this project developed in the VS 2008 in Windows 7 platform
Direct Link
This consist of 3 projects
I WcfServiceLibrary1
---------------------
1.IService1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace WcfServiceLibrary1
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract()]
string SayHello();
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
2. Service1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Net;
namespace WcfServiceLibrary1
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public string SayHello()
{
return string.Format("Hi, Hello from {0}",Dns.GetHostName().ToString());
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
II WCF_Host_Application
Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll
Design the from
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Host_Application
{
public partial class Form1 : Form
{
ServiceHost vHost;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//start
try
{
vHost = new ServiceHost(typeof(Service1));
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
vHost.AddServiceEndpoint(typeof(IService1), b, new Uri("net.tcp://localhost/myservice/"));
vHost.Open();
btn_start.Enabled = false;
btn_stop.Enabled = true;
lbl_info.Text = "Service started";
MessageBox.Show("Service started");
}
catch (Exception ex)
{
lbl_info.Text = ex.Message.ToString();
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
vHost.Close();
btn_start.Enabled =true ;
btn_stop.Enabled = false;
lbl_info.Text = "Service stoped";
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
III. WCF Client
Add the References
* System.ServiceModel
* WcfServiceLibrary1.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using WcfServiceLibrary1;
namespace WCF_Client
{
class Program
{
static void Main(string[] args)
{
IService1 vService;
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Message;
b.Security.Message.ClientCredentialType = MessageCredentialType.Windows;
EndpointAddress vEndPoint = new EndpointAddress("net.tcp://192.168.1.11/myservice/");
ChannelFactory
vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}
vEndPoint = new EndpointAddress("net.tcp://192.168.1.12/myservice/");
cf = new ChannelFactory
vService = cf.CreateChannel();
try
{
Console.WriteLine("Result from {0} : {1} ", vEndPoint.Uri.ToString(), vService.SayHello());
}
catch (Exception ex)
{
Console.WriteLine("Error occured while connection establishment on " + vEndPoint.Uri.ToString());
}
Console.ReadLine();
}
}
}
Thats all
now Run the "WCF_Host_Application" and press Start
Now Run the "WCF_Client"
Enjoy Programming
Comments