Search code examples
c#jsonwcfhttp-status-code-404http-error

Test WCF with JSON Response


I have a WCF Service that return a json. I want to test it using Fiddle (this is the application that is used in the tutorial i was following). The problem is that i get HTTP 404 error when invoking "AddAngajat" for example.

Implementation of the service :

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, AddressFilterMode=AddressFilterMode.Any)]
    public class AngajatService : InterfaceAngajat
    {
        List<Angajat> listaAngajati =null;

        public AngajatService()
        {
            if (listaAngajati == null)
                listaAngajati = new List<Angajat>();
        }

        [WebInvoke(Method="POST", UriTemplate = "AddAngajat", ResponseFormat=WebMessageFormat.Json)]
        public void AddAngajat(Angajat angajat)
        {
            listaAngajati.Add(angajat);
        }
        public Angajat[] GetAllAngajati()
        {
            listaAngajati.Add(new Angajat() { idAngajat = "2f34", nume = "nume1" });
            return listaAngajati.ToArray();
        }
    }

App.config :

 <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="testJSON.AngajatService">
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <endpoint address="json" behaviorConfiguration="JSONEndpointBehavior"
          binding="webHttpBinding" bindingConfiguration="" contract="testJSON.InterfaceAngajat" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/testJSON/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="JSONEndpointBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

Any idea on how to fix this ? Other way of testing it ?


Solution

  • You need to call http://localhost:8732/testJSON/json/AddAngajat and it should work.

    Otherwise include your client code (or a snippet).

    UPDATE

    With the changes you mentioned, URL must be this http://localhost/testJSON/Service.svc/json/AddAngajat