My service contract, named IService1, is as follows
using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfServiceDemo
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string GetData(int Value);
}
}
Here's my service implementation,
using System.ServiceModel.Activation;
namespace WcfServiceDemo
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(int Value)
{
return string.Format("You entered: {0}", Value);
}
}
}
My web.config,
="1.0"
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name ="WcfDemoService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="EndPointBehavior" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Script for consuming WCF service,
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Enter a number:<input type="text" id="txtNum"/>
<input type="button" onclick="AlertEnteredNum();"/>
</div>
</form>
<script type="text/javascript" src="Script/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function AlertEnteredNum() {
var Value = $('#txtNum').val();
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: 'Service1.svc/GetData',
data: '{"Value": "' + Value + '"}',
dataType: "json",
processData: false,
success: function (data) {
alert("Success: " + data.d);
},
error: function (result) {
alert("error: " + result);
}
});
}
</script>
</body>
</html>
Always error callback
error: function (result) {
alert("error: " + result);
}
gets executed
I think I need to host on IIS, as I am setting compatibility mode in my config.
But, When I host this service on IIS, I am getting parser error as " The value for the targetFramework attribute is invalid: '4'. Error: FrameworkName cannot have less than two components or more than three components.
Parameter name: frameworkName."
Any suggestion...