Click here to Skip to main content
15,896,606 members

cant call wcf from webhttpbinding

Moonaly asked:

Open original thread
i have simple wcf , i dont know how to test it throw calling my function with browser?
this is my service :

C#
using System.ServiceModel.Activation;

namespace MYWcfService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1 : IService1
    {

        public string GetData()
        {
            return string.Format("You entered:  ");
        }


    }
}


and this is my interface :

C#
namespace MYWcfService
{
   [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        [WebInvoke(UriTemplate = "GetData",
                   Method = "Post",
                   ResponseFormat = WebMessageFormat.Json,
                   BodyStyle = WebMessageBodyStyle.Wrapped)]
        string GetData();


    }

}


and this is my web.config :
XML
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="CacheFor10Seconds" duration="10" varyByParam="none"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true"/>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttpBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="MYWcfService.Service1">
        <endpoint address="" behaviorConfiguration="webHttpBehavior" binding="webHttpBinding" bindingConfiguration="webHttpBindingWithJsonP" contract="MYWcfService.IService1"/>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>


and this is service markup :
<%@ ServiceHost Language="C#" Debug="true" Service="MYWcfService.Service1" CodeBehind="Service1.svc.cs" %>

when i run the service project in .net, IIS manager will run but because it is the webhttpbinding , it cant show my service.
i write in browser ""http://localhost/Service1.svc/GetData[^]
but it shows nothing.
i dont know myservice is work or not.
i want to call this service from html like this :
<pre lang="HTML">
<html>
<head>
    <title>JSONP Service Client Page</title>
    <script type="text/javascript" ></script>
    <script src="JS/JQuery.js" type="text/javascript"></script>
    <script src="jquery-1.7.2.js"></script>
    <script type="text/javascript">
        function WCFJSON() {

            type = "POST";
            url =  "http://Localhost/Service1.svc";
            methodName = "GetData";
            data = "";
            ajaxCall(type, url, methodName, data)
        }

        function ajaxCall(type, url, methodName, data) {

            var fullUrl = url + "\\" + methodName;
            if (navigator.appName == 'Netscape')
                fullUrl = url + '/' + methodName;
            var d = "{}";
            if (data != "") d = '{"data":"' + data + '"}';
            else d = "{}";
            $.ajax({
                type: type,
                url: fullUrl,
                data: d ,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                processData: 'true',
                success: function (msg) {  alert('OK'); },
                error: function (a, b, c) {  alert('error' + a + b + c + ">"); }
            });
        }
    </script>

</head>
<body>
<form id="form1" runat="server">

    <h1>
        JSONP Service Client Page</h1>
</form>

<input type="button" value="click me" onclick="WCFJSON()" />

</body>
</html>

i searched alot but couldent find and solve my problem.
i need to call services in my project , but when i cant working with this simple service , i realy diapointed myself. please someone help me.
Tags: WCF, Ajax

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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