WebOperationContext.Current in WCF service null

1.9k Views Asked by At

I have a WCF service with the service file as - Serivce.svc Here I can read incoming headers using the WebOperationContext.Current

The code from the Service file access a data access utility layer which makes other calls; I need to to do some work in the data access layer based on the header passed in.

However, the WebOperationContext.Current is null here.

How do I get around this?

1

There are 1 best solutions below

0
On

From your question, it seems your "data access utility layer" is dependent on information that was passed to the service through the headers. Make this explicit, preferably through an interface so it's easily testable. Something like this:

public class DataAccessLayer(IMetaInfoFromHeaders requiredInfo)
{ /* implementation */ }

(Alternatively you could just have the IMetaInfoFromHeaders be an argument for just one or a few methods in the DAL, if that seems better - this depends on the specifics.)

Your service is responsible for processing the message. It should extract the information from the headers, and pass it to the DAL using an object implementing IMetaInfoFromHeaders.

Bottom line: don't make the DAL dependent on the WebOperationContext.