简体   繁体   English

WCF新手问题:从JavaScript调用方法

[英]WCF Newbie Question: Calling Methods from JavaScript

Greetings! 问候!

I'm new to WCF and I thought it would be similar to ASP.NET web services, but I'm unable to call a method from client-side JavaScript. 我是WCF的新手,我认为它类似于ASP.NET Web服务,但我无法从客户端JavaScript调用方法。 My web form looks like this: 我的网络表单如下所示:

<form id="form1" runat="server">
   <div>
      <asp:ScriptManager ID="ScriptManager1" runat="server">
         <Scripts>
            <asp:ScriptReference Path="~/test.js" />
         </Scripts>
         <Services>
            <asp:ServiceReference Path="~/MyService.svc" />
         </Services>
      </asp:ScriptManager>
   </div>
   <button onclick="test()">Click Me</button>
</form>

My service's interface looks like this: 我的服务界面如下所示:

namespace Test
{
    [ServiceContract(Namespace = "Test")]
    public interface IMyService
    {
        [OperationContract]
        void DoWork();

        [OperationContract]
        string SayHi();
    }
}

And here's its implementation: 这是它的实现:

namespace Test
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService : IMyService
    {
        public void DoWork()
        {
        }

        public string SayHi()
        {
            return "Hello, World!";
        }
    }
}

And finally the JavaScript: 最后是JavaScript:

function test() {
    Test.MyService.SayHi(SayHiSuccess, SayHiError);
}

function SayHiSuccess(result) {
    alert(result[0]);
}

function SayHiError(error) {
    alert(error.toString());
}

It appears that the service's SayHi() method never executes, although I'm not sure why or how to being troubleshooting. 似乎服务的SayHi()方法永远不会执行,虽然我不确定为什么或如何进行故障排除。 Any suggestions? 有什么建议?

You didn't post your web.config contents. 您没有发布您的web.config内容。 What binding are you using? 你使用什么装订? You should probably be using webHttpBinding . 你可能应该使用webHttpBinding

Also, it may help to know your .svc file contents. 此外,它可能有助于了解您的.svc文件内容。 Although I've never tried it, I understand that you don't have to modify web.config at all if you use WebScriptServiceHostFactory as your service host factory. 虽然我从未尝试过,但我知道如果您使用WebScriptServiceHostFactory作为服务主机工厂,则根本不需要修改web.config。 That's as simple as modifying your .svc file as follows: 这就像修改.svc文件一样简单,如下所示:

<%@ ServiceHost Service="MyService"
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
 %>

If all else fails, here are some resources for you: 如果一切都失败了,这里有一些资源给你:

Your code looks OK. 你的代码看起来不错。 Should be working by right. 应该是正确的。 You could try adding the WebService and the WebMethod attributes as well. 您也可以尝试添加WebService和WebMethod属性。

For debugging a WCF web service I normally use Fiddler to track HTTP calls while running the WCF code with a debugger attached (run in Visual Studio in most cases). 为了调试WCF Web服务,我通常使用Fiddler来跟踪HTTP调用,同时运行附加了调试器的WCF代码(在大多数情况下在Visual Studio中运行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM