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>