Let’s begin from creating a simple Person web service
Step 1
Create a new project “Person Service” – A WCF service
Create a new interface IPersonService.cs with the following contents
using System.ServiceModel;
using System.Runtime.Serialization;
namespace PersonService
{
[ServiceContract(Namespace="http://techkrishnan.blogspot.com")]
public interface IPersonService
{
[OperationContract(IsOneWay=true)]
void AddPerson(Person pers);
[OperationContract()]
int RemovePerson(Person pers);
[OperationContract]
Person GetPersonById(int nId);
}
[DataContract]
public class Person
{
private int id;
[DataMember(IsRequired=false, Order=1)]
public int Id
{
get { return id; }
set { id = value; }
}
private string firstName;
[DataMember(IsRequired = true, Order = 2)]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
private string lastName;
[DataMember(IsRequired = true, Order = 3)]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
private int age;
[DataMember(IsRequired = true, Order = 4)]
public int Age
{
get { return age; }
set { age = value; }
}
private string country;
[DataMember(IsRequired = true, Order = 5)]
public string Country
{
get { return country; }
set { country = value; }
}
}
Implement interface IPersonService
namespace PersonService
{
public class PersonService : IPersonService
{
#region IPersonService Members
public void AddPerson(Person pers)
{
//Id is not needed. Rest of them are
Console.WriteLine("New Person information");
Console.WriteLine("First Name :" + pers.FirstName);
Console.WriteLine("Last Name :" + pers.LastName);
Console.WriteLine("Age : " + pers.Age);
Console.WriteLine("Country : " + pers.Country);
Console.WriteLine("**********************************");
}
public int RemovePerson(Person pers)
{
//Id is a must
//check for ID
if (pers.Id != 0)
{
Console.WriteLine("Deleted person : " + pers.Id);
}
return 1;
}
public Person GetPersonById(int nId)
{
//return person
Person p = new Person();
p.Id = 1;
p.FirstName = "Krishnan";
p.LastName = "Sriram";
p.Age = 30;
p.Country = "India";
return p;
}
#endregion
}
}
Compile project. Ensure there are no compiler errors
Step 3
Add a new project in the solution. This will be our host application. For now we can be contended with a simple console based host
Create a new Windows console project – “PersonServiceHost”
Add reference to ServiceModel library
Add reference to PersonService.dll
Make changes to App.config as mentioned below
<configuration>
<system.serviceModel>
<bindings />
<client />
<behaviors>
<endpointBehaviors>
<behavior name="NewBehavior" />
</endpointBehaviors>
<serviceBehaviors>
<behavior name="HttpPersonServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<dataContractSerializer ignoreExtensionDataObject="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="HttpPersonServiceBehavior" name="PersonService.PersonService">
<endpoint address="BasicPersonService" binding="basicHttpBinding" bindingConfiguration="" name="HttpPersonServiceEndPoint" contract="PersonService.IPersonService" />
<endpoint address="add" binding="mexHttpBinding" bindingConfiguration="" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://aa-09660:8080" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Open Program.cs and make the following changes
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace PersonServiceHost
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost sHost = new ServiceHost(typeof(PersonService.PersonService)))
{
Console.WriteLine("Service started.........");
sHost.Open();
Console.WriteLine("Press any key to terminate service");
Console.ReadKey(true);
sHost.Close();
}
}
}
}
Compile code. Ensure there are no compiler errors
Step 3
Create a new project for client, a windows console or forms client. I created a forms client – “PersonServiceClient”
Create a form and add controls and methods to accommodate the controls to save, read, and list data from service
Add new app.config
Execute PersonServiceHost
Add serviceconfiguration – I named it “PersonClientReference”
Make changes to forms to get values from the controls and validate it (for NULL and invalid entries)
My sample code for the form is appended
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace PersonServiceClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnNew_Click(object sender, EventArgs e)
{
txtId.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
txtAge.Text = "";
txtCountry.Text = "";
}
private void btnSave_Click(object sender, EventArgs e)
{
PersonClientReference.Person p = new PersonServiceClient.PersonClientReference.Person();
p.FirstName = txtFirstName.Text;
p.LastName = txtFirstName.Text;
p.Age = System.Convert.ToInt32(txtAge.Text);
p.Country = txtCountry.Text;
PersonClientReference.PersonServiceClient proxy = new PersonServiceClient.PersonClientReference.PersonServiceClient();
proxy.AddPerson(p);
}
private void btnFindById_Click(object sender, EventArgs e)
{
PersonClientReference.Person p = null;
PersonClientReference.PersonServiceClient proxy = new PersonServiceClient.PersonClientReference.PersonServiceClient();
p = proxy.GetPersonById(1);
txtId.Text = p.Id.ToString();
txtFirstName.Text = p.FirstName;
txtLastName.Text = p.LastName;
txtAge.Text = p.Age.ToString();
txtCountry.Text = p.Country;
}
#endregion
}
}
No comments:
Post a Comment