Created
January 16, 2012 15:33
-
-
Save mfloryan/1621399 to your computer and use it in GitHub Desktop.
ServiceClientBaseHeaderInjectingExtension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Security.Principal; | |
| using System.ServiceModel; | |
| using System.ServiceModel.Channels; | |
| using Adp.Shared.Security; | |
| namespace Adp.Shared.ServiceClient | |
| { | |
| public static class ServiceClientBaseHeaderInjectingExtension | |
| { | |
| /// <summary> | |
| /// Sets the OID and AOID headers and calls the service. | |
| /// First argument to action is OID | |
| /// </summary> | |
| public static TResult CallServiceSettingHeaders<TResult, TService>( | |
| this ClientBase<TService> serviceClient, | |
| IPrincipal user, | |
| Func<string, TResult> action) where TService : class | |
| { | |
| using (var scope = new OperationContextScope(serviceClient.InnerChannel)) | |
| { | |
| AddHeaders(user); | |
| return action(user.Oid()); | |
| } | |
| } | |
| /// <summary> | |
| /// Sets the OID and AOID headers and calls the service. | |
| /// First argument to action is OID and the second in AOID | |
| /// </summary> | |
| public static TResult CallServiceSettingHeaders<TResult, TService>( | |
| this ClientBase<TService> serviceClient, | |
| IPrincipal user, | |
| Func<string, string, TResult> action) where TService : class | |
| { | |
| using (var scope = new OperationContextScope(serviceClient.InnerChannel)) | |
| { | |
| AddHeaders(user); | |
| return action(user.Oid(), user.Aoid()); | |
| } | |
| } | |
| /// <summary> | |
| /// Sets the OID and AOID headers and calls the service. | |
| /// First argument to action is OID and the second in AOID | |
| /// Third argument is passed through | |
| /// </summary> | |
| public static TResult CallServiceSettingHeaders<TResult, TService, TArg>( | |
| this ClientBase<TService> serviceClient, | |
| IPrincipal user, | |
| Func<string, string, TArg, TResult> action, | |
| TArg arg) where TService : class | |
| { | |
| using (var scope = new OperationContextScope(serviceClient.InnerChannel)) | |
| { | |
| AddHeaders(user); | |
| return action(user.Oid(), user.Aoid(), arg); | |
| } | |
| } | |
| private static void AddHeaders(IPrincipal user) | |
| { | |
| var headers = new HttpRequestMessageProperty(); | |
| headers.Headers.Add("oid", user.Oid()); | |
| headers.Headers.Add("aoid", user.Aoid()); | |
| OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = headers; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment