Click here to Skip to main content
15,921,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
namespace IDM.Sedaru.Service
{
    public class HydraulicsService : IHydraulicsService
    {
      public string TestService()
        {
            return "Test service is called";
        }
    }
}

3) Web.config file:
XML
<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:
HTML
<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">

    //Call Rest WCF Service
    $(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>
Posted
v3
Comments
Do you see any error in Developer Tool's console?

And yes, don't include more than one jQuery files in code. Delete one.
NAPorwal(8015059) 6-Mar-14 5:12am    
Are you talking about, using wcftestclient in VS command prompt. if this is you are talking about, it returns the expected result.
No. I am asking about the Browser Developer Tool Console, which you can see on clicking F12.
If you are using Firefox, then you can add Firebug add-in and check inside that.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900