Exemplo n.º 1
0
        /// <summary>
        /// Called when an <see cref="InstanceContext"/> object recycles a service object.
        /// </summary>
        /// <param name="instanceContext">The service's instance context.</param>
        /// <param name="instance">The service object to be recycled.</param>
        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
            InstanceContextServiceScope ctxScope = instanceContext.Extensions.Find <InstanceContextServiceScope>();

            if (ctxScope != null)
            {
                ctxScope.Dispose();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a service object given the specified System.ServiceModel.InstanceContext object.
        /// </summary>
        /// <param name="instanceContext">The current InstanceContext object.</param>
        /// <param name="message">The message that triggered the creation of a service object.</param>
        /// <returns>The service object.</returns>
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            InstanceContextServiceScope ctxScope = instanceContext.Extensions.Find <InstanceContextServiceScope>();

            if (ctxScope == null)
            {
                Trace.TraceError("InstanceContextServiceScope not registred for instance context. Using default service provider");
                return(serviceProvider.GetService(contractType));
            }
            IServiceScope scope = ctxScope.GetServiceScope(serviceProvider);

            return(scope.ServiceProvider.GetService(contractType));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Called before client calls are sent and after service responses are returned.
        /// </summary>
        /// <param name="operationName">The name of the operation.</param>
        /// <param name="inputs">The objects passed to the method by the client.</param>
        /// <returns>The correlation state that is returned as the correlationState parameter in <see cref="AfterCall(string, object[], object, object)"/>.
        /// Return null if you do not intend to use correlation state.</returns>
        public object BeforeCall(string operationName, object[] inputs)
        {
            InstanceContextServiceScope ctxScope = OperationContext.Current.InstanceContext.Extensions.Find <InstanceContextServiceScope>();
            var       svcProvider = ctxScope?.GetServiceScope(null)?.ServiceProvider;
            ErrorList errors      = svcProvider?.GetService <ErrorList>();

            if (errors == null)
            {
                return(null);
            }

            foreach (object obj in inputs)
            {
                foreach (ValidationResult result in DataAnnotationValidator.GetValidationErrors(serviceProvider, obj))
                {
                    errors.AddValidationError(result.ErrorMessage);
                }
            }
            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Returns a service object given the specified <see cref="InstanceContext"/> object.
        /// </summary>
        /// <param name="instanceContext">The current InstanceContext object.</param>
        /// <param name="message">The message that triggered the creation of a service object.</param>
        /// <returns>The service object.</returns>
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            InstanceContextServiceScope ctxScope = instanceContext.Extensions.Find <InstanceContextServiceScope>();

            if (ctxScope == null)
            {
                Trace.TraceError("InstanceContextServiceScope not registered for instance context. Using default service provider");
                return(serviceProvider.GetService(contractType));
            }
            IServiceScope scope = ctxScope.GetServiceScope(serviceProvider);

            // find and set the current principal on the principal provider for the current scope
            IPrincipalProvider principalProvider = scope.ServiceProvider.GetService <IPrincipalProvider>();

            if (principalProvider != null)
            {
                if (ServiceSecurityContext.Current.AuthorizationContext.Properties.TryGetValue("ClaimsPrincipal", out object principal))
                {
                    principalProvider.CurrentPrincipal = principal as ClaimsPrincipal;
                }
            }

            return(scope.ServiceProvider.GetService(contractType));
        }