INterface:
[ServiceContract]
public interface IStudentData
{
[OperationContract]
[WebInvoke(UriTemplate="Allstudents", ResponseFormat = WebMessageFormat.Json)]
List<ListItem> GetStudents();
}
Class:
public List<ListItem> GetStudents()
{
string connstr = ConfigurationManager.ConnectionStrings["Resrservice"].ToString();
SqlConnection con = new SqlConnection(connstr);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select RollNo,Name from Student";
cmd.Connection = con;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
con.Open();
da.Fill(ds, "Student");
con.Close();
List<ListItem> stulist = new List<ListItem>();
if (ds.Tables != null)
{
int re = ds.Tables["Student"].Rows.Count;
if (re > 0)
{
DataTable dtab = ds.Tables["Student"];
for (int i = 0; i < re; i++)
{
stulist.Add(new ListItem(
dtab.Rows[i]["Name"].ToString(),
dtab.Rows[i]["RollNo"].ToString()
));
}
}
}
cmd.Dispose();
return stulist;
}
Confiq file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<connectionStrings>
<add name="Resrservice" connectionString="Data Source=NOTOUSANIL\SQLEXPRESS;Initial Catalog=REST;Persist Security Info=True;User ID=sa;Password=123"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfService.StudentData">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="restPoxBehavior" contract="WcfService.IStudentData"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restPoxBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
After i a running this service
http://