Example #1
0
        public override Task <byte[]> RequestResponseAsync(IServiceRemotingRequestContext requestContext, ServiceRemotingMessageHeaders messageHeaders, byte[] requestBody)
        {
            var contextDto = messageHeaders.GetContextDto();

            CustomServiceContext.SetContext(contextDto);
            return(ServiceRequestContext.RunInRequestContext(async() =>
                                                             await base.RequestResponseAsync(
                                                                 requestContext,
                                                                 messageHeaders,
                                                                 requestBody),
                                                             contextDto));
        }
Example #2
0
        public static void InvokeService <TServiceInterface>(Uri serviceUri, Action <TServiceInterface> action, CustomContextDataDto context = null) where TServiceInterface : IService
        {
            if (CustomServiceContext.GetContext() == null && context != null)
            {
                CustomServiceContext.SetContext(context);
            }
            CustomContextDataDto dto     = CustomServiceContext.GetContext();
            ServiceProxyFactory  factory = new ServiceProxyFactory(CreateServiceRemotingClientFactory);
            ServicePartitionKey  key     = new ServicePartitionKey(GetLongHashCode(dto == null ? string.Empty : dto.ID));

            ServiceRequestContext.RunInRequestContext(() => {
                var serviceProxy = factory.CreateServiceProxy <TServiceInterface>(serviceUri, key);
                return(Task.Run(() => { action(serviceProxy); }));
            }, dto);
        }
        public static Task <TResult> RunInRequestContext <TResult>(Func <Task <TResult> > action, CustomContextDataDto contextDto)
        {
            Task <Task <TResult> > task = null;

            task = new Task <Task <TResult> >(async() =>
            {
                Debug.Assert(ServiceRequestContext.Current == null);
                ServiceRequestContext.Current = new ServiceRequestContext(contextDto);
                try
                {
                    return(await action());
                }
                finally
                {
                    ServiceRequestContext.Current = null;
                }
            });
            task.Start();
            return(task.Unwrap <TResult>());
        }