3

Is there any way to extract http header value and pass it to WCF rest service operation at global extension point, e.g. customized IParameterInspector? Thanks in advance.

1 Answer 1

3

Through your WCF service WebOperationContext you can access the current request's http headers like the following:

var request = WebOperationContext.Current.IncomingRequest;
string header = request.Headers[HttpRequestHeader.Cookie];
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. But I'm trying to find a way extractting the header value and then binding it to be as parameters of service operation, Is it possible?
I'm sorry but I'm not following you... What exactly do you mean by "extract the header value and bind it as a service operation parameter"? Could you elaborate more about your use cases? Maybe you are trying to set a header value... for that you can use the WebHeaderCollection.Set Method
Hi Thomas, sorry about the question was not clear. In my use case, each wcf rest service operation has a parameter accountName, meanwhile,I use HMAC protocol to protect my rest api,the accountName already exist in the http header item Authentication. So I try to find a way passing the accountName from http header to operation parameter better than OperationContext.Current.IncomingMessageProperties.Add("accountName", accountName), so I can omit the rest url segment of accountName. Is there any suggestion? Thank you very much.
Now it is clear to me that what you want is to override a parameter in a WCF method call with a value extracted from the request header. I'm sorry but I don't know a proper way of doing this, but from my point of view this seams like a code smell, because you maybe would be passing a null value in your method call and magically the receiver would get another value... I suggest you simply define a static helper method in your WCF service class to perform this task, you could name it MyWcfClass.GetAccountNameFromHeader(). Then, everytime you need this value you just call this helper method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.