Answered by:
Get OperationContract Name In IDispatchMessageInspector

Question
-
Hi,
Can any one tell me how to get Operation Contract Name in IDispatchMessageInspector??
I used to get the WebMethod name in SoapExtensions in legacy ASMX WebService as below
message.MethodInfo.Name where message is a SoapMessage.
Since IDispatchMessageInspector is a replacement of SoapExtensions in WCF, can anyone suggest the appropriate code to get the Operation Contract name in IDispatchMessageInspector ??
Any suggestions here are welcome.
Thanks In Advance.
Saturday, May 3, 2008 11:32 PM
Answers
-
That's what I did in the past: (There might be a cleaner way)
OperationContext ctx = OperationContext.Current;
ServiceDescription hostDesc = ctx.Host.Description;
ServiceEndpoint endpoint = hostDesc.Endpoints.Find(ctx.IncomingMessageHeaders.To);
string operationName = ctx.IncomingMessageHeaders.Action.Replace(
endpoint.Contract.Namespace + endpoint.Contract.Name + "/", "");
OperationDescription operation =
endpoint.Contract.Operations.Find(operationName);
And yes, it uses string manipulation but at least it relies on the proper endpoint and service contract namespace and name.Sunday, May 4, 2008 1:48 PM
All replies
-
Well, the message that you're inspecting has the action in its headers:
message.Headers.Action
Or if you don't want to use the message, you can access the message headers from the OperationContext:
OperationContext.Current.IncomingMessageHeaders.Action
Sunday, May 4, 2008 7:23 AM -
Hi Sasha,
Thanks for that information using the code message.Headers.Action i am getting the full URI of the action like if my method name is testmethod i am getting http://sunsmile.com/testmethod
Is there anyway i can get only the method name (testmethod) using any of the properties of message or should i do some kind of String manipulation to extract method name from URI?
Getting the method name directly instead of doing string manipulation would be great.
Any comments , suggestions are welcome here.
Thanks In Advance.
Sunday, May 4, 2008 1:39 PM -
That's what I did in the past: (There might be a cleaner way)
OperationContext ctx = OperationContext.Current;
ServiceDescription hostDesc = ctx.Host.Description;
ServiceEndpoint endpoint = hostDesc.Endpoints.Find(ctx.IncomingMessageHeaders.To);
string operationName = ctx.IncomingMessageHeaders.Action.Replace(
endpoint.Contract.Namespace + endpoint.Contract.Name + "/", "");
OperationDescription operation =
endpoint.Contract.Operations.Find(operationName);
And yes, it uses string manipulation but at least it relies on the proper endpoint and service contract namespace and name.Sunday, May 4, 2008 1:48 PM -
Hi Sasha,
Thanks for that code. The below code works for me according to my namespaces.
OperationContext ctx = OperationContext.Current;
ServiceDescription hostDesc = ctx.Host.Description;
ServiceEndpoint endpoint = hostDesc.Endpoints.Find(ctx.IncomingMessageHeaders.To);
string operationName = request.Headers.Action.Replace(endpoint.Contract.Namespace + "/", "").ToLower();
where operationName comes out as testmethod. Request = Incoming message.
One quick question here in soap extensions i used to get the incoming parameter of webmethod as below
string str = message.GetInParameterValue(0)
Can you please suggest equivalent code in IDispatchMessageInspector.
Any comments here are welcome.
Thanks In Advance.
Sunday, May 4, 2008 2:35 PM -
You will need a parameter inspector to do that. In your parameter inspector, you will gain access to the inputs and outputs of the operation.
http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iparameterinspector.aspxSunday, May 4, 2008 2:48 PM -
How about this?
Path.GetFileName(Buffer.CreateMessage().Headers.Action)
Tuesday, October 27, 2009 3:08 AM -
and this for the service
Path.GetFileNameWithoutExtension(Buffer.CreateMessage().Headers.To.AbsolutePath)
cheesy but it worksTuesday, October 27, 2009 3:14 AM