简体   繁体   中英

WCF rest WebInvoke get method not working returning 404 error

I created a basic WCF REST service with default methods. It is working when i request for svc file, but it returns 404 error while placing a request with rest parameters. ie it gives response when i call http://localhost/FirstWCFRestApp/RestServiceImpl.svc but returns 404 error when i called http://localhost/FirstWCFRestApp/RestServiceImpl.svc/xml/12 .

It is very basic service with only 1 method and confusing me as why its not working. I have pasted the code below.

Please let me know where it went wrong and why its not working.

Interface `

using System.ServiceModel;
using System.ServiceModel.Web;

namespace FirstWCFRestApp
{
    // 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",UriTemplate="/xml/{id}",RequestFormat=WebMessageFormat.Json,
            ResponseFormat=WebMessageFormat.Json)]
        string DoWork(string id);


    }
}

Class File `

namespace FirstWCFRestApp
{
    // 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 DoWork(string id)
        {
            return "You requested Id is "+ id;
        }


    }
}

SVC file

<%@ ServiceHost Language="C#" Debug="true" Service="FirstWCFRestApp.RestServiceImpl" CodeBehind="RestServiceImpl.svc.cs" %>

Web.Config

<?xml version="1.0"?>
<configuration>



  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>


    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <behaviors>
      <endpointBehaviors>
        <behavior name="FWRBehaviour">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>

        <behavior name="htBehaviour">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="FirstWCFRestApp.RestServiceImpl" behaviorConfiguration="htBehaviour">
        <endpoint address="Stud" binding="webHttpBinding"
                  contract="FirstWCFRestApp.IRestServiceImpl" behaviorConfiguration="FWRBehaviour"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
      </service>
    </services>
  </system.serviceModel>

  <system.webServer>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>

The address http://localhost/FirstWCFRestApp/RestServiceImpl.svc is the service metadata address. The practical service address should base on the UriTemplate property and the Address property of the service address.

UriTemplate="/xml/{id}"

binding="webHttpBinding" contract="FirstWCFRestApp.IRestServiceImpl" >behaviorConfiguration="FWRBehaviour">

In addition, the Method property of the WebInvoke should be capitalized.

[WebInvoke(Method ="GET",ResponseFormat =WebMessageFormat.Json,UriTemplate ="/xml/{id}")]

In summary, the service address should be,

http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12

Feel free to let me know if there is anything I can help with.

Like Abraham said:

Web Invoke Format: {SVC Path}/{webHttpBinding Endpoint Address}/{WebInvoke UriTemplate}

In your case, it should be:

http://localhost/FirstWCFRestApp/RestServiceImpl.svc/Stud/xml/12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM