Click here to Skip to main content
15,891,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have made a a restful api using post method but it shows me the error that Endpoint not found.
here is the
file
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{

    [ServiceContract]
    public interface IRestTestService
    {//NeighbourhoodApi/?apiKey={apiKey}&lat={lat}&longg={longg}


        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "NeighbourhoodApi/{apiKey}/{lat}/{longg}")]
        string NeighbourhoodApi(string apiKey, string lat, string longg);


    }

}


SVC File
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Net;
using System.Web.Script.Serialization;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Xml.Serialization;

namespace WcfService2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestTestService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select RestTestService.svc or RestTestService.svc.cs at the Solution Explorer and start debugging.
    public class RestTestService : IRestTestService
    {
        public class neighbourHood
        {
            public neighbourHood()
            {

            }

            public string name { get; set; }
            public string vicinity { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }

        }
        public string NeighbourhoodApi(string apiKey, string lat, string longg)
        {
          //  responseData objres = new responseData();
            string apikey = ConfigurationSettings.AppSettings["apiKey"];

            if (apiKey == apikey)
            {

            List<neighbourhood> lstNh = new List<neighbourhood>();
            neighbourHood objNeigh = new neighbourHood();
            JavaScriptSerializer jss = new JavaScriptSerializer();
            int indexCount = 0;
            string finaldata = string.Empty;

            //radius is given in meters so we have to give the radious in meters to get neigbhourhoods in ur curent location
            string url = "https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=" + lat + "," + longg + "&radius=5000&sensor=false&key=AIzaSyAhaD4HwgofkA2_9Z7fLbGB1V8Shi-S7do";
            WebRequest request = WebRequest.Create(url);

            try
            {

                using (WebResponse response = request.GetResponse())
                {
                    string res = string.Empty;
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            res = reader.ReadToEnd();
                        }

                        XmlDocument myDoc = new XmlDocument();
                        myDoc.LoadXml(res);
                        XmlElement root = myDoc.DocumentElement;
                        XmlNodeList objList = root.SelectNodes("/PlaceSearchResponse/result");
                        foreach (XmlNode name in objList)
                        {
                            objNeigh = new neighbourHood();

                            XmlNodeList objname = root.SelectNodes("/PlaceSearchResponse/result/name");
                            XmlNodeList objVicinity = root.SelectNodes("/PlaceSearchResponse/result/vicinity");
                            XmlNodeList objLat = root.SelectNodes("/PlaceSearchResponse/result/geometry/location/lat");
                            XmlNodeList objLong = root.SelectNodes("/PlaceSearchResponse/result/geometry/location/lng");

                            objNeigh.name = objname[indexCount].InnerText;
                            objNeigh.vicinity = objVicinity[indexCount].InnerText;
                            objNeigh.latitude = Convert.ToDouble(objLat[indexCount].InnerText);
                            objNeigh.longitude = Convert.ToDouble(objLong[indexCount].InnerText);

                            lstNh.Add(objNeigh);
                            indexCount++;
                        }

                    }

                    bool result;

                    if (res != null)
                    {
                        result = true;
                        //res = res.Replace("/", "");
                        //res = res.Replace("+", "");
                    }
                    else
                    {
                        result = false;
                    }
                    string abc ="{" + '"' + "Head" + '"' + ":{" + '"' + "Status" + '"' + ":" + jss.Serialize(result) + "}," + '"' + "Body" + '"' + ":" + jss.Serialize(lstNh) + "}";
                    return abc;
                   
                }
            }
            catch (Exception e)
            {
                string error = "Please enter a valid latitude and longitude";
                return error;
               
            }
            }
            else
            {
             
                string error = "Api is not valid testing";
                return error; 
            }
        }

       
    }
}

and the web .config file
XML
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="apiKey" value="59AB8FB9-E60D-4FC8-9F10-80330AAFD741"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>

  <customErrors mode="Off">

  </customErrors>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfService2.RestTestService" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address="" binding="webHttpBinding" contract="WcfService2.IRestTestService" behaviorConfiguration="web">
          <!--
              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>

      </service>
    </services>
    <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="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


can any one help me
Posted
Updated 18-Jul-13 7:43am
v2

1 solution

 
Share this answer
 

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