无法从webhttpbinding调用wcf [英] cant call wcf from webhttpbinding

查看:73
本文介绍了无法从webhttpbinding调用wcf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有简单的wcf,我不知道如何测试它并用浏览器调用我的函数?
这是我的服务:

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

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 :

namespace MYWcfService
{
   [ServiceContract]
    public interface IService1
    {

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


    }

}



这是我的web.config:



and this is my web.config :

<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>



这就是服务标记:
<%@ ServiceHost语言="C#" Debug ="true" Service ="MYWcfService.Service1" CodeBehind ="Service1.svc.cs"%>

当我在.net中运行服务项目时,IIS管理器将运行,但是由于它是webhttpbinding,因此无法显示我的服务.
我在浏览器中写" http://localhost/Service1.svc/GetData [



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.

推荐答案

.ajax({ 类型: 网址:fullUrl, 数据:d, contentType:``应用程序/json; charset = utf-8'', dataType:``json'', processData:``true'', 成功:功能(msg){alert(``OK''); }, 错误:函数(a,b,c){alert(``错误''+ a + b + c +>")); } }); } </script> </head> <身体> < form id ="form1" runat ="server"> < h1> JSONP服务客户端页面</h1> </form> < input type ="button" value ="click me" onclick ="WCFJSON()"/> </body> </html>
.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.


我发现3个可以测试我的wcf服务的示例抛出浏览器:)

http://dotnet.dzone.com/articles/creating-restful-wcf-service [ ^ ]


http://www.asp.net-crawler. com/articles/WCF-WebServices/Creating-accessing-using-RestWCFService-browser.aspx [ http://www.dotnetcurry.com/ShowArticle.aspx?ID=728 [ ^ ]
I find 3 sample that i can test my wcf service throw Browser :)

http://dotnet.dzone.com/articles/creating-restful-wcf-service[^]


http://www.asp.net-crawler.com/articles/WCF-WebServices/Creating-accessing-consuming-RestWCFService-browser.aspx[^]


http://www.dotnetcurry.com/ShowArticle.aspx?ID=728[^]


这篇关于无法从webhttpbinding调用wcf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆