WCF Service - Async How to? - wcf

I am consuming a 'third party' WCF service. I am making to call to their method in Async mode from C# console application.
The order I am making call is
1. WCF.MethodA (Async)
2. WCF.MethodB (Async)
3. WCF.MethodC (Async)
I wanted to raise an event (which makes call to 'MethodB') after call to 'MethodA' is successfully complete. Can anyone tell me how to do that? Is that something the service needs to support (raising events)?
Thanks

If you turn on the asynchronous option when you add the service reference, you'll get asynchronous calls with events.
For example, you should get WCF.MethodAAsync with a WCF.MethodACompleted event automatically. You can subscribe to the completion events, and start the next method upon completion.

Related

Problems with OpenRTI callbacks

How in the OpenRTI to make the rti send callbacks? For example, reserve a name with the reserveObjectInstanceName
_rtiAmbassador->reserveObjectInstanceName(name);
Errors:
terminate called after throwing an instance of
'rti1516e::ObjectInstanceNameNotReserved'
If you are using the HLA 1516e API (as opposed to HLA 1516 or HLA 1.3) when you make a call to connect you can specify a CallbackModel which is either SYNCHRONOUS or ASYNCHRONOUS. In a synchronous callback model, the federate has to call the method evokeCallback in order to trigger the RTI to send whatever is queued up. In asynchronous, the callbacks are sent automatically.
What you should do in this instance is have something like this:
_rtiAmbassador->reserveObjectInstanceName(std::wstring(L"MyObject"));
_rtiAmbassador->evokeMultipleCallbacks();
MyFederateAmbassador::objectInstanceNameReservationSucceeded(std::wstring const & name){
_rtiAmbassador->registerObjectInstance(handle, std::wstring(L"MyObject"));
}
After each call to RTI, to get a callback, you need to call
evokeCallback()

How to tell if middleware contains a Run()?

Is there any way to tell in ASP.NET Core if any given middleware will contain a Run() call which will stop the pipeline? It seems that UseMvc() is one big one, but I am not even certain about that, I just keep reading that it needs to go at the end, I assume it is because it contains a call to Run().
Perhaps there is a way to generate a visualisation of the pipeline for all middleware currently in use, showing which one contains the Run() call?
There is no sure way to tell, beyond reading documentation on each specific piece of middleware.
quoting itminus in the comments on my question:
Not only Run(), but also MapWhen() will terminate the process. Also, anyone could create a custom middleware that doesn't invoke the next delegate and then cause to a terminate.
It's the duty of middleware to determine whether there's a need to to call next. There's no built-in way to visualize the pipeline except you read the document/source code. That's because all the middlewares will be built into a single final delegate at startup time. When there's an incoming message, the final delegate will be used to process requests. As a programmer, we know what will be done by the middlewares, we know the time when it branches, and we know the time it terminates that's because we write the code. But the program won't know it until it actually runs just because the final delegate is built at startup time.

Load xml file on start of Windows 8.1 Universal App

in my Windows 8.1 universal app I want to retrieve a local XML file on application start. But I'm confused with async / await. I use GetFileAsync, so it needs to be in an async function. But I could not call it from the initialization of the app. When I just call the function it says i need to add await. If I await, it says I could use await only in an async method, and therefore I could not do it in the initialization. So how could this be done?
Many thanks!
See here: How to call asynchronous method from synchronous method in C#?
"If you have a simple asynchronous method that doesn't need to synchronize back to its context, then you can use Task.WaitAndUnwrapException"
So put the code into a separate method and use the method above or the second method RunTask.

What is the difference bewteen handler and callback function?

In my current project, there are lots of networking code, and it use the event handler to handle the input message. Is this mechanism different with the call back function ?
Typically not much. The handler is usually used in the context of a UI application where the UI control will call the handler to handle a UI event. The callback function is traditionally used from the C days (Function pointers) and also in the C++ (Functors) world.
As a general concept I would say that the call back functions are primarily used for Asynchronous execution. Where for example, client side function must look something up on the server and it may take a while. So instead of blocking it says :"Call back at this number (myCallBackFunction) when you are done looking up stuff on the server".
Now event handlers are just that: they handle some predefined events. Usually they wait for users to do something like click a button and then they spring into action. They typically but not necessarily expect some sort of input.
Hope this helps.

how to cancel WCF service call?

I have a WCF function that is executing long time, so I call the function in UI with backgraundworker... I want to give a feature to cancel the execution, so I abort IComunicationObject, the problem is that Service execution is not stoping, Is there any way to stop Service execution in this case?
You may not need a BackgroundWorker. You can either make the operation IsOneWay, or implement the asynchronous pattern. To prevent threading issues, consider using the SynchronizationContext. Programming WCF Services does a great job at explaining these.
Make a CancelOperation() method which sets some static ManualResetEvent in your service. Check this event in your Operation method frequently. Or it can be CancelOperation(Guid operationId) if your service can process multiple operation calls concurrently.
One important thing to understand if you're using the Async calls is that there's still no way to cancel a request and prevent a response coming back from the service once it's started. It's up to your UI to be intelligent in handling responses to avoid race conditions. Fortunately there's a simple way of doing this.
This example is for searching orders - driven by a UI. Lets assume it may take a few seconds to return results and the user is running two searches back to back.
Therefore if your user runs two searches and the first search returns after the second - you need to make sure you don't display the results of the first search.
private int _searchRequestID = 0; // need one for each WCF method you call
// Call our service...
// The call is made using the overload to the Async method with 'UserToken'.
// When the call completes we check the ID matches to avoid a nasty
// race condition
_searchRequestID = _searchRequestID++;
client.SearchOrdersCompleted += (s, e) =>
{
if (_searchRequestID != (int)e.UserState))
{
return; // avoid nasty race condition
}
// ok to handle response ...
}
client.SearchOrdersAsync(searchMessage, _searchRequestID);