Reputation: 3927
I am new to the WCF and writing my first WCF application. I am not able to call service from json request
Following are the details of project/solution
I have total 3 projects in my solution
1. Service Library Project(MyFirstRESTfulService)
2. Host application(WCFService2)
3. Service Client(ServiceClient)
1. MyFirstRESTfulService
Class IEmployeeService.cs
namespace MyFirstRESTfulService
{
[ServiceContract()]
public interface IEmployeeService
{
[WebGet(UriTemplate = "Employee", ResponseFormat=WebMessageFormat.Json )]
[OperationContract]
List<Employee> GetAllEmployeeDetails();
}
}
Class EmployeeService.cs
namespace MyFirstRESTfulService
{
[AspNetCompatibilityRequirements(RequirementsMode= AspNetCompatibilityRequirementsMode.Allowed )]
public class EmployeeService: IEmployeeService
{
public List <Employee> GetAllEmployeeDetails()
{
return EmployeeData.Instance.EmployeeList;
}
}
}
2. WCFService2
Service.svc
Web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="false" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyFirstRESTfulService.EmployeeService">
<endpoint address="http://localhost:8046/WCFService2/Service.svc" behaviorConfiguration="Web" binding="webHttpBinding" contract="MyFirstRESTfulService.IEmployeeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
3. ServiceClient
Default.aspx
<body>
<script src="Script/jquery1.9.0.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery.support.cors = true;
function RefreshPage() {
var serviceUrl = "http://localhost:8046/WCFService2/Service.svc/MyFirstRESTfulService/GetAllEmployeeDetails";
debugger;
$.ajax({
type: "GET",
url: serviceUrl,
dataType: 'json',
contentType: "application/json; charset=utf-8",
success: function (data) {
var itemRow = "<table>";
$.each(data, function (index, item) {
itemRow += "<tr><td>" + item.EmpId + "</td><td>" + item.Fname + "</td></tr>";
});
itemRow += "</table>";
$("#divItems").html(itemRow);
},
error: ServiceFailed
});
}
</script>
</script>
<form id="form1" runat="server">
<input type="button" onclick="RefreshPage()" name="btnRefesh" value="Refresh" />
<div id="divItems">
</div>
</form>
</body>
But these things not working at all.While clicking on button nothing is happening.Can you please tell me where I went wrong.
I am getting following error
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Endpoint not found.</p>
</div>
</body>
</html>
Thanks in Advance
Upvotes: 0
Views: 281
Reputation: 4108
You will need to use the webHttpBinding instead of the wsHttpBinding if you want to be able to communicate over HTTP with POX or JSON.
Your config should look something like
<services>
<service behaviorConfiguration="Default" name="Example.MyService">
<endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="Example.IServiceContract" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000/MYService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
Also annotate your operation contracts with "RequestFormat=WebMessageFormat.Json" & "ResponseFormat = WebMessageFormat.Json"
Hope this helps,
Upvotes: 2