none
Use of Delegates through WCF RRS feed

  • Question

  • Hi everyone,

    I have a doubt about the use of delegates in WCF, I'm a beginner in WCF, as I am in Delegates.

    The scenario is as follows:

    A service application and a dll as a client being used in a Console App (in order to test the dll), this, because the dll client is going to be used later in a final software which allows the use of .net dlls. All of this is going to be executed locally (the service, and the software which uses the dll client) by using netNamedPipes.

    Well, In the service side I have a method which takes a long time (more than 1 sec.) to be executed and when using the dll in the afore mentioned software, the execution of the software using the dll client crashes, so I found that a possible solution for this situation is the use of Asynchronous Execution (IAsyncResult BeginProcess / EndProcess) in the service side of the long time execution process. It was all fine until I was asked to change all the processes in the service to be executed asynchronously, for that reason I decided to create a class (DataInput) with a constructor that takes optional named parameters which allows me to use it in every service method and that way (as far I understand) in the callback function with the use of a delegate I could just call the "passed method" via that delegate with the object instantiated with the needed arguments.

    I hope I have explained myself clearly.

    Thanks in advance.

    Wednesday, November 25, 2015 2:57 PM

Answers

All replies

  • Thank you Stefan,

    The first approach is the one I used successfuly in my case.

    Additionally I want to make a "generic" implementation (I don't know if it's possible) of the asynchronous model for all the service operations, with a delegate instantiated in the client and passed to the service, so that the service can identify which operation is going to be executed.

    Regards.

    Wednesday, November 25, 2015 3:50 PM
  • I can tell I have tested with the use of a delegate instantiated in the client and passed to the service, but then I don't know if WCF allows it or, there is something I'm missing, I get always an error about serializing the delegate. Something like: "There was an error while trying to serialize".
    Wednesday, November 25, 2015 3:58 PM
  • I don't understand what you mean with "generic". Where do you want generic code?

    On the service side you need to implement each of the necessary methods. On the client side the code is auto-created, when you add you service reference.. Can you give me an concrete description what you want to generalize?

    Wednesday, November 25, 2015 4:32 PM
  • I can tell I have tested with the use of a delegate instantiated in the client and passed to the service, but then I don't know if WCF allows it or, there is something I'm missing, I get always an error about serializing the delegate. Something like: "There was an error while trying to serialize".
    https://www.safaribooksonline.com/library/view/programming-wcf-services/0596526997/ch03s07.html
    Wednesday, November 25, 2015 4:40 PM
  • Ok Stefan, by "generic" I mean a callback function in the service which takes the IAsyncResult parameter instantiated in a BeginProcess (from the pair BeginProcess / EndProcess) and invokes the method passed via a delegate to the service. For example in the next code of my BeginProcess method, I pretend to receive via the delegate the pointer to the function that is needed to be executed:

    paramData is the object which has all the parameters that are needed in the operations (methods) of my service.

    selectedOperation is the Delegate in which from the client I specify the service operation I want the service to asynchronously execute.


    public IAsyncResult BeginProcess(DataInput paramData, FuncToExec selectedOperation, AsyncCallback callback, object state)
            {
                AsyncResult asyncResult = new AsyncResult(callback, state, this, "Asynch_BeginProcess", paramData, selectedOperation);
                ThreadPool.QueueUserWorkItem(new WaitCallback(Callback), asyncResult);
                return asyncResult;
            }

    private void Callback(object state)
    {
        AsyncResult<DataInput> asyncResult = state as AsyncResult<DataInput>;
        try
        {
            asyncResult.funcParam.result = asyncResult.funcParam.Operation        (asyncResult.funcParam);

        }

    }

    Again, I don't know if it's possible or at least there's another approach to do what I mean.

    Regards.

    Wednesday, November 25, 2015 6:02 PM
  • I'll check that info, thank you DA924!
    Wednesday, November 25, 2015 6:15 PM
  • You have normally one Begin/End method pair per method.

    See How to: Call WCF Service Operations Asynchronously.

    You call the methods explicitly in the client.

    Wednesday, November 25, 2015 8:11 PM