Llamada al método delegado que tiene parámetros

I have a number of WCF calls which I would like to have a try catch around them. Instead of copying the same block of try catch, I would like to delegate the function call.

Here is my sample original function (cut down);

public DTO_Echo_Response SendEcho(DTO_Echo_Request request)
{
    try
    {
        return Proxy.SendEcho(request);
    }
    catch (System.ServiceModel.CommunicationException)
    {
        throw new Communication_Error("Communication Error");
    }
}

Me gustaría algo como lo siguiente:

public DTO_Echo_Response SendEcho(DTO_Echo_Request request)
{
    // invoke Process(Proxy.SendEcho(request));
}

public _DTO_BaseResponse Process(Func myFunction)
{
    try
    {
        return myFunction();
    }
    catch (System.ServiceModel.CommunicationException)
    {
        throw new Communication_Error("Communication Error");
    }
}

I have visited many articles and tried many different things which were way off the mark.

gracias

preguntado el 01 de febrero de 12 a las 04:02

1 Respuestas

You're pretty close. Try this:

public DTO_Echo_Response SendEcho(DTO_Echo_Request request)
{
    return Process(() => Proxy.SendEcho(request));
}

public TResult Process<TResult>(Func<TResult> myFunction)
{
    try
    {
        return myFunction();
    }
    catch (System.ServiceModel.CommunicationException)
    {
        throw new Communication_Error("Communication Error");
    }
}

Respondido 01 Feb 12, 09:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.