Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Excuse me
WCF REST Service giving full table data in json view but when i try to get record GetById.
It is giving "endpoint not fuond" error
Please tell me how to resolve it. I am new to WCF Services
Here is web.config
C#
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WCFDB.ServiceContract">
        <endpoint address="" behaviorConfiguration="restfulBehavior" binding="webHttpBinding" bindingConfiguration="" contract="WCFDB.IServiceContract"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/ServiceContract.svc"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="restfulBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 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>
  <connectionStrings>
    <add name="tawaseelEntities" connectionString="metadata=res://*/NewsModel.csdl|res://*/NewsModel.ssdl|res://*/NewsModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=tawaseel;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

and here is IServiceContract.cs
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 WCFDB
{
    [ServiceContract]
    public interface IServiceContract
    {
        [OperationContract]
        [WebGet (ResponseFormat=WebMessageFormat.Json)]
        List<news> GetNewsList();

        [OperationContract]
        //[WebGet]
        [WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "news/{id}")]
        news GetNewsById(string id);
    }
}

Here is ServiceContract.svc
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFDB
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ServiceContract" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select ServiceContract.svc or ServiceContract.svc.cs at the Solution Explorer and start debugging.
    public class ServiceContract : IServiceContract
    {
        public List<news> GetNewsList()
        {
            using (tawaseelEntities entities = new tawaseelEntities())
            {
                return entities.news.ToList();
            }
        }
        public news GetNewsById(string id)
        {
            try
            {
                int Nid = Convert.ToInt32(id);
                using (tawaseelEntities entities = new tawaseelEntities())
                {
                    return entities.news.SingleOrDefault(news => news.id == Nid);
                }
            }
            catch
            {
                throw new FaultException("Something went Wrong");
            }
        }
    }
}

and here the url that is working correctly
http://localhost:42813/ServiceContract.svc/GetNewsList[^]
and here that is giving endpoint error
http://localhost:42813/ServiceContract.svc/GetNewsById/3[^]

Thankss in advance
Posted
Updated 22-May-14 3:20am
v2
Comments
Prasad Khandekar 22-May-14 9:28am    
Hello Arshad,

Have you tried calling this web service as http://localhost:42813/ServiceContract.svc/news/2. Also suggest you to go through this CP article (http://www.codeproject.com/Articles/571813/A-Beginners-Tutorial-on-Creating-WCF-REST-Services).

Regards,
Khurram Arshad 23-May-14 0:34am    
wow my WCF is workingggg
thanks alot #Parsad Khandekar as i mentioned i am new so i was just trying GetById but now by table name its working thanks
Yogesh Wani 17-Apr-15 7:02am    
Hey i am facing same issue "Endpoint Not Found" what need to change?

1 solution

try this:
http://localhost:42813/ServiceContract.svc/news/3

and see this:
REST and WCF[^]

have a good day.
 
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