当前位置: 首页>>代码示例>>C#>>正文


C# TimeoutHelper.ElapsedTime方法代码示例

本文整理汇总了C#中System.Runtime.TimeoutHelper.ElapsedTime方法的典型用法代码示例。如果您正苦于以下问题:C# TimeoutHelper.ElapsedTime方法的具体用法?C# TimeoutHelper.ElapsedTime怎么用?C# TimeoutHelper.ElapsedTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.TimeoutHelper的用法示例。


在下文中一共展示了TimeoutHelper.ElapsedTime方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Connect

 public IConnection Connect(Uri uri, TimeSpan timeout)
 {
     if (DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x4002b, System.ServiceModel.SR.GetString("TraceCodeInitiatingTcpConnection"), new StringTraceRecord("Uri", uri.ToString()), this, null);
     }
     int port = uri.Port;
     IPAddress[] iPAddresses = GetIPAddresses(uri);
     Socket socket = null;
     SocketException innerException = null;
     if (port == -1)
     {
         port = 0x328;
     }
     int invalidAddressCount = 0;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     for (int i = 0; i < iPAddresses.Length; i++)
     {
         if (helper.RemainingTime() == TimeSpan.Zero)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateTimeoutException(uri, helper.OriginalTimeout, iPAddresses, invalidAddressCount, innerException));
         }
         AddressFamily addressFamily = iPAddresses[i].AddressFamily;
         if ((addressFamily == AddressFamily.InterNetworkV6) && !Socket.OSSupportsIPv6)
         {
             iPAddresses[i] = null;
         }
         else
         {
             DateTime utcNow = DateTime.UtcNow;
             try
             {
                 socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
                 socket.Connect(new IPEndPoint(iPAddresses[i], port));
                 innerException = null;
                 break;
             }
             catch (SocketException exception2)
             {
                 invalidAddressCount++;
                 TraceConnectFailure(socket, exception2, uri, (TimeSpan) (DateTime.UtcNow - utcNow));
                 innerException = exception2;
                 socket.Close();
             }
         }
     }
     if (socket == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("NoIPEndpointsFoundForHost", new object[] { uri.Host })));
     }
     if (innerException != null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertConnectException(innerException, uri, helper.ElapsedTime(), innerException));
     }
     return this.CreateConnection(socket);
 }
开发者ID:pritesh-mandowara-sp,项目名称:DecompliedDotNetLibraries,代码行数:56,代码来源:SocketConnectionInitiator.cs

示例2: ConnectAsync

        public async Task<IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
        {
            int port = uri.Port;
            IPAddress[] addresses = await SocketConnectionInitiator.GetIPAddressesAsync(uri);
            IConnection socketConnection = null;
            SocketException lastException = null;

            if (port == -1)
            {
                port = TcpUri.DefaultPort;
            }

            int invalidAddressCount = 0;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            for (int i = 0; i < addresses.Length; i++)
            {
                if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
                }

                DateTime connectStartTime = DateTime.UtcNow;
                try
                {
                    socketConnection = await CreateConnectionAsync(addresses[i], port);
                    lastException = null;
                    break;
                }
                catch (SocketException socketException)
                {
                    invalidAddressCount++;
                    lastException = socketException;
                }
            }

            if (socketConnection == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
            }

            if (lastException != null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    SocketConnectionInitiator.ConvertConnectException(lastException, uri,
                    timeoutHelper.ElapsedTime(), lastException));
            }

            return socketConnection;
        }
开发者ID:SoumikMukherjeeDOTNET,项目名称:wcf,代码行数:51,代码来源:SocketConnection.cs

示例3: Connect

        public IConnection Connect(Uri uri, TimeSpan timeout)
        {
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.InitiatingTcpConnection,
                    SR.GetString(SR.TraceCodeInitiatingTcpConnection),
                    new StringTraceRecord("Uri", uri.ToString()), this, null);
            }

            int port = uri.Port;
            IPAddress[] addresses = SocketConnectionInitiator.GetIPAddresses(uri);
            Socket socket = null;
            SocketException lastException = null;

            if (port == -1)
            {
                port = TcpUri.DefaultPort;
            }

            int invalidAddressCount = 0;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            for (int i = 0; i < addresses.Length; i++)
            {
                if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
                }

                AddressFamily addressFamily = addresses[i].AddressFamily;

                if (addressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)
                {
                    addresses[i] = null; // disregard for exception attempt purposes
                    continue;
                }

                DateTime connectStartTime = DateTime.UtcNow;
                try
                {
                    socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(new IPEndPoint(addresses[i], port));
                    lastException = null;
                    break;
                }
                catch (SocketException socketException)
                {
                    invalidAddressCount++;
                    SocketConnectionInitiator.TraceConnectFailure(socket, socketException, uri, DateTime.UtcNow - connectStartTime);
                    lastException = socketException;
                    socket.Close();
                }
            }

            if (socket == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new EndpointNotFoundException(SR.GetString(SR.NoIPEndpointsFoundForHost, uri.Host)));
            }

            if (lastException != null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    SocketConnectionInitiator.ConvertConnectException(lastException, uri,
                    timeoutHelper.ElapsedTime(), lastException));
            }

            return CreateConnection(socket);
        }
开发者ID:krytht,项目名称:DotNetReferenceSource,代码行数:69,代码来源:SocketConnection.cs


注:本文中的System.Runtime.TimeoutHelper.ElapsedTime方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。