IBM WorkLight Studio Client Side Adapter Call - ibm-mobilefirst

Will it be possible for the Worklight Adapter to be called synchronously?
This is the client code I am using:
function GetAccount(){
var acctresponse;
//Adapter call to get accounts from Server
//Registered Onsuccess and OnFailure
onSuccess: function(response){acctresponse=response},
onFailure: function(error){acctresponse=null;}
//UI Code dependent on above acctresponse.
}
Being Client adapter synchronous, the UI code is executed before the response arrives.
Can anyone suggest what is the best approach to handle a situation like this?

Request to adapter is issued using AJAX, which stands for Asynchronous JavaScript and XML. Therefore the answer is no since underlying transport layer is asynchronous.

As was stated above the adapter calls are by nature asynchronous - as are many of the javascript APIs used in web and mobile development. To ensure that the UI code is only executed after the adapter call complets you should invoke it from the onSuccess callback function.

Related

Using NServiceBus in a synchronous API

I'm exposing an API to a vendor where that vendor requires the API to be synchronous. I however would like to use NServiceBus on the backend of it to make good use of our other architecture.
Is there a product/framework that can be used to do support this flow?
Are there any thoughts/considerations in choosing this approach?
The API is as said synchronous where we will transform the request a bit and then put it on an NServiceBus queue for further processing by other systems.
After the message has been sent to a queue we should wait for the other system to complete its actions and be woken up again when the reply message is received.
Pseudo code:
void APICall(String someMessage) {
var msgBusMessage = new { json = someMessage, ID = Guid.NewID() };
NServiceBus.EnqueueMessage(msgBusMessage);
var returnMessage = NServiceBus.WaitForReplyMessageWithID(msgBusMessage.ID);
return returnMessage;
}
NServiceBus has a feature called Callbacks which is designed to handle this type of interaction. It works almost exactly like the snippet you have provided. You send a request via NServiceBus and then get a Task that will eventually contain the response. You can await that task and return the result to the synchronous API.
To set it up:
Add a reference to the NServiceBus.Callbacks package (in both the sender and the receiver endpoints)
Enable callbacks by calling endpointConfiguration.EnableCallbacks(). If an endpoint returns replies but does not request request synchronous callbacks then you should use endpointConfiguration.EnableCallbacks(makesRequests: false)
Any endpoint that makes callback requests should be made Uniquely Addressable endpointConfiguration.MakeInstanceUniquelyAddressable(someUniqueIdThatSurvivesRestarts). This allows the response message to be routed to the specific endpoint instance that is waiting for it.
In the caller, use the following var response = await endpoint.Request<ResponseMessage>(new RequestMessage { ... })
In the receiver create a handler for RequestMessage and return context.Reply(new ResponseMessage {...})
You can read more about this feature (including some caveats around when you should and shouldn't use it) in the documentation. There is also a sample showing the feature in use.

How to intercept MobileFirst adapter's resource method call

Is it possible to intercept MobileFirst adapter's resource method call? The purpose of such intercepting is adding common logic for all adapter's resource methods into one place (e.g. appending HTTP response with particular headers).
I don't think you can "intercept it", but you can just write your code in a way that all adapter calls first go through some helper function where you add your required headers before "releasing" the call from the client and have it sent to the server.

Calling WCF in async manner from client makes the WCF service also act in async manner?

When a WCF service is called in an async manner from a client, then we know that the client app is NOT blocked.
But is the thread on the WCF side blocked while executing the async method call OR the WCF releases the thread it uses to initiate the method call ?
For example, if I call the 'Add' method in an async manner from an ASP.Net app, as in code below, will the service instance in back-end wait till method completes? The WCF uses an InstanceContext of PerCall.
CalculatorClient client = new CalculatorClient();
// AddAsync
double value1 = 100.00D;
double value2 = 15.99D;
client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
client.AddAsync(value1, value2);
WCF support for asynchronous calls are strictly a client-side feature only.
In-fact the service has no way (and should not have) of telling the difference between two clients, one making a synchronous call and the other making an asynchronous call.
This is true regardless of whether the client is making a call via an async proxy, or directly via async invocation.
It is easy to show this is true with a thought experiment; ANY wcf service can be called asynchronously, and additionally this is REGARDLESS of the binding selected - therefore this must be solely a client-side facility.

wcf async callback

I have WCF service which sends messages to its clients. I would like to call callback methods asynchronously. I have read this answer:
WCF asynchronous callback
But there is one problem. When I am generating IMyServiceCallback from WebServiceReference it contains both synchronous and asynchronous methods (while on the service side there is callback contract with only asynchronous methods - BeginCallbackMethod and EndCallbackMethod). What is more when I call from MyService to calback BeginCallbackMethod, on the client side (in callback implementation) it is using synchronous CallbackMethod. The question is why? Is there any way to configure it?
By default WCF will call the synchronous version of an operation if both sync and async are present; I don't know how (or if) you can change that logic, but one thing you can do is to simply remove the synchronous method from the generated callback interface. The callback code should continue to work, and it will use the asynchronous implementation instead. You can also just remove the [OperationContract] attribute from the synchronous version, to the same effect.

Async WFC server side like IHttpAsyncHandler

I'd like to be able to create a WCF service that the client see as synchronous, but is implemented server side as asynchronous. I know that ASP.NET allows me to do this by implementing the IHttpAsyncHandler, but can't seem to find the equivalent in WCF.
This is a service that exists already and we'd like to move over to an asynchronous implement server side, without changing the client. Is this possible?
If you are on .Net 4.5 change the OperationContract methods in service contract interface to return Task<type> instead of type. In the class that implements the interface use the async modifier on the methods and use await inside following the Task based Asynchronous Pattern
See http://msdn.microsoft.com/en-us/library/ms734701
http://channel9.msdn.com/Events/TechEd/NorthAmerica/2012/DEV326