Quantcast
Channel: WCF, ASMX and other Web Services
Viewing all articles
Browse latest Browse all 555

Asynchronous WCF Method Call From jQuery

$
0
0

Hi all,

First off, I didn't know if this should go in the jQuery section or the WCF section but I'm thinking it's more of a WCF issue than a jQuery issue (I could be wrong).

I've been struggling with an issue where I'm not able to call a function through jQuery when using the Async Pattern in WCF. When  AsyncPattern = True isn't in the contract it works fine but as soon as I put it in the code breaks! It's saying 404 error for the web service call and the web service itself is saying: "Endpoint not found." Please help if you can!

Class:

Imports System.Threading

Public Class AsyncResult(Of T)
    Implements IAsyncResult

    Private _data As T = Nothing

    Public Sub New(ByVal data As T)
        _data = data
    End Sub

    Public ReadOnly Property Data() As T
        Get
            Return _data
        End Get
    End Property

    Public ReadOnly Property AsyncState() As Object Implements System.IAsyncResult.AsyncState
        Get
            Return DirectCast(_data, Object)
        End Get
    End Property

    Public ReadOnly Property AsyncWaitHandle() As WaitHandle Implements System.IAsyncResult.AsyncWaitHandle
        Get
            Throw New Exception("The method or operation is not implemented.")
        End Get
    End Property

    Public ReadOnly Property CompletedSynchronously() As Boolean Implements System.IAsyncResult.CompletedSynchronously
        Get
            Return True
        End Get
    End Property

    Public ReadOnly Property IsCompleted() As Boolean Implements System.IAsyncResult.IsCompleted
        Get
            Return True
        End Get
    End Property
End Class

WCF:

Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.Threading

<ServiceContract(Namespace:="")>
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerCall, ConcurrencyMode:=ConcurrencyMode.Multiple)>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class ajax
    ' Synchronous
    <OperationContract()> _
    <WebInvoke(RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)> _
    Public Function BeginTest(ByVal str As String) As String
        Return str
    End Function

    ' Asynchronous
    <OperationContract(AsyncPattern:=True)> _
    <WebInvoke(RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)> _
    Public Function BeginTest(ByVal str As String, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
        Return New AsyncResult(Of String)(str)
    End Function

    Public Function EndTest(ByVal result As IAsyncResult) As String
        Dim asyncResult As AsyncResult(Of String) = TryCast(result, AsyncResult(Of String))

        Return asyncResult.Data
    End Function
End Class

jQuery:

$.ajax({
            url: "Services/ajax.svc/BeginTest",
            type: "POST",
            async: true,
            data: '{"str":"' + "Success" + '"}',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {   // success callback
                alert(data.d);
            },
            error: function (error) {   // error callback
                alert("Fail");
            }
        });






Viewing all articles
Browse latest Browse all 555

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>