locked
Created WCF service Successfully,How to call uriTemplate on remote server. RRS feed

  • Question

  • User211197711 posted

    Hi developers,I am working in asp.net web forms from last 5 years but now for some requirement i need to use sql server database both for web application and iphone application.So i will need to create wcf returing json data.I created wcf service and working fine on link: http://ambalahelpline.com/ambalahelpline/EmpService.svc

    Let me know how to call uri template to get data from service contract.M pasting my all code here:

    ------------------------------------------------------------------------------------------------------------------------------------

    IEmpService.cs:

    [ServiceContract]
    public interface IEmpService
    {
    [OperationContract()]
    [WebGet(UriTemplate = "Employees", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    List<Employee> GetEmployeeList();

    [OperationContract()]
    [WebGet(UriTemplate = "Employees/{Empno}", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]

    Employee GetEmployeeById(string Empno);

    [OperationContract()]
    [WebInvoke(UriTemplate = "Employees", Method = "POST")]
    void AddEmployee(Employee oEmp);

    [OperationContract()]
    [WebInvoke(UriTemplate = "Employees", Method = "PUT")]
    void UpdateEmployee(Employee oEmp);

    [OperationContract()]
    [WebInvoke(UriTemplate = "Employees/{Empno}", Method = "DELETE")]
    //[WebInvoke(UriTemplate = "/Employees?Empno={Empno}", Method = "DELETE")]
    void DeleteEmployee(string Empno);
    }

    [DataContract(Namespace = "")]
    public class Employee
    {

    [DataMember(Order = 0)]
    public int GENERALID { get; set; }

    [DataMember(Order = 1)]
    public string FIRSTNAME { get; set; }

    [DataMember(Order = 2)]
    public string SEX { get; set; }

    [DataMember(Order = 3)]
    public string ADDRESS { get; set; }

    [DataMember(Order = 4)]
    public string PASSWORD { get; set; }

    [DataMember(Order = 5)]
    public string EMAIL { get; set; }

    }

    -------------------------------------------------------------------------------------------------------

    EmpService.cs

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class EmpService : IEmpService
    {
    public List<Employee> GetEmployeeList()
    {
    //var dt = (new DbHelper()).GetResultSet("SELECT empno, ename, sal, deptno FROM emp");
    var dt = (new DbHelper()).GetResultSet("select GENERALID, FIRSTNAME, SEX, ADDRESS, PASSWORD, EMAIL from FT_GENERAL_USERS");
    var qEmp = from dr in dt.AsEnumerable()
    select new Employee()
    {
    //Empno = dr["empno"].ToString(),
    //Ename = dr["ename"].ToString(),
    //Sal = dr["sal"].ToString(),
    //Deptno = dr["deptno"].ToString()
    GENERALID = Convert.ToInt32(dr["GENERALID"]),
    FIRSTNAME = dr["FIRSTNAME"].ToString(),
    SEX = dr["SEX"].ToString(),
    ADDRESS = dr["ADDRESS"].ToString(),
    PASSWORD = dr["PASSWORD"].ToString(),
    EMAIL = dr["EMAIL"].ToString()
    };

    return qEmp.ToList();
    }

    //public Employee GetEmployeeById(string Empno)
    public Employee GetEmployeeById(string Empno)
    {
    //var dt = (new DbHelper()).GetResultSet("SELECT empno, ename, sal, deptno FROM emp WHERE empno = " + Empno);
    var dt = (new DbHelper()).GetResultSet("SELECT GENERALID, FIRSTNAME, SEX, ADDRESS, PASSWORD, EMAIL FROM FT_GENERAL_USERS WHERE GENERALID = " + Convert.ToInt32(Empno));
    if (dt.Rows.Count > 0)
    {
    var dr = dt.Rows[0];
    return new Employee()
    {
    //Empno = dr["empno"].ToString(),
    //Ename = dr["ename"].ToString(),
    //Sal = dr["sal"].ToString(),
    //Deptno = dr["deptno"].ToString()
    GENERALID = Convert.ToInt32(dr["GENERALID"]),
    FIRSTNAME = dr["FIRSTNAME"].ToString(),
    SEX = dr["SEX"].ToString(),
    ADDRESS = dr["ADDRESS"].ToString(),
    PASSWORD = dr["PASSWORD"].ToString(),
    EMAIL = dr["EMAIL"].ToString()
    };
    }
    return null;
    }

    public void AddEmployee(Employee oEmp)
    {
    StringBuilder sb = new StringBuilder();
    //sb.Append("INSERT INTO Emp (Empno, Ename, Sal, Deptno) VALUES ");
    sb.Append("INSERT INTO FT_GENERAL_USERS (FIRSTNAME, SEX, ADDRESS, PASSWORD, EMAIL) VALUES ");
    //sb.AppendFormat("( {0}, '{1}', {2}, {3} )",
    // oEmp.Empno, oEmp.Ename, oEmp.Sal, oEmp.Deptno);
    sb.AppendFormat("( '{0}', '{1}', '{2}', '{3}', '{4}' )",
    oEmp.FIRSTNAME, oEmp.SEX, oEmp.ADDRESS, oEmp.PASSWORD, oEmp.EMAIL);

    (new DbHelper()).SqlExecute(sb.ToString());

    }

    public void UpdateEmployee(Employee oEmp)
    {
    StringBuilder sb = new StringBuilder();
    //sb.Append("UPDATE Emp SET ");
    //sb.AppendFormat(" ename = '{0}', sal = {1}, deptno = '{2}' WHERE empno = {3} ",
    // oEmp.Ename, oEmp.Sal, oEmp.Deptno, oEmp.Empno);
    sb.Append("UPDATE FT_GENERAL_USERS SET ");
    sb.AppendFormat(" FIRSTNAME = '{0}', SEX = '{1}', ADDRESS = '{2}', PASSWORD = '{3}', EMAIL = '{4}' WHERE GENERALID = '{5}' ",
    oEmp.FIRSTNAME, oEmp.SEX, oEmp.ADDRESS, oEmp.PASSWORD, oEmp.EMAIL, oEmp.GENERALID);

    (new DbHelper()).SqlExecute(sb.ToString());

    }

    public void DeleteEmployee(string Empno)
    {
    (new DbHelper()).SqlExecute("DELETE FROM FT_GENERAL_USERS WHERE GENERALID = " + Convert.ToInt32(Empno));
    }
    }

    ------------------------------------------------------------------------------

    Web.config:

    <system.serviceModel>
    <behaviors>
    <serviceBehaviors>
    <behavior name="WithDebug">
    <serviceMetadata httpGetEnabled="true" />
    <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
    <service name="EmpService" behaviorConfiguration="WithDebug">
    <endpoint
    address="Test"
    binding="basicHttpBinding"
    contract="IEmpService" >
    <identity>
    <dns value="localhost"/>
    </identity>
    </endpoint>
    <endpoint
    address="mex"
    binding="mexHttpBinding"
    contract="IMetadataExchange" />
    </service>
    </services>
    </system.serviceModel>

    please let me know how to use get,post and all methods to use web services on iphone

    Wednesday, October 30, 2013 3:52 AM

Answers