1

I'm using an IPropertyInspector to validate a supplied token and fetch the corresponding user profile (I throw a Fault Exception if the token is not found or is not valid). I then store this user profile in the OperationContext as per this suggested implementation:(Where to store data for current WCF call? Is ThreadStatic safe?)

In my service implementation class, I'd like the user profile to be a field I can easily access, so my first idea was to populate this field in the constructor, but unfortunately the service class is instantiated before the IPropertyInspector fires. So, as an alternative, is there some event I can subscribe to in the WCF pipeline which happens after the IPropertyInspector has run, but before the operation on my service has been called, in which I can populate this user profile field from the Operation Context?

If not, I'll use RemoteRequestContext.Current.Items["User"] as UserProfile every time I want it, but a field would just be cleaner and more convenient.

Community
  • 1
  • 1
Whatever Man
  • 506
  • 7
  • 21

2 Answers2

1

You can implement the Attribute, IServiceBehavior and IDispatchMessageInspector Interface and in ApplyDispatchBehavior() method you can call your method. It will execute every time before executing any Operation method. You just read on internet about Attribute, IServiceBehavior and IDispatchMessageInspector. If you wont get anything comment it here, I will give you the some source code.

Nitish Mishra
  • 77
  • 2
  • 9
  • Thank you for your suggestion. Given that I am already using ApplyDispatchBehavior() to add my IParameterInspector, it was quick to add a message inspector as well. From there I can get the service instance from instanceContext.GetServiceInstance()...however...the Message Inspector fires before the Parameter Inspector, so once again the User Profile has not yet been populated when I need it. – Whatever Man Aug 14 '14 at 13:39
0

I got the suggestion I needed from here: How do I get access to the WCF service instance in the current context?

From within the Parameter Inspector, I am able to access the operation context, from which I can get my service instance. So, instead of pulling the information in the service by subscribing to an event, I can simply push it to the service inside the Parameter Inspector like so:

  var service = OperationContext.Current.InstanceContext.GetServiceInstance() as IRemoteServiceBase;
  if (service != null)
        service.UserProfile = userFromToken;
Community
  • 1
  • 1
Whatever Man
  • 506
  • 7
  • 21