Пример #1
0
 private void CheckAdminRights(IRequestBase request)
 {
     if (!request.Context.IsAdmin)
     {
         throw new InvalidOperationException();
     }
 }
Пример #2
0
 private void CheckAuthorization(IRequestBase request)
 {
     if (request.Context == null || !request.Context.IsAuthorized)
     {
         throw new UnauthorizedAccessException();
     }
 }
Пример #3
0
 private static void SetMessageValuesIfNotSetInClient(IRequestBase message, IRuntime runtime)
 {
     if (message.MessageId.IsNullOrWhiteSpace())
     {
         message.MessageId = Guid.NewGuid().ToString();
     }
     if (message.Environment.IsNullOrWhiteSpace())
     {
         message.Environment = runtime.Environment;
     }
     if (message.ServiceName.IsNullOrWhiteSpace())
     {
         message.ServiceName = runtime.ServiceName;
     }
     if (message.ConfigSet.IsNullOrWhiteSpace())
     {
         message.ConfigSet = ConfigurationManagerHelper.GetValueOnKey("configSet");
     }
     if (message.TimeStamp == null)
     {
         message.TimeStamp = DateTime.UtcNow;
     }
     if (runtime.RequestContext.IsInstance())
     {
         message.ReferingMessageId = runtime.RequestContext.MessageId;
     }
 }
Пример #4
0
        private void ReleaseRequest(IRequestBase request)
        {
            if (request != null)
            {
                // Synchronization of OnReleaseRequest is the
                // responsibility of the concrete implementation of request.
                request.OnReleaseRequest();
            }

            lock (_outstandingRequests)
            {
                // Remove supports the connection having been removed, so don't need extra Contains() check,
                // even though this may have been removed by Abort()
                _outstandingRequests.Remove(request);
                if (_outstandingRequests.Count == 0)
                {
                    // When we are closed or closing, _closedTcs is managed by the close logic.
                    if (!_closed && State != CommunicationState.Closing)
                    {
                        // Protect against close altering _closedTcs concurrently
                        var closedTcs = Interlocked.CompareExchange(ref _closedTcs, null, _closedTcs);
                        if (closedTcs != null)
                        {
                            closedTcs.TrySetResult(null);
                        }
                    }
                }
            }
        }
Пример #5
0
 private void TrackRequest(IRequestBase request)
 {
     lock (this.outstandingRequests)
     {
         base.ThrowIfDisposedOrNotOpen();
         this.outstandingRequests.Add(request);
     }
 }
Пример #6
0
 private void TrackRequest(IRequestBase request)
 {
     lock (_outstandingRequests)
     {
         ThrowIfDisposedOrNotOpen(); // make sure that we haven't already snapshot our collection
         _outstandingRequests.Add(request);
     }
 }
Пример #7
0
        public async Task <TData> ExecuteAsync <TData>(IRequestBase <IResponseBase <TData> > request, CancellationToken token)
        {
            var res = await ExecuteAsync <IResponseBase <TData>, TData>(request, token);

            if (res.IsSuccess)
            {
                return(res.data);
            }
            throw new BusinessException(res.msg, res.resCode);
        }
Пример #8
0
 public IRuntime InitializeWithMessageContext(IRequestBase message)
 {
     if (message.RequestHeader.IsNull())
     {
         message.RequestHeader = new RequestHeader();
     }
     InitializeWithConfigSetName(message.RequestHeader.ConfigSet);
     RequestContext = message;
     return(this);
 }
        public virtual async Task <Response <object?> > ExecuteAsync(
            IRequestBase request,
            System.Threading.CancellationToken cancellationToken,
            IRequestHandlerExecutionContext executionContext)
        {
            // request.GetType();
            await Task.CompletedTask;

            throw new Exception();
        }
Пример #10
0
 private void ReleaseRequest(IRequestBase request)
 {
     lock (this.outstandingRequests)
     {
         this.outstandingRequests.Remove(request);
         if (((this.outstandingRequests.Count == 0) && !this.closed) && (this.closedEvent != null))
         {
             this.closedEvent.Set();
         }
     }
 }
Пример #11
0
 public void ValidateRequest(IRequestBase request)
 {
     if (request is IRequestWithAdminAuthorization)
     {
         CheckAuthorization(request);
         CheckAdminRights(request);
     }
     else if (request is IRequestWithBasicAuthorization)
     {
         CheckAuthorization(request);
     }
 }
 private static void SetMessageValuesIfNotSetInClient(IRequestBase message, IRuntime runtime)
 {
     if (message.MessageId.IsNullOrWhiteSpace())
         message.MessageId = Guid.NewGuid().ToString();
     if (message.Environment.IsNullOrWhiteSpace())
         message.Environment = runtime.Environment;
     if (message.ServiceName.IsNullOrWhiteSpace())
         message.ServiceName = runtime.ServiceName;
     if (message.ConfigSet.IsNullOrWhiteSpace())
         message.ConfigSet = ConfigurationManagerHelper.GetValueOnKey("configSet");
     if (message.TimeStamp == null)
         message.TimeStamp = DateTime.UtcNow;
     if (runtime.RequestContext.IsInstance())
         message.ReferingMessageId = runtime.RequestContext.MessageId;
 }
Пример #13
0
 private IRequestBase[] CopyPendingRequests(bool createEventIfNecessary)
 {
     IRequestBase[] array = null;
     lock (this.outstandingRequests)
     {
         if (this.outstandingRequests.Count > 0)
         {
             array = new IRequestBase[this.outstandingRequests.Count];
             this.outstandingRequests.CopyTo(array);
             this.outstandingRequests.Clear();
             if (createEventIfNecessary && (this.closedEvent == null))
             {
                 this.closedEvent = new ManualResetEvent(false);
             }
         }
     }
     return(array);
 }
Пример #14
0
        internal void LogCall(string name, IRequestBase request)
        {
            // unfortunately we cannot return the CallInfo object
            // the [Conditional] of this Log method requires void return type.
            // therefore we store it in the request;
            // the LogResponse method can then retrieve it from the request that is passed in.

            if (_LogServiceCalls == -1) return;

            if (request != null) request.DebugInfo = new CallInfo(name);

            switch (_LogServiceCalls)
            {
                case 0:
                    return;
                case 1:
                    _Log.Info("+" + name);
                    return;
            }

            if (request != null)
            {
                string session = request.SessionID.ShortString();
                switch (_LogServiceCalls)
                {
                    case 2:
                        _Log.InfoFormat("{0}|+{1}", session, name);
                        break;
                    case 3:
                        _Log.InfoFormat("{0}|+{1}: {2}", session, name, request);
                        break;
                    case 4:
                        string detail = RequestDetail(request);
                        _Log.InfoFormat("{0}|+{1}: {2}", session, name, detail);
                        break;
                }
            }
            else
            {
                _Log.InfoFormat("----|+{0}", name);
            }
        }
Пример #15
0
        public async Task <IResponseBase> ExecuteAsync(IRequestBase <IResponseBase> request, CancellationToken token)
        {
            var obj  = (IResponseBase)Activator.CreateInstance(request.ResponseType);
            var read = await ReadAsync(request, obj, token);

            if (read.isSuccess)
            {
                try
                {
                    obj = read.body.ToJson <IResponseBase>(request.ResponseType);
                }
                catch (Exception ex)
                {
                    obj.resCode = ResponseErrorCode.ToJsonError;
                    obj.msg     = ex.Message;
                    this.Logger.LogError(0, ex, "rpc content to json error.{0}", read.Item2);
                }
            }
            return(obj);
        }
Пример #16
0
        private IRequestBase[] CopyPendingRequests(bool createTcsIfNecessary)
        {
            IRequestBase[] requests = null;

            lock (_outstandingRequests)
            {
                if (_outstandingRequests.Count > 0)
                {
                    requests = new IRequestBase[_outstandingRequests.Count];
                    _outstandingRequests.CopyTo(requests);
                    _outstandingRequests.Clear();

                    if (createTcsIfNecessary && _closedTcs == null)
                    {
                        _closedTcs = new TaskCompletionSource <object>();
                    }
                }
            }

            return(requests);
        }
Пример #17
0
        IRequestBase[] CopyPendingRequests(bool createEventIfNecessary)
        {
            IRequestBase[] requests = null;

            lock (outstandingRequests)
            {
                if (outstandingRequests.Count > 0)
                {
                    requests = new IRequestBase[outstandingRequests.Count];
                    outstandingRequests.CopyTo(requests);
                    outstandingRequests.Clear();

                    if (createEventIfNecessary && closedEvent == null)
                    {
                        closedEvent = new ManualResetEvent(false);
                    }
                }
            }

            return(requests);
        }
Пример #18
0
        void ReleaseRequest(IRequestBase request)
        {
            if (request != null)
            {
                // Synchronization of OnReleaseRequest is the
                // responsibility of the concrete implementation of request.
                request.OnReleaseRequest();
            }

            lock (outstandingRequests)
            {
                // Remove supports the connection having been removed, so don't need extra Contains() check,
                // even though this may have been removed by Abort()
                outstandingRequests.Remove(request);
                if (outstandingRequests.Count == 0)
                {
                    if (!closed && closedEvent != null)
                    {
                        closedEvent.Set();
                    }
                }
            }
        }
Пример #19
0
        void IServiceBase.Initialize(IRequestBase message, string methodName = null)
        {
            if (message.RequestHeader.IsNull())
            {
                message.RequestHeader = new RequestHeader();
            }
            var environment = Utilities.Utilities.GetEnvironment();
            var serviceName = Utilities.Utilities.GetServiceName();

            if (methodName.IsNullOrWhiteSpace() && message.IsInstance())
            {
                if (message.RequestHeader.MethodName.ContainsCharacters())
                {
                    methodName = message.RequestHeader.MethodName;
                }
            }
            InitializeWithCallerName(environment, serviceName, methodName);
            if (message.RequestHeader != null && message.RequestHeader.SupportCode.ContainsCharacters())
            {
                Runtime.TrySetSupportCode(message.RequestHeader.SupportCode);
            }
            Runtime.InitializeWithMessageContext(message);
        }
Пример #20
0
        private void ReleaseRequest(IRequestBase request)
        {
            try
            {
                if (request != null)
                {
                    // Synchronization of OnReleaseRequest is the
                    // responsibility of the concrete implementation of request.
                    request.OnReleaseRequest();
                }
            }
            finally
            {
                // Setting _closedTcs needs to happen in a finally block to guarantee that we complete
                // a waiting close even if OnReleaseRequest throws
                lock (_outstandingRequests)
                {
                    _outstandingRequests.Remove(request);
                    var outstandingRequestCloseCount = Interlocked.Decrement(ref _outstandRequestCloseCount);

                    if (outstandingRequestCloseCount == 0 && _closedTcs != null)
                    {
                        // When we are closed or closing, _closedTcs is managed by the close logic.
                        if (!_closed)
                        {
                            // Protect against close altering _closedTcs concurrently by caching the value.
                            // Calling TrySetResult on an already completed TCS is a no-op
                            var closedTcs = _closedTcs;
                            if (closedTcs != null)
                            {
                                closedTcs.TrySetResult(null);
                            }
                        }
                    }
                }
            }
        }
Пример #21
0
        private void ReleaseRequest(IRequestBase request)
        {
            if (request != null)
            {
                // Synchronization of OnReleaseRequest is the
                // responsibility of the concrete implementation of request.
                request.OnReleaseRequest();
            }

            lock (_outstandingRequests)
            {
                // Remove supports the connection having been removed, so don't need extra Contains() check,
                // even though this may have been removed by Abort()
                _outstandingRequests.Remove(request);
                if (_outstandingRequests.Count == 0)
                {
                    if (!_closed && _closedTcs != null)
                    {
                        _closedTcs.TrySetResult(null);
                        _closedTcs = null;
                    }
                }
            }
        }
Пример #22
0
 public StarShipService(IRequestBase <ResponseDTO> requestBase)
 {
     _requestBase = requestBase;
 }
Пример #23
0
 private string RequestDetail(IRequestBase request)
 {
     var sb = new StringBuilder();
     sb.Append(request);
     if (request is ParameterMessageBase)
     {
         sb.Append('+');
         var r = (ParameterMessageBase)request;
         if (r.HasParameters)
         {
             foreach (var k in r.Keys)
             {
                 sb.AppendFormat("{0}={1},", k, r[k]);
             }
         }
     }
     if (sb[sb.Length - 1] == ',')
     {
         sb.Length--;
     }
     return sb.ToString();
 }
Пример #24
0
 protected void Initialize(IRequestBase request, string methodName = null)
 {
     ((IServiceBase)this).Initialize(request, methodName);
 }
Пример #25
0
 public RequestUser(IRequestBase <User> requestBase)
 {
     this.requestBase = requestBase;
 }
Пример #26
0
        internal void LogResult(IRequestBase request, object response)
        {
            if (_LogServiceCalls == -1 || request == null) return;
            var info = request.DebugInfo as CallInfo;
            if (_LogServiceResults == 0 || info == null) return;

            string duration = info.Stopwatch != null
                                                    ? string.Format(" ({0} ms)", info.Stopwatch.ElapsedMilliseconds)
                                                    : string.Empty;

            string session = request.SessionID.ShortString();
            string name = info.MethodName;
            switch (_LogServiceResults)
            {
                case 1:
                    _Log.Info("-" + name + duration);
                    break;
                case 2:
                    _Log.InfoFormat("{0}|-{1}{2}", session, name, duration);
                    break;
                case 3:
                    _Log.InfoFormat("{0}|-{1}{2}: {3}", session, name, duration, response);
                    break;
                case 4:
                    string detail = ResponseDetail(response);
                    _Log.InfoFormat("{0}|-{1}{2}: {3}", session, name, duration, detail);
                    break;
            }
        }
Пример #27
0
 public Task <IResponseBase> ExecuteAsync(IRequestBase <IResponseBase> request)
 {
     return(this.ExecuteAsync(request, CancellationToken.None));
 }
Пример #28
0
 public async Task <TRes> ExecuteAsync <TRes, TData>(IRequestBase <TRes> request, CancellationToken token)
     where TRes : class, IResponseBase <TData>
 {
     return((TRes)await this.ExecuteAsync((IRequestBase <IResponseBase>) request, token));
 }
Пример #29
0
 public Task <TRes> ExecuteAsync <TRes, TData>(IRequestBase <TRes> request)
     where TRes : class, IResponseBase <TData>
 {
     return(this.ExecuteAsync <TRes, TData>(request, CancellationToken.None));
 }
Пример #30
0
 public RequestLaboratory(IRequestBase <Laboratory> requestBase)
 {
     this.requestBase = requestBase;
 }
Пример #31
0
 public RequestComputer(IRequestBase <Computer> requestBase)
 {
     this.requestBase = requestBase;
 }