Click here to Skip to main content
15,886,018 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
My service contract, named IService1, is as follows

C#
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,

XML
<?xml version="1.0"?>
   <configuration>

     <system.web>
       <compilation debug="true" targetFramework="4.0" />
     </system.web>
     <system.serviceModel>

       <behaviors>
         <serviceBehaviors>
           <behavior name ="ServiceBehavior">
             <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
             <serviceMetadata httpGetEnabled="true"/>
             <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
             <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"/>
       <!--
           To browse web app root directory during debugging, set the value below to true.
           Set to false before deployment to avoid disclosing web app folder information.
         -->
       <directoryBrowse enabled="true"/>
     </system.webServer>

   </configuration>


Script for consuming WCF service,
C#
<!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
C#
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...
Posted
Updated 11-Aug-14 18:10pm
v2
Comments
Kornfeld Eliyahu Peter 11-Aug-14 16:58pm    
Check that you application pool is set to framework 4.
Re-register framework 4 with IIS...
[no name] 12-Aug-14 0:05am    
Yes, I have confirmed these. Application pool has .net4.0. still no luck...
same error callback is getting executed
Kornfeld Eliyahu Peter 12-Aug-14 1:30am    
The registration can be more important! Run aspnet_regiis.exe -i from the proper folder of .NET installation!
KVPalem 12-Aug-14 1:19am    
Please provide error details
SRS(The Coder) 12-Aug-14 2:12am    
Have you tried using 'AjaxEnabledWCFService' its a preconfigured template that can be used directly from ajax.

1 solution

Finally got my mistake & it's solution.
Just needed to change the service element in config as:

XML
<services>
  <service name="WcfServiceDemo.Service1" behaviorconfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="WcfServiceDemo.IService1" behaviorconfiguration="EndPointBehavior" />
  </service>
</services>


where name ="WcfServiceDemo.Service1" is in format "namespace.ServiceImplementationName" & contract="WcfServiceDemo.IService1" is in format "namespace.ServiceContractName"
 
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