Hello,
I am new to wcf rest services and I do not have that much idea about endpoint and behavior, we mention in web.config. However I got some tutorial and was able to create a service. Below is the service contract, wcf service file, we.cong and jquery service call code snippet. I am not sure about, every time when the service is getting called is goes to error block.
Please help me out. Thanks.
1)Service contract:
namespace IDM.Sedaru.Service
{
[ServiceContract]
public interface IHydraulicsService
{
[OperationContract]
[WebGet(UriTemplate = "TestService", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string TestService();
}
}
2) Service(.svc) file:
namespace IDM.Sedaru.Service
{
public class HydraulicsService : IHydraulicsService
{
public string TestService()
{
return "Test service is called";
}
}
}
3) Web.config file:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="false" />
<services>
<service behaviorConfiguration="WebServiceBehavior" name="IDM.Sedaru.Service.HydraulicsService">
<endpoint address="" behaviorConfiguration="jsonBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="IDM.Sedaru.Service.IHydraulicsService" />
<endpoint address="soap" binding="basicHttpBinding" contract="IDM.Sedaru.Service.IHydraulicsService" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None"></security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Buffered" useDefaultWebProxy="true" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
</system.serviceModel>
4) JQUERY Call, inside a new project file:
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/jscript">
$(document).ready(function () {
$(".btn").click(function () {
$.ajax({
type: "GET",
url: "http://localhost:56158/HydraulicsService.svc/TestService",
processData: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
data:{},
success: function (data) {
alert(data);
},
error: function (xhr, status, error) {
alert(error);
}
});
});
});
</script>