1

I am trying to pass user information from controller to service layer.

IClientMessageInspectors and IDispatchMessageInspectors seem to be the most likely solution.

After going through Building an HTTP User Agent Message Inspector and Writing a WCF Message Inspector I have got a fair idea how to add information to the message header and how to read it.

What I cant figure out is this:

How do I get User information in the IClientMessageInspector?

Inside the controller I used to sue User.Identity.Name. But the User object is not available in the message inspector. One way to have this information in the message inspector would be to pass it in the constructor, but I never explicitly create the MessageInspector object so how can I pass the user information in its constructor.

Edit: I found the answer to my first question. The user information can be fetched by System.Threading.Thread.CurrentPrincipal.Identity.Name. Still stuck with the second part.

How to have the user information available inside a service method?

I can go through the headers and fetch the user information from the message header in the DispatchMessageInspector, but how can I have this information available in one of my operation contract?

Arnab Chakraborty
  • 7,442
  • 9
  • 46
  • 69

1 Answers1

0

Create class with properties related to User information. Overide object BeforeSendRequest(ref Message request, IClientChannel channel); method set value in header using this method --MessageHeader _messageHeader = MessageHeader.CreateHeader(); and put object of use information class.

// Summary:
//     Creates a new message header with the specified data.
//
// Parameters:
//   name:
//     The local name of the header XML element.
//
//   ns:
//     The namespace URI of the header XML element.
//
//   value:
//     The content of the header to be created.
//
// Returns:
//     A System.ServiceModel.Channels.MessageHeader.
public static MessageHeader CreateHeader(string name, string ns, object value); 

At service side get it from -- MessageHeaders class method--

// Summary:
//     Finds a message header in this collection by the specified LocalName and
//     namespace URI of the header element.
//
// Parameters:
//   name:
//     The LocalName of the header XML element.
//
//   ns:
//     The namespace URI of the header XML element.
//
// Type parameters:
//   T:
//     The message header.
//
// Returns:
//     A message header.
public T GetHeader<T>(string name, string ns);
Ethan Cabiac
  • 4,943
  • 20
  • 36
Vivek
  • 570
  • 8
  • 21
  • I know how to add information to header, and know how to read it in a dispatch message inspector too. What I can't figure out is how do I pass this information to one of my data contracts. – Arnab Chakraborty Jun 27 '12 at 04:35
  • You have to set extension at service behavior only, need not explicitly mention any thing with your service contract or data contract. With Message contract as parameter of your operation, information is implicitly added in soap header. – Vivek Jun 27 '12 at 06:03
  • You can also go through this link.. http://social.msdn.microsoft.com/Forums/en/wcf/thread/3f8575d6-0d29-41e4-ad3b-872b996801fd – Vivek Jun 27 '12 at 06:10