Пример #1
0
        public static TServiceInterface CreateService <TServiceInterface>(Uri serviceUri) where TServiceInterface : IService
        {
            CustomContextDataDto dto     = CustomServiceContext.GetContext();
            ServiceProxyFactory  factory = new ServiceProxyFactory(CreateServiceRemotingClientFactory);
            ServicePartitionKey  key     = new ServicePartitionKey(GetLongHashCode(dto == null ? string.Empty : dto.ID));
            var serviceProxy             = factory.CreateServiceProxy <TServiceInterface>(serviceUri, key);

            return(serviceProxy);
        }
Пример #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);
        }
Пример #3
0
        public static void SetContext(CustomContextDataDto contextDto)
        {
            var old = GetValue(ServiceContext);

            locker.EnterWriteLock();

            try
            {
                SetValue(ServiceContext, contextDto);
            }
            finally
            {
                locker.ExitWriteLock();
            }
        }
Пример #4
0
 public static CustomContextDataDto GetContextDto(this ServiceRemotingMessageHeaders headers)
 {
     byte[] result;
     if (headers.TryGetHeaderValue("contextdata-header", out result))
     {
         string str = Encoding.UTF8.GetString(result);
         if (!string.IsNullOrEmpty(str))
         {
             CustomContextDataDto dto = JsonConvert.DeserializeObject <CustomContextDataDto>(str);
             return(dto);
         }
         return(null);
     }
     else
     {
         return(null);
     }
 }
        public static Task RunInRequestContext(Func <Task> action, CustomContextDataDto contextDto)
        {
            Task <Task> task = null;

            task = new Task <Task>(async() =>
            {
                Debug.Assert(ServiceRequestContext.Current == null);
                ServiceRequestContext.Current = new ServiceRequestContext(contextDto);
                try
                {
                    await action();
                }
                finally
                {
                    ServiceRequestContext.Current = null;
                }
            });
            task.Start();
            return(task.Unwrap());
        }
 public ServiceRequestContext(CustomContextDataDto contextDto)
 {
     this.ContextDto = contextDto;
 }
Пример #7
0
 public static bool SetContextDto(this ServiceRemotingMessageHeaders headers,  CustomContextDataDto contextData)
 {
     try
     {
         if (contextData != null)
         {
             string str = JsonConvert.SerializeObject(contextData);
             headers.AddHeader("contextdata-header", Encoding.UTF8.GetBytes(str));
             return(true);
         }
         return(false);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }