Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
how are you.

I am facing a problem in post method. i am trying to update in server but service saying end point not found.

The thing is that web get method is working but post method is not working here is code :

C#
namespace Healthcare
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

      
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "EmpRecord"
            )]
        List<EmpRecord> GetEmployeeLIst();

        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "EmpRecord/{id}")]
        
        EmpRecord GetemById(string id);



      [OperationContract(Name = "PostSampleMethod")]
        [WebInvoke(Method = "POST",
         UriTemplate = "PostSampleMethod")]
        
        string PostSampleMethod(string city);
       



    }


i am successfully calling service with get mehtod with this url : http://localhost:57325/Service1.svc/emprecord


but when calling PostSampleMethod it saying method not found with url :
http://localhost:57325/Service1.svc/PostSampleMethodPostSampleMethod

if i put parameter
http://localhost:57325/Service1.svc/PostSampleMethod/usa

then it say end point not found




the service code

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using Emp.Interface;
namespace Healthcare
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public List<EmpRecord> GetEmployeeLIst()
        {
            string connStr = ConfigurationManager.ConnectionStrings["constr"].ToString();
            SqlConnection conn = new SqlConnection(connStr);
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "select city,address from tblemployee";
            comm.Connection = conn;
            SqlDataAdapter da = new SqlDataAdapter(comm);
            DataSet ds = new DataSet();
            conn.Open();
            da.Fill(ds, "StudentRecord");
            conn.Close();
            List<EmpRecord> stuList = new List<EmpRecord>();
            
            if (ds.Tables != null)
            {
                int recordCount = ds.Tables["StudentRecord"].Rows.Count;
                if (recordCount > 0)
                {
                    DataTable dtab = ds.Tables["StudentRecord"];
                    for (int i = 0; i < recordCount; i++)
                    {
                        EmpRecord objemp = new EmpRecord();
                        objemp.city = dtab.Rows[i]["city"].ToString();
                        objemp.address = dtab.Rows[i]["address"].ToString();    
                        stuList.Add(objemp);
                    }
                }
            }
            comm.Dispose();
            return stuList;
           
        }



        public EmpRecord GetemById(string id)
        {
            try
            {
                int bookId = Convert.ToInt32(id);

                string connStr = ConfigurationManager.ConnectionStrings["constr"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand comm = new SqlCommand();
                comm.CommandText = "select Empkey,city,[Address]    from tblemployee where Empkey="+bookId;
                comm.Connection = conn;
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataSet ds = new DataSet();
                conn.Open();
                da.Fill(ds, "StudentRecord");
                conn.Close();
                
                EmpRecord objemp = new EmpRecord();
                if (ds.Tables != null)
                {
                    int recordCount = ds.Tables["StudentRecord"].Rows.Count;
                    if (recordCount > 0)
                    {
                        DataTable dtab = ds.Tables["StudentRecord"];
                       
                            objemp.city = dtab.Rows[0]["city"].ToString();
                            objemp.address = dtab.Rows[0]["address"].ToString();
                            
                        
                    }
                }
                comm.Dispose();
                return objemp;


            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
        }


        public string PostSampleMethod(string city)
        {
            try
            {

                string m = "True";
                string connStr = ConfigurationManager.ConnectionStrings["constr"].ToString();
                SqlConnection conn = new SqlConnection(connStr);
                SqlCommand comm = new SqlCommand();
                comm.CommandText = "update tblemployee set  city='rupndagr' where city=" + city;
                comm.Connection = conn;
                SqlDataAdapter da = new SqlDataAdapter(comm);
                conn.Open();
                comm.ExecuteNonQuery();  
                conn.Close();
                return m;

              

                
            }
            catch
            {
                throw new FaultException("Something went wrong");
            }
        }

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









Please help here is web.config

C#
<?xml version="1.0"?>
<configuration>
  <connectionStrings>
  <add name="constr" connectionString="Data Source=.;Initial Catalog=dbHealthcare;Integrated Security=false;Persist Security Info=True;User Id=sa;Password=nic123" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Healthcare.Service1" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address =""  name="webHttpBinding" binding="webHttpBinding" contract="Healthcare.IService1" behaviorConfiguration="webHttp">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>

        <endpoint name="mexHttpBinding"
                  address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange"
                  />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1"></serviceHostingEnvironment>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above 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="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>

  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>
Posted
Updated 2-Aug-13 20:04pm
v2
Comments
Jameel VM 3-Aug-13 2:37am    
from where you want to call the that service?
Jameel VM 3-Aug-13 2:43am    
did you try to call the service using httpWebRequest?
balongi 3-Aug-13 3:26am    
i am testing the web service .. and it say method not found

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900