Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating my first rest api. I followed below link. http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide.Link is excellent.
Steps 1: I have created new project WCFservice application.Deleted already created Interface and class.Added new service into it.Project name is 'RestService' and service name is 'IRestServiceImpl'Working well when accessing through browser.But when I try to consume it through client/web application ,I am getting error.I unable to see any endpoint in client's webconfig file.When I am adding service reference ,it is showing 2 reference found.How 2?So again I deleted and entered new service reference ,that time I unchecked reused type in referenced asseblies" but no success.What to do now? Step 4 : written here because in below box unable to fit all code of step 4


<configuration>

<appsettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true">

<system.web>
<compilation debug="true" targetframework="4.5">
<httpruntime targetframework="4.5">

<system.servicemodel>
<services>
<service name="RestService.RestServiceImpl" behaviorconfiguration="ServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorconfiguration="web">


<behaviors>
<servicebehaviors>
<behavior name="ServiceBehaviour">
<!-- 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">


<endpointbehaviors>
<behavior name="web">
<webhttp>



<protocolmapping>
<add binding="basicHttpsBinding" scheme="https">

<servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true">

<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">




What I have tried:

C#
step 2: Interface

namespace RestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
    [ServiceContract]
    public interface IRestServiceImpl
    {
        [OperationContract]
        [WebInvoke(Method="GET",ResponseFormat=WebMessageFormat.Xml,BodyStyle=WebMessageBodyStyle.Wrapped,UriTemplate="xml/{id}") ]
        string XmlData(string id);

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/{id}")]
        string JSONData(string id);
    }
}
step 3: Class Implementing Interface
namespace RestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "RestServiceImpl" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select RestServiceImpl.svc or RestServiceImpl.svc.cs at the Solution Explorer and start debugging.
    public class RestServiceImpl : IRestServiceImpl
    {
      public  string XmlData(string id)
        {
            return "Your id is " + id;
        }

      public string JSONData(string id)
      { 
          return "Your id is " + id;
      }
    }
}
Step 4 : Webconfig
<?xml version="1.0"?>
<configuration>

  <sappSettings>
    <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="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- 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>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </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>

</configuration>
Posted
Updated 13-Nov-16 19:24pm
v3

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