Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wcf get raw request from operation

Tags:

c#

soap

wcf

I've searched a lot for a good answer but got a lot of answers that didn't satisfy me.

We have a service with the usual operations. Inside each operation we call a different project to handle te operation and store some info of the operation inside a database.

We've got a new requirement: Store the soap message of the operation in the database too. Is it possible to receive the soap (xml) from the wcf operation? Or are event really needed?

like image 672
PcPulsar Avatar asked Oct 29 '14 09:10

PcPulsar


2 Answers

I was unable to use OperationContext.Current.RequestContext.RequestMessage.ToString() because it translates the request into Xml. When the request is JSON, I'd prefer it stay that way. I also found that, by the time an endpoint's method is invoked, Request.InputStream is empty---even when I have <add key="wcf:serviceHostingEnvironment:useClassicReadEntityBodyMode" value="true" /> in web.config (FYI: the app is running .Net 4.5.2).

So, what I did was to implement Application_BeginRequest in Global.asax.cs. I extract the contents of Request.InputStream there, and save that request body to Context.Items. Later, in IErrorHandler's ProvideFault(...), I transfer the saved string over to the Exception's Data property so that the logging will later capture it when HandleError(...) runs (Items is empty by the time HandleError runs).

like image 126
Granger Avatar answered Nov 04 '22 03:11

Granger


For myself i found this solution to get the raw soap message in framework 4.5:

OperationContext.Current.RequestContext.RequestMessage.ToString()

Read all comments above for more details about alternatives and their problems.

like image 44
PcPulsar Avatar answered Nov 04 '22 04:11

PcPulsar