A sample code for WCF Service ?


This article will describe about the WCF services by using a sample code. By reading this article you will come to know about how to use the WCF in an application. This is a very simple application of WCF. This application is created in Visual Studio.

Find a sample code for WCF Service?


I will write a sample code to show the uses of WCF in an application.

Follow below steps:
1. Open visual studio.
2. New->Project
3. From Project type select Web and from templates select WCF service application. Click OK.
4. Now your service interface and service implementation class will be created automatically with name "IService1.cs" and "Service1.svc" respectively.
5. In interface define a method like below:

Below is the operation defined for service:

[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);

[OperationContract]
Boolean AddAddress(string vname, string fathername, string vstreet, string vcity, Int64 phno);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

[OperationContract]
DataSet GetAlladdress();

[OperationContract]
DataSet GetAddressByName(string name);

// TODO: Add your service operations here
}


In the same interface define DataContract like below:


[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; }
}
}


Now you have to implement the service/operation that you have defined in interface:


public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}

public Boolean AddAddress(string vname, string fathername, string vstreet, string vcity, long phno)
{
//throw new NotImplementedException();
string vsql = string.Format("insert into address values ('{0}','{1}','{2}','{3}','{4}')", vname, fathername, vstreet, vcity, phno);
con.Open();
SqlCommand cmd = new SqlCommand(vsql, con);
cmd.ExecuteNonQuery();
con.Close();
return true;
}

public DataSet GetAlladdress()
{
DataSet ds = new DataSet();
string sqls = "select * from address";
con.Open();
SqlCommand cmd = new SqlCommand(sqls,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "res");
da.Dispose();
con.Close();
return ds;
}

public DataSet GetAddressByName(string name)
{
DataSet ds = new DataSet();
string sql1 = string.Format("select * from address where sname = '{0}'", name);
con.Open();
SqlCommand cmd = new SqlCommand(sql1, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds, "res");
da.Dispose();
con.Close();
return ds;
}



Now your coding has been completed. You may now browse your.svc file. And copy the url. Right click on project name in solution explorer. Select 'Add Refrence'. In target url paste the url you copied and click on go.

Your service name will be listed and you can see your methods which you have defined in your code.

You can give a name to this namespace in given input box. And now you can call your methods in your UI code by using this namespace.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: