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
According to your description, recommend you configure tracing for your service. This will generate a trace file that you can view with the trace file viewer and get detailed information about exceptions that may be occurring within the service. For more
information on configuring tracing, see:
Configuring Tracing.
It seems the problem comes from the asyn method, you can set breakpoints to debug, and make sure apply async pattern and call them properly. The calls like:
var result = BeginTest(null, null,null);
string value = EndTest(result);
And now that I look over the previous answer again and think about it I'm pretty sure I've found my problem...I'm trying to call the async method directly from jQuery when instead I should call the regular method and return the async result. From my tests
it works and *looks* like it's working asynchronously but it's hard to tell. More tests are needed to confirm but it I'm going to say this is resolved since my real problem was just getting the async pattern to run through jQuery.
Targe
Member
56 Points
59 Posts
Asynchronous WCF Method Call From jQuery
Aug 03, 2012 12:59 AM|LINK
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 ClassWCF:
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 ClassjQuery:
$.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"); } });Targe
Member
56 Points
59 Posts
Re: Asynchronous WCF Method Call From jQuery
Aug 03, 2012 01:17 AM|LINK
-- Redacted --
Haixia Xie -...
Contributor
3087 Points
298 Posts
Microsoft
Re: Asynchronous WCF Method Call From jQuery
Aug 06, 2012 06:47 AM|LINK
Hi,
According to your description, recommend you configure tracing for your service. This will generate a trace file that you can view with the trace file viewer and get detailed information about exceptions that may be occurring within the service. For more information on configuring tracing, see: Configuring Tracing.
More about WCF troubleshooting, you may refer:
http://msdn.microsoft.com/en-us/library/aa702636.aspx
Best Regards,
Feedback to us
Develop and promote your apps in Windows Store
Targe
Member
56 Points
59 Posts
Re: Asynchronous WCF Method Call From jQuery
Aug 07, 2012 05:20 PM|LINK
Thanks Haixia,
The tracing tool is definitely helping out but still haven't gotten it working at this moment. For what it's worth the error it's giving is:
"The incoming HTTP request's URI 'http://localhost/Services/ajax.svc/BeginTest' does not match any service operation."
oak_silver
Member
418 Points
64 Posts
Re: Asynchronous WCF Method Call From jQuery
Aug 08, 2012 09:21 AM|LINK
It seems the problem comes from the asyn method, you can set breakpoints to debug, and make sure apply async pattern and call them properly. The calls like:
http://stackoverflow.com/questions/5959138/wcf-rest-not-processing-asynchronously
Targe
Member
56 Points
59 Posts
Re: Asynchronous WCF Method Call From jQuery
Aug 10, 2012 05:50 PM|LINK
I can call the method in code behind without issues. My problem is it doesn't get called at all through jQuery.
Targe
Member
56 Points
59 Posts
Re: Asynchronous WCF Method Call From jQuery
Aug 10, 2012 06:03 PM|LINK
And now that I look over the previous answer again and think about it I'm pretty sure I've found my problem...I'm trying to call the async method directly from jQuery when instead I should call the regular method and return the async result. From my tests it works and *looks* like it's working asynchronously but it's hard to tell. More tests are needed to confirm but it I'm going to say this is resolved since my real problem was just getting the async pattern to run through jQuery.
Thanks for the help guys, it's much appreciated!