Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted
Comments
Moonaly 29-May-12 1:02am    
i mean by "IIS manager" , "WCF test client". i correct my sentence :
"when i run the service project in .net, WCF test client will run but because it is the webhttpbinding , it cant show my service."
Jim Jos 29-May-12 2:21am    
Have tested it with basichttpbinding first? Please use basichttpbinding and see whether its working.. then we can be sure its only due to webhttpbinding..

1 solution

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