1

I was just trying to create very simple WCF service that will interact with javascript ajax calls (REST), but its not working for me, seems like I'm lost:

Service Interface

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Wcf.App
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(
            UriTemplate = "GetData/{value}", 
            ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        string getEmpData(string value);
    }
}

Implementation:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Wcf.App
{
    public class Clocked
    {
        public string date { get; set; }
        public string type { get; set; }
    }

    public class DATA
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string emailAddress { get; set; }
        public string phoneNum { get; set; }
        public string image { get; set; }
        public string title { get; set; }
        public List<Clocked> clocked { get; set; }
    }

    public class RootObject
    {
        public DATA DATA { get; set; }
    }

    public class Service1 : IService1
    {
        public string getEmpData(string test)
        {
            try
            {
                return " hello " + test;
                }
                catch (Exception ex)
                {
                    return "Can not open connection! " + ex.Message + "  Connection: " + connetionString;
                }
            }
            catch (SqlException e)
            {
                return e.Message;
            }
        }
    }
}

Now when I try to call this service using jQuery AJAX, it throws 404 not found error.

JS:

 $.ajax({
                url: "Service1.svc/GetData/3046722425",
                type: "GET",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                }
            });

Configuration:

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
    <bindings>
      <customBinding>
        <binding name="basicConfig">
          <binaryMessageEncoding/>
          <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864" />
        </binding>
      </customBinding>      
      <webHttpBinding>
        <binding name="basicConfig">
          <security mode="None">
            <transport clientCredentialType="Basic"/>
          </security>
        </binding>
        <binding>
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <client/>
    <services>
        <service name="Wcf.App.Service1" behaviorConfiguration="ServiceBehaviour">
          <!--<endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="Wcf.App.IService1"/>-->
          <endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="Wcf.App.IService1"/>
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <behavior name="REST">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="ServiceBehaviour">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
        <behavior name="REST">
          <webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json"/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>
  </system.serviceModel>
Mox Shah
  • 2,967
  • 2
  • 26
  • 42
  • can you hit the url in the browser and see if you get a positive response:? – qamar Jan 06 '16 at 06:10
  • Nope, I tried hitting this URL `http://localhost/WCFServices/Service1.svc/GetData/3046722425` but its not working. **returns 404** – Mox Shah Jan 06 '16 at 06:12

2 Answers2

0

Please make sure you've activated WCF components from here.

HTTP Error 404.3 - Not Found" while browsing wcf service on Windows Server 2008(64bit)

Community
  • 1
  • 1
Cyrus
  • 2,261
  • 2
  • 22
  • 37
-1

Try to hit the service from your browser once(discarding the methodname Getdata and the param value) or add it to the WCFTestClient application.