inforakesha

A fine WordPress.com site

My first Basic WCF Example -Step by step

Leave a comment

Create a console application

In that:-

create

1.Interface

[ServiceContract]
interface IJob
{
[OperationContract]
string GetJobInfo(int jobid);

[OperationContract]
void AddJob();
}

2.class and implement that interface

class Job : IJob
{
string str = @”Data Source=T6\SQLEXPRESS2012; initial catalog=pubs;
user id=sa ;pwd=sa123 ;”;

public string GetJobInfo(int jobid)
{
Console.WriteLine(“Request Made : ” + DateTime.Now.ToString());
SqlConnection cn = new SqlConnection(str);
cn.Open();
string sqlstat = “Select job_desc from jobs where job_id=” + jobid;
SqlCommand cmd = new SqlCommand(sqlstat, cn);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
return dr[0].ToString();
else
return “-1”;

}

public void AddJob()
{
throw new NotImplementedException();
}

}

3.Now in  class Program.cs
and write code:-
class Program
{
static void Main(string[] args)
{
ServiceHost obj = new ServiceHost(typeof(Job));
obj.AddServiceEndpoint(typeof(IJob),
new BasicHttpBinding(), “http://localhost:8083/nitjobs”);

// obj.AddServiceEndpoint(typeof(IJob), new NetTcpBinding(), “net.tcp://localhost:8082/nitjobs1”);

obj.Open();
Console.WriteLine(“Started Server – Press any key to terminate”);
Console.ReadKey();
}

Our web service is now ready to use.

Now we will create one console application to use our webservice

Write following code in program.cs
1.[ServiceContract]
interface IJob
{
[OperationContract]
string GetJobInfo(int jobid);

[OperationContract]
void AddJob();
}

class Program
{
static void Main(string[] args)
{
Console.WriteLine(“HTTP Client”);
//ChannelFactory obj =
// new ChannelFactory
// (new NetTcpBinding(),
// new EndpointAddress(“net.tcp://localhost:8082/nitjobs1”));

ChannelFactory obj =
new ChannelFactory
(new BasicHttpBinding(),
new EndpointAddress(“http://localhost:8083/nitjobs”));

IJob req = obj.CreateChannel();
Console.WriteLine(“Enter Jobid to Search : “);
int x = int.Parse(Console.ReadLine());
Console.WriteLine(req.GetJobInfo(x));

Console.ReadKey();
}

Important points:

netstat -- show network status
DESCRIPTION
     The netstat command symbolically displays the contents of various net-
     work-related data structures.  There are a number of output formats,
     depending on the options for the information presented.

     netstat [-AaLnSTWx] [-f protocol_family | -p protocol] [-M core]
	     [-N system]
	     Display a list of active sockets (protocol control blocks) for
	     each network protocol, for a particular protocol_family, or for a
	     single protocol.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s