WCF Newbie Question: call methods from JavaScript

Hello!

I am new to WCF and I thought it would look like ASP.NET web services, but I cannot call the method from client-side JavaScript. My web form is as follows:

<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 interface is as follows:

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

        [OperationContract]
        string SayHi();
    }
}

And here is its implementation:

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

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

And finally JavaScript:

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

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

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

It seems that the service's SayHi () method is never executed, although I'm not sure why and how to be troubleshooting. Any suggestions?

+3
source share
2 answers

You have not posted your web.config content. What binding are you using? You should probably use webHttpBinding.

, .svc . , , web.config, WebScriptServiceHostFactory factory. , .svc :

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

, :

+3

. . WebService WebMethod.

- WCF Fiddler HTTP WCF ( Visual Studio ).

0

Source: https://habr.com/ru/post/1705831/


All Articles