Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added HttpMessageHandler but ports are still in TIME_WAIT #70855

Closed
datacruze opened this issue Jun 5, 2022 · 7 comments
Closed

Added HttpMessageHandler but ports are still in TIME_WAIT #70855

datacruze opened this issue Jun 5, 2022 · 7 comments

Comments

@datacruze
Copy link

datacruze commented Jun 5, 2022

Following this dotnet/wcf#3230 I've managed to inject my own HttpMessageHandler from IHttpMessageHandlerFactory into the WCF client.

My EndpointBehavior looks like this:

        public class CustomEndpointBehavior : IEndpointBehavior
        {
            private readonly Func<HttpMessageHandler> _httpHandler;

            public CustomEndpointBehavior(IHttpMessageHandlerFactory factory) => _httpHandler = () => factory.CreateHandler("ClientName");

            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) => bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(handler => _httpHandler()));

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

            public void Validate(ServiceEndpoint endpoint) { }
        }

In Startup.cs
services.AddHttpClient("ClientName");

And this is how I use the httpHandler:

            WcfResponseDto response;
            var binding = CreateHttpBinding();  //Basic HTTP Binding
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

            var factory = new ChannelFactory<MyWcfChannel>(binding, new EndpointAddress(url));
            IHttpMessageHandlerFactory httpMessageHandler = _serviceProvider.GetService(typeof(IHttpMessageHandlerFactory)) as 
            IHttpMessageHandlerFactory;
              factory.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior(httpMessageHandler));
            factory.Credentials.ClientCertificate.Certificate = GetCertificate();  //returns a x509 certificate
           var serviceProxy = factory.CreateChannel();

            try
            {

                ((ICommunicationObject)serviceProxy).Open();
                response = await
                    serviceProxy.EnterpriseGetCountyDetailsAsync(requestDto).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                ((ICommunicationObject)serviceProxy).Abort();
                factory.Abort();
                throw new FaultException(ex.Message);
            }
            finally
            {
                ((ICommunicationObject)serviceProxy).Close();
                factory.Close();
            }

            return response;
        }

When I use SOAP UI load test, I still see the ports open in TIME_WAIT after the application is done executing the load test. Is the HttpMessagehandler not working as it should or am I implementing it wrong? I am using the latest System.ServiceModel* dll (4.9.0)
172071553

@datacruze
Copy link
Author

datacruze commented Jun 5, 2022

Hi, @mconnew any insights please?

@datacruze datacruze changed the title Added HttpMessageHandler but ports are still in TIME_WAIT for load test Added HttpMessageHandler but ports are still in TIME_WAIT Jun 5, 2022
@HongGit HongGit transferred this issue from dotnet/wcf Jun 16, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jun 16, 2022
@HongGit
Copy link
Contributor

HongGit commented Jun 16, 2022

Transfer to runtime, this is a question of behavior of IHttpMessageHandlerFactory.

@ghost
Copy link

ghost commented Jun 16, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Following this dotnet/wcf#3230 I've managed to inject my own HttpMessageHandler from IHttpMessageHandlerFactory into the WCF client.

My EndpointBehavior looks like this:
`public class CustomEndpointBehavior : IEndpointBehavior
{
private readonly Func _httpHandler;

        public CustomEndpointBehavior(IHttpMessageHandlerFactory factory) => _httpHandler = () => factory.CreateHandler("ClientName");

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) => bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(handler => _httpHandler()));

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

        public void Validate(ServiceEndpoint endpoint) { }
    }`

In Startup.cs
services.AddHttpClient("ClientName");

And this is how I use the httpHandler:
`
WcfResponseDto response;
var binding = CreateHttpBinding(); //Basic HTTP Binding
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

        var factory = new ChannelFactory<MyWcfChannel>(binding, new EndpointAddress(url));
        IHttpMessageHandlerFactory httpMessageHandler = _serviceProvider.GetService(typeof(IHttpMessageHandlerFactory)) as 
        IHttpMessageHandlerFactory;
          factory.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior(httpMessageHandler));
        factory.Credentials.ClientCertificate.Certificate = GetCertificate();  //returns a x509 certificate
       var serviceProxy = factory.CreateChannel();

        try
        {

            ((ICommunicationObject)serviceProxy).Open();
            response = await
                serviceProxy.EnterpriseGetCountyDetailsAsync(requestDto).ConfigureAwait(false);
        }
        catch (Exception ex)
        {
            ((ICommunicationObject)serviceProxy).Abort();
            factory.Abort();
            throw new FaultException(ex.Message);
        }
        finally
        {
            ((ICommunicationObject)serviceProxy).Close();
            factory.Close();
        }

        return response;
    }

`

When I use SOAP UI load test, I still see the ports open in TIME_WAIT after the application is done executing the load test. Is the HttpMessagehandler not working as it should or am I implementing it wrong? I am using the latest System.ServiceModel* dll (4.9.0)
172071553

Author: datacruze
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@wfurt
Copy link
Member

wfurt commented Jun 16, 2022

@ghost
Copy link

ghost commented Jun 21, 2022

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Following this dotnet/wcf#3230 I've managed to inject my own HttpMessageHandler from IHttpMessageHandlerFactory into the WCF client.

My EndpointBehavior looks like this:

        public class CustomEndpointBehavior : IEndpointBehavior
        {
            private readonly Func<HttpMessageHandler> _httpHandler;

            public CustomEndpointBehavior(IHttpMessageHandlerFactory factory) => _httpHandler = () => factory.CreateHandler("ClientName");

            public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) => bindingParameters.Add(new Func<HttpClientHandler, HttpMessageHandler>(handler => _httpHandler()));

            public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime) { }

            public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }

            public void Validate(ServiceEndpoint endpoint) { }
        }

In Startup.cs
services.AddHttpClient("ClientName");

And this is how I use the httpHandler:

            WcfResponseDto response;
            var binding = CreateHttpBinding();  //Basic HTTP Binding
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate;

            var factory = new ChannelFactory<MyWcfChannel>(binding, new EndpointAddress(url));
            IHttpMessageHandlerFactory httpMessageHandler = _serviceProvider.GetService(typeof(IHttpMessageHandlerFactory)) as 
            IHttpMessageHandlerFactory;
              factory.Endpoint.EndpointBehaviors.Add(new CustomEndpointBehavior(httpMessageHandler));
            factory.Credentials.ClientCertificate.Certificate = GetCertificate();  //returns a x509 certificate
           var serviceProxy = factory.CreateChannel();

            try
            {

                ((ICommunicationObject)serviceProxy).Open();
                response = await
                    serviceProxy.EnterpriseGetCountyDetailsAsync(requestDto).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                ((ICommunicationObject)serviceProxy).Abort();
                factory.Abort();
                throw new FaultException(ex.Message);
            }
            finally
            {
                ((ICommunicationObject)serviceProxy).Close();
                factory.Close();
            }

            return response;
        }

When I use SOAP UI load test, I still see the ports open in TIME_WAIT after the application is done executing the load test. Is the HttpMessagehandler not working as it should or am I implementing it wrong? I am using the latest System.ServiceModel* dll (4.9.0)
172071553

Author: datacruze
Assignees: -
Labels:

untriaged, area-Extensions-HttpClientFactory

Milestone: -

@karelz
Copy link
Member

karelz commented Jun 21, 2022

Triage: This is by design behavior of the OS when we close Socket. As @wfurt mentions above, it will disappear.
If you are trying to limit the max number of connections, you can use MaxConnectionsPerServer to limit that.
Closing as resolved. Let us know if we misunderstood what you need.

@karelz karelz closed this as completed Jun 21, 2022
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jun 21, 2022
@karelz karelz added this to the 7.0.0 milestone Jul 19, 2022
@dotnet dotnet locked as resolved and limited conversation to collaborators Aug 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants