Вопрос

I use WCF to async communicate between 2 processes.
till now I implemented the IAsyncResult pattern, and did it by having 3 methods:
BeginOperation - client.BeginOperation, when service receive it queue the job on threadpool with delegate for Operation
Operation - run on service side
EndOperation - this is what client callback.

My question is, I want to send strings from client to service, I want the send to be async, AND I dont want to get response - just want the service to print the string.
Is this enough ? This must be Non-Blocking

    [OperationContract(IsOneWay = true)]
    void PrintString(string message);

OR i need to do as following:

    [OperationContract(IsOneWay = true, AsyncPattern=true)]
    void BeginPrintString(string message, AsyncCallback callback, object state);
    void EndPrintString(IAsyncResult asyncResult);
Это было полезно?

Решение

IsOneWay should be enough, but it still can block your client in specific circumstances, as well as throw errors.

See this and this posts for more details:

General things you should keep in mind about OneWay operations -

O/W operations must return void. O/W operations can still yield exceptions. invoking an operation on the client channel might still throw an exception if it couldn’t transmit the call over to the service. O/W operations can still block. if the service is pumped with messages and a queue had started, calling O/W operation may block your following code.

Другие советы

You can make it more asynchronous by using a separate thread to do your work.

Instead of:

public void MyOneWayServiceOperation() {
   // do stuff
}

do something like:

public void MyOneWayServiceOperation() {
   new Thread(() => {
      // do stuff
   }).Start();
}

The service operation will have been executed on a new thread anyway, so it shouldn't cause any more problems for your code to be on a different new thread.

You can add a Thread.Sleep() call before your work if you want the service operation to have completed before you begin. This allowed me to quit the service from a service operation without having an exception thrown.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top