Ejemplo n.º 1
0
 void ic_Faulted(object sender, EventArgs e)
 {
     ic.Abort();
     ic = new InstanceContext(ServerCallback);
     ic.Open();
     listBox1.Items.Add("Channel Fault repaired");
 }
        public object AfterReceiveRequest(
            ref Message request,
            IClientChannel channel,
            InstanceContext instanceContext)
        {
            HttpRequestMessageProperty requestProperty = null;

            if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
            {
                requestProperty = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            }

            if (requestProperty != null)
            {
                var origin = requestProperty.Headers["Origin"];
                if (!string.IsNullOrEmpty(origin))
                {
                    // if a cors options request (preflight) is detected,
                    // we create our own reply message and don't invoke any
                    // operation at all.
                    if (requestProperty.Method.ToUpperInvariant() == "OPTIONS")
                    {
                        instanceContext.Abort();
                    }
                }
            }

            return(requestProperty);
        }
        public void Add(InstanceContext instanceContext)
        {
            bool flag = false;

            lock (base.ThisLock)
            {
                if (base.State == LifetimeState.Opened)
                {
                    if (instanceContext.InstanceContextManagerIndex != 0)
                    {
                        return;
                    }
                    if (this.firstFreeIndex == 0)
                    {
                        this.GrowItems();
                    }
                    this.AddItem(instanceContext);
                    base.IncrementBusyCountWithoutLock();
                    flag = true;
                }
            }
            if (!flag)
            {
                instanceContext.Abort();
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(base.GetType().ToString()));
            }
        }
Ejemplo n.º 4
0
 public void OffLine()
 {
     client.Unsubscribe();
     //client.OffLine("黄埔");
     instanceContext.Abort();
     instanceContext.Close();
     client.Abort();
     client.Close();
 }
Ejemplo n.º 5
0
        private async Task CloseInitiateAsync(CancellationToken token)
        {
            InstanceContext[] instances = ToArray();
            for (int index = 0; index < instances.Length; index++)
            {
                InstanceContext instance = instances[index];
                try
                {
                    if (instance.State == CommunicationState.Opened)
                    {
                        Task result = instance.CloseAsync(token);
                        if (!result.IsCompleted)
                        {
                            ContinueCloseInstanceContext(result);
                            continue;
                        }

                        await result;
                    }
                    else
                    {
                        instance.Abort();
                    }
                }
                catch (ObjectDisposedException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (InvalidOperationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (CommunicationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (TimeoutException e)
                {
                    //if (TD.CloseTimeoutIsEnabled())
                    //{
                    //    TD.CloseTimeout(e.Message);
                    //}
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
            }
        }
Ejemplo n.º 6
0
        void CloseInitiate(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            InstanceContext[] instances = this.ToArray();
            for (int index = 0; index < instances.Length; index++)
            {
                InstanceContext instance = instances[index];
                try
                {
                    if (instance.State == CommunicationState.Opened)
                    {
                        IAsyncResult result = instance.BeginClose(timeoutHelper.RemainingTime(), Fx.ThunkCallback(new AsyncCallback(CloseInstanceContextCallback)), instance);
                        if (!result.CompletedSynchronously)
                        {
                            continue;
                        }
                        instance.EndClose(result);
                    }
                    else
                    {
                        instance.Abort();
                    }
                }
                catch (ObjectDisposedException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (InvalidOperationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (CommunicationException e)
                {
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
                catch (TimeoutException e)
                {
                    if (TD.CloseTimeoutIsEnabled())
                    {
                        TD.CloseTimeout(e.Message);
                    }
                    DiagnosticUtility.TraceHandledException(e, TraceEventType.Information);
                }
            }
        }
Ejemplo n.º 7
0
        internal void AbortInstanceContext()
        {
            if (InstanceContext != null && !isInstanceContextSingleton)
            {
                try
                {
                    InstanceContext.Abort();
                }
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    channelHandler.HandleError(e);
                }
            }
        }
    public void Initialize(InstanceContext instanceContext, Message message)
    {
        RemoteEndpointMessageProperty ep = message.Properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

        // which action do we want to throttle
        if (message.Headers.Action.EndsWith("register") &&
            ep != null &&
            ep.Address != null)
        {
            // get the IP address
            var item = cache[ep.Address];
            if (item == null)
            {
                // not found, so init
                cache.Add(
                    ep.Address,
                    new Counter {
                    Count = 0
                },
                    new CacheItemPolicy
                {
                    SlidingExpiration = new TimeSpan(0, 1, 0)         // 1 minute
                });
            }
            else
            {
                // how many calls?
                var count = (Counter)item;
                if (count.Count > 5)
                {
                    instanceContext.Abort();
                    // not sure if this the best way to break
                    throw new Exception("throttle");
                }
                // add one call
                count.Count++;
            }
        }
    }
Ejemplo n.º 9
0
 public void Logout()
 {
     Console.WriteLine("Logout");
     _channelContext.Abort();
 }