How to properly close a client proxy (An existing connection was forcibly closed by the remote host)?

后端 未结 3 1633
一向
一向 2020-12-15 09:08

Please don\'t close as duplicate until you read the question to the end; I already googled for hours without success.


EDIT: Now I\'m convinced it\'s related to

3条回答
  •  暖寄归人
    2020-12-15 10:00

    To resolve this error, simply close the Channel Factory as well.

    private async Task TestTask()
    {
        Service1Client proxy = null;
    
        try
        {
            Console.WriteLine("Calling service");
    
            proxy = new Service1Client();
            return await proxy.DoWorkAsync();
        }
        finally
        {
            if (proxy.State != System.ServiceModel.CommunicationState.Faulted)
            {
                Console.WriteLine("Closing client");
                proxy.ChannelFactory.Close();
                proxy.Close();
            }
            else
            {
                Console.WriteLine("Aborting client");
                proxy.Abort();
            }
        }
    }
    

提交回复
热议问题