none
How to unload app domain safely after response RRS feed

  • Question

  • I have a WCF service that has to load a big application and perform complicated operations before exiting.
    I can see that the process holds on to memory and this is a huge problem given the size of the application.
    I want to be able to release/recycle the service to keep the memory down. Only thing that seems to work is 

    HttpRuntime.UnloadAppDomain()

    Problem with that is that even if called in Dispose() of the service, I can't be sure that the call has completed it's response. At least it looks like that when debugging, and I get a "The server did not provide a meaningful reply" exception message.

    I also tried the recycle settings on the app pool but that got the same result.
    Is there some better way to do this? I'm running out of ideas.

    Wednesday, November 21, 2018 1:02 PM

All replies

  • Hi Robert Engzell,

    According to your description, I am not sure how you load an application in your WCF service, is it a DLL application, or an exe application, could you please share a bit more information about the issue, such as related code, the steps about how to reproduce the issue.

    In addition, here is a sample about how to unload a Running Dll for your reference.

    https://www.codeproject.com/Articles/18556/To-Unload-a-Running-Dll

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 23, 2018 5:14 AM
  • Hi, I was a bit unclear. I'm actually running our applications client code minus all the ui parts, in a WCF web service on IIS instead. It's actually regular c# code so nothing special, I just can't depend on it releasing memory after it's done, I'm afraid so I need to handle it myself.

    Wednesday, November 28, 2018 7:29 AM
  • Hi Robert Engzell,

    Please check the following thread, which suggest that make the object implement IDisposable and have it cancel the long running task/thread when it is called.

    If it's a plugin that you are sandboxing is there really a problem with killing the appdomain that you have used to host it? If it's not your code then obviously you can't control them misbehaving but you can provide a way for the plugin to be notified that it is being unloaded before nuking it's sandbox.

    https://stackoverflow.com/questions/25150989/when-does-the-garbage-collector-collect-instances-created-with-appdomain-createi

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 29, 2018 5:58 AM
  • I found a solution by testing a clean service and and a console app calling it.
    I set the Recycling->Request Limit  = 1 and Process Model->Shutdown Time Limit = 1200s

    Since the it's a PerCall service, it will create a new w3wp.exe for each call and recycle the old one giving it ampel time to finish processing. They are also overlapping which is great.

    I still faced an issue getting CommunicationException directly after the call (even though the service processed everything correctly) but when changing from nettcpbinding to wsHttpBinding, it worked. Unsure why.

    Thursday, November 29, 2018 2:50 PM
  • Hi Robert Engzell,
    Thanks for sharing your solution with me, nice idea. 
    >>I still faced an issue getting CommunicationException directly after the call (even though the service processed everything correctly) but when changing from NetTcpBinding to wsHttpBinding, it worked. Unsure why.
    There may be the reason that the wshttpbinding is Http protocol based and stateless, so there is no MaxConnections property. For Nettcpbinding based on TCP connections, the MaxConnections property exists and defaults to 10, try setting it to 1.
    Best Regards
    Abraham
    Wednesday, December 5, 2018 3:32 AM
    Moderator