Do I need to close the connection to a WCF service from a console that exits straight away?

676 views Asked by At

I have a console app that I want to do a "fire-and-forget" call to a WCF service, and then close down without waiting for a response. It is just supposed to initiate a cleanup job. The job can take several hours to finish, so I don't want the console app to stay open and wait for it.

I have added "IsOneWay=true" to the methods in the contract, but the console app still waits for the task to finish before doing client.close() and exiting.

If I remove the client.Close() then the console app works the way I want, but I am not sure if the channel will remain open even though the console app is not running anymore?

Here is my console app code:

static void Main(string[] args)
{
    Console.WriteLine("Starting Cleanup");

    var client = new IntegrationWcfServiceClient(EndPointConfigurationName);

    try
    {
        client.ExecuteCleanup();
        //client.Close();
    }
    catch (Exception ex)
    {
        client.Abort();
        WriteLineRed($"Couldn't start cleanup: {ex.Message}");
        return;
    }

    WriteLineGreen("Cleanup started successfully");
}

And here is the operation contract code:

[OperationContract(IsOneWay = true)]
void ExecuteCleanup();
1

There are 1 answers

0
Hameed Syed On

There are few things you need to consider while making oneway call. From the book programming WCF services.

  1. Ideally, when the client calls a one-way method, it should be blocked only for the briefest moment required to dispatch the call. However, in reality, one-way calls do not equate to asynchronous calls. When one-way calls reach the service, they may not be dispatched all at once but may instead be buffered on the service side to be dispatched one at a time, according to the service’s configured concurrency mode behavior
  2. Although one-way operations do not return values or exceptions from the service itself, it’s wrong to perceive them as a one-way street or a “black hole” from which nothing can come out. The client should still expect exceptions from a one-way call, and can even deduce that the call failed on the service. When dispatching a one-way operation, any error because of communication problems (such as a wrong address or the host being unavailable) will throw an exception on the side of the client trying to invoke the operation.

If I remove the client.Close() then the console app works the way I want, but I am not sure if the channel will remain open even though the console app is not running anymore?

A one-way call is not fire-and-forget in nature, since the client can discover that something went wrong on the service during a one-way invocation.

Here you are tring to invoke invoke a one-way operation asynchronously and hence you are not able to close the connection or proxy.

[OperationContract(IsOneWay = true,AsyncPattern = true)]
IAsyncResult ExecuteCleanup(AsyncCallback callback,object asyncState);

client.ExecuteCleanup(,null,null);

Note:If you dont want to complicate things ,then make sure ExecuteCleanup is the last call in your service and later you can close which will not affect later operations. Possible implementation How to properly close a client proxy (An existing connection was forcibly closed by the remote host)?