Consume WCF service from JQuery

I am trying to add a WCF service to my web control class project and allow my jquery client to consume the service. Ideally, I want to host the WCF service in the same project and allow a custom web control's (inside the same project) jQuery method consume the service. I'm not sure what I am doing wrong, but I am unable to make a connection between the jquery call and the service. Although there is no error, the break point on my service is never reached. Here is what I did:

  1. Right Click on project and select Add
  2. Select Web Service
  3. This creates three files: Service1.vb, app.config, and IService1.vb
  4. I edited the files to look like this:

Service1

Public Class Service1
    Implements IService1

    Public Function getUsers(ByVal prefixText As String) As List(Of String) Implements IService1.getUsers
        Dim myList As New List(Of String)
        With myList
            .Add("Some String")
            .Add("Another String")
        End With

        Return myList
    End Function
End Class

IService1

Imports System.ServiceModel   

<ServiceContract()>
Public Interface IService1

    <OperationContract()> _
    Function getUsers(ByVal prefixText As String) As List(Of String)

End Interface

And then I try to call it with the following jQuery:

 $.ajax({
        type: "POST",
        url: 'Service1.vb/getUsers',           
        data: '{"prefixText":"' + getText + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
          alert("success")

        },
        error: function (e) {
             alert("Failed")
        }
    });

As I said, the break point on my getUsers function is never reached and the jquery success/failure alerts are never raised either. If someone can tell me how to reach the service and/or how to alert the error in my jQuery, I'd appreciate it. I left out the app.config stuff but can add it if it would be helpful.

thanks

Answers


This is a terrible misunderstanding in your code. By default, WCF uses Soap and Javascript/Jquery does not provide an easy way to invoke a SOAP Service.

You should use WCF Web HTTP Programming Model to expose WCF service operations to non-SOAP endpoints, like a REST-like service (can be called from JS)

Iy your are using WCF 4, this is quite easy.

Service Contract

<ServiceContract()>
Public Interface IService1

    <OperationContract()>
    <WebInvoke(BodyStyle:=WebMessageBodyStyle.Bare, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
    Function getUsers() As List(Of String)

End Interface

Service Implementation

Public Class Service1
    Implements IService1

    Public Function getUsers(ByVal prefixText As String) As List(Of String) Implements IService1.getUsers
        Dim myList As New List(Of String)
        With myList
            .Add("Some String")
            .Add("Another String")
        End With

        Return myList
    End Function

End Class

Service1.svc

<%@ ServiceHost Language="VB" 
Service="MvcApplication2.Service1" 
CodeBehind="Service1.svc.vb" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

I won't explain you everything here, and continue reading here or with this example

Also note that WCF REST is less popular today since because of ASP.NET Web Api. I don't believe that WCF REST is deprecated, but to expose something on the Web, Web Api sounds like a better solution.


Need Your Help

sql query column value = column value - 1

mysql sql database

I need to -1 from a column on each row of a table and I was wondering if there was a way to do this with SQL rather than writing something in PHP?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.