3

I have a WCF service that use Exceptions to returns wrong results. for example when a argument of method is wrong an exception raised and send a exception to a client and client catch that exception and show its message. I use FaultException too. but the problem is the destructor of service class doesn't called when an exception raised. I must dispose the repository in the destructor when exceptions occured. how can I do it?

public class TestService : ITestService
{
    private AccountReposiory _accountReposiory;

    public TestService()
    {
        Console.WriteLine("Started!");

        _accountReposiory = new AccountReposiory();
    }

    public void DoWork()
    {
        //Exception Occurred!;

        throw new Exception("Some Exception");

        //...
    }

    ~TestService()
    {
        Console.WriteLine("Finished!");

        _accountReposiory.Dipose();
    }
}

2 Answers 2

0

Do not use destructor , because .net C# is managed language

GC class automatically call when then any class initialized,

use ....,

try{

   Console.WriteLine("Exception Occurred!");

}catch(Execption ex){

     return;

} 


try{

   Console.WriteLine("Exception Occurred!");

}catch{



} 

block to solve this problem. don't use destructor. I think it may help you

0

Doing cleanups is always a good thing, except when not needed, and if it adds your code a lot of overhead.

In your case, I suppose you want to make sure the class is destroyed after use because of used non managed systems resources. In this case maybe the answer in this question will help you:

If you are not using non managed system resources, then you can leave the clean up to the garbage collector.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.