If I understood correctly, you want to use the identifier as correlationState to associate the request with the response. To do this, return your identifier as an object from BeforeSendRequest , and it will be passed as an AfterReceiveReply argument.
public object BeforeSendRequest(ref Message request, IClientChannel channel) { var correlationState = ...
UPDATE
The problem is how to pass the parameter to the BeforeSendRequest method.
In this case, if you do not have all the necessary information in your request, the obvious solution is ThreadStatic to transfer information from your caller to BeforeSendRequest .
Set the field before calling the service, then BeforeSendRequest and use the value in BeforeSendRequest .
UPDATE 2
Another possibility I can use is to set the parameter to the message itself, but how can I access it in the request parameter in BeforeSendRequest?
I do not understand this question. If I understand correctly, you want to pass the parameter from the calling BeforeSendRequest method. You know how to pass it from BeforeSendRequest to AfterReceiveReply using the AfterReceiveReply correlation.
The way to do this is to use the ThreadStatic field. For example, you can create the following class:
public static class CallerContext { [ThreadStatic] private static object _state; public static object State { get { return _state; } set { _state = value; } } }
Then your caller will set CallerContext.State before calling the web service and will be available to both BeforeSendRequest and AfterReceiveReply . For example.
... try { CallerContext.State = myId; ... call web service here ... } finally { CallerContext.State = null; }
and this will be available in BeforeSendRequest :
public object BeforeSendRequest(ref Message request, IClientChannel channel) { var correlationState = CallerContext.State; ... do your stuff here return correlationState; }
Note that using try / finally before CallerContext.State to null is not strictly necessary, but prevents access to unrelated code by the available, possibly confidential, data that you store there.