Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
We have a console application using the Azure WebJob SDK. The WebJob relies on a WCF service using SOAP, which it accesses through a DLL we wrote that wraps the auto-generated WCF types in somethin...
#1: Initial revision
How can I return XML from BeforeSendRequest and AfterReceiveReply to the calling method in a thread-safe way?
We have a console application using the Azure WebJob SDK. The WebJob relies on a WCF service using SOAP, which it accesses through a DLL we wrote that wraps the auto-generated WCF types in something a bit more friendly. For logging purposes, we want to save the request and response XML bodies for requests that we make. These XML bodies would be saved in our database. But, because the WCF code lives in a low-level DLL, it has no concept of our database and can't save to it. The DLL uses Microsoft's DI extensions to register types, and the WebJob calls into it like this: ```csharp class WebJobClass { IWCFWrapperClient _wcfWrapperClient; public WebJobClass(IWCFWrapperClient wcfWrapperClient) { _wcfWrapperClient = wcfWrapperClient; } public async Task DoThing() { var callResult = await _wcfWrapperClient.CallWCFService(); } } ``` `IWCFWrapperClient` looks like this: ```csharp class WCFWrapperClient : IWCFWrapperClient { IWCF _wcf; // auto-generated by VS, stored in Reference.cs public async Task<object> CallWCFService() { return await _wcf.Call(); // another auto-generated method } } ``` I've implemented an `IClientMessageInspector`, and it works fine to get me the XML request/response, but I don't have a way to pass it back up to `WCFWrapperClient.CallWCFService` so that it can be returned to `WebJobClass.DoThing()`, who could then save it to the database. The problem is multithreading. WebJobs, IIRC, will run multiple requests in parallel, calling into the DLL from multiple threads. This means we can't, say, share a static property `LastRequestXmlBody` since multiple threads could overwrite it. We also can't, say, give each call a Guid or something since there's no way to pass anything from `IWCFWrapperClient.CallWCFService` into the auto-generated `IWCF.Call` except what was auto-generated. So, how can I return XML to `WebJobClass.DoThing` in a thread-safe way?