Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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

66%
+2 −0
Q&A 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 somethin...

1 answer  ·  posted 11mo ago by Celarix‭  ·  edited 11mo ago by Alexei‭

#3: Nominated for promotion by user avatar Alexei‭ · 2023-06-25T09:46:58Z (11 months ago)
#2: Post edited by user avatar Alexei‭ · 2023-06-22T16:35:15Z (11 months ago)
removed too specific tag
#1: Initial revision by user avatar Celarix‭ · 2023-06-13T23:55:30Z (11 months ago)
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?