本文整理汇总了C#中System.Net.Http.DelegatingHandler类的典型用法代码示例。如果您正苦于以下问题:C# DelegatingHandler类的具体用法?C# DelegatingHandler怎么用?C# DelegatingHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DelegatingHandler类属于System.Net.Http命名空间,在下文中一共展示了DelegatingHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConfigureRoute
public MusicStoreBootStrap ConfigureRoute()
{
// List of delegating handlers.
DelegatingHandler[] handlers = new DelegatingHandler[] { new AdminAuthorizationMessageHandler(new AdminUserQuery()) };
// Create a message handler chain with an end-point.
var routeHandlers = HttpClientFactory.CreatePipeline(
new HttpControllerDispatcher(httpConfiguration), handlers);
httpConfiguration.Routes.MapHttpRoute(
name: "AdminRoutes",
routeTemplate: "api/AdminUser/{user}",
defaults: new { controller = "AdminUser", user = RouteParameter.Optional }
, constraints: null
, handler: routeHandlers);
httpConfiguration.Routes.MapHttpRoute(
name: "Inventory",
routeTemplate: "api/Inventory/{id}",
defaults: new { controller = "Inventory", id = RouteParameter.Optional }
, constraints: null
, handler: routeHandlers);
httpConfiguration.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { controller = "Product", id = RouteParameter.Optional });
httpConfiguration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { controller = "Product", id = RouteParameter.Optional });
httpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
return this;
}
示例2: CreateHandler
public DelegatingHandler CreateHandler(HttpConfiguration httpConfig, DelegatingHandler innerHandler = null)
{
return new CorsMessageHandler(this, httpConfig)
{
InnerHandler = innerHandler
};
}
示例3: Register
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.MessageHandlers.Add(new CustomHeaderHandler());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var handlers = new DelegatingHandler[]
{
new PreRouteMessageHandler()
};
var routeHandlers = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(config), handlers);
config.Routes.MapHttpRoute(
name: "Route2",
routeTemplate: "api1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: null,
handler: routeHandlers
);
config.MessageHandlers.Add(new MethodOverrideHandler());
}
示例4: PostForm
internal async Task<dynamic> PostForm(
IEnumerable<KeyValuePair<string, string>> formParameters,
string path, DelegatingHandler[] handlers, IEnumerable<KeyValuePair<string, string>> headerParameters = null, bool shouldRetry = true)
{
using (var client = HttpClientFactory.Create(handlers == null
? new DelegatingHandler[] { new UserAgentHandler() }
: handlers.ToArray()))
{
var uri = _baseAddress + path;
var httpResponse =
await client.PostAsync(uri, new FormUrlEncodedContent(formParameters));
try
{
return await HandleResponse(httpResponse);
}
catch (UnauthorizedException)
{
if (shouldRetry)
{
_credentials.ResetAccessToken();
using (var retryClient = HttpClientFactory.Create(await GetHandlers(headerParameters)))
{
httpResponse = await retryClient.PostAsync(uri, new FormUrlEncodedContent(formParameters));
return await HandleResponse(httpResponse);
}
}
throw;
}
}
}
示例5: CreateEncryptedRoute
private void CreateEncryptedRoute(HttpConfiguration configuration)
{
var section = (NameValueCollection)ConfigurationManager.GetSection("Api2CryptoGraphySettings");
if (section == null)
{
throw new ApplicationException("Config section 'RestfulWebService' has not been set.");
}
// arrange.
var settings = new CryptoGraphySettings(section);
var cryptoHandler = new ServerCryptoHandler(settings.SecretKey, settings.InitialVector, settings.HashKey);
var injection = new DelegatingHandler[] { new ServerMessageDumper(), cryptoHandler }; // dump decrypted request & plain response.
var handler = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(configuration), injection);
// register encrypted HTTP route.
configuration.Routes.MapHttpRoute("Encrypted Route", "api2/{controller}/{action}", null, null, handler);
// register timestamp as a route.
var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)) as ITimestampProvider<string>;
var timestampHandler = new HttpTimestampHandler<string>(timestampProvider);
configuration.Routes.MapHttpRoute("Timestamp Route", "api3/!timestamp!/get", null, null, timestampHandler);
// register global timestamp service, it should align with encrypted HTTP route or will not work.
configuration.MessageHandlers.Add(new HttpTimestampHandler<string>(configuration, "api2/!timestamp!/get", timestampProvider));
}
示例6: MessageHandlerTracer
public MessageHandlerTracer(DelegatingHandler innerHandler, ITraceWriter traceWriter)
{
Contract.Assert(innerHandler != null);
Contract.Assert(traceWriter != null);
_innerHandler = innerHandler;
_traceWriter = traceWriter;
}
示例7: GetChains
private IEnumerable<string> GetChains(DelegatingHandler handler)
{
yield return handler.GetType().FullName;
var innerHander = handler.InnerHandler as DelegatingHandler;
if (innerHander != null)
{
yield return innerHander.GetType().FullName;
}
}
示例8: RetryDelegatingHandler
/// <summary>
/// Initializes a new instance of the <see cref="RetryDelegatingHandler"/> class.
/// </summary>
/// <param name="retryPolicy">Retry policy to use.</param>
/// <param name="innerHandler">Inner http handler.</param>
public RetryDelegatingHandler(RetryPolicy retryPolicy, DelegatingHandler innerHandler)
: base(innerHandler)
{
if (retryPolicy == null)
{
throw new ArgumentNullException("retryPolicy");
}
RetryPolicy = retryPolicy;
}
示例9: CreateBatchRestClient
protected override BatchServiceClient CreateBatchRestClient(string url, string accountName, string key, DelegatingHandler handler = default(DelegatingHandler))
{
// Add HTTP recorder to the BatchRestClient
HttpMockServer mockServer = HttpMockServer.CreateInstance();
mockServer.InnerHandler = new HttpClientHandler();
BatchServiceClient restClient = base.CreateBatchRestClient(url, accountName, key, mockServer);
return restClient;
}
示例10: RegisterEncryptedRoute
private void RegisterEncryptedRoute(HttpConfiguration configuration, ITimestampProvider<string> timestampProvider)
{
// arrange.
var cryptoHandler = new ServerCryptoHandler("secretKeyPassword", "initialVectorPassword", "hashKeyString", timestampProvider);
var injection = new DelegatingHandler[] { new ServerMessageDumper(), cryptoHandler, new ServerMessageDumper() };
var handler = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(configuration), injection);
// register encrypted HTTP route.
configuration.Routes.MapHttpRoute("Encrypted API", "api2/{controller}/{action}", null, null, handler);
}
示例11: ExecuteRequest
public HttpResponseMessage ExecuteRequest(DelegatingHandler testTarget, HttpRequestMessage requestMessage)
{
testTarget.InnerHandler = new OKHandler();
InnerHandler = testTarget;
var requestTask = SendAsync(requestMessage, new CancellationToken());
requestTask.Wait(5000); // 5 second timeout - tests should be quicker than this, but better than infinite for now
return requestTask.Result;
}
示例12: BasicAuthenticationHandler
public BasicAuthenticationHandler(DelegatingHandler innerChannel, IUserValidation userValidation, string realm)
: base(innerChannel)
{
if (userValidation == null)
throw new ArgumentNullException("userValidation");
if (string.IsNullOrEmpty(realm))
throw new ArgumentNullException("realm");
this.userValidation = userValidation;
this.realm = realm;
}
示例13: GetHandlerChain
/// <summary>
/// 获得MyHttpServer牵头的HttpMessageHandler链
/// </summary>
/// <param name="handler"></param>
/// <returns></returns>
private IEnumerable<string> GetHandlerChain(DelegatingHandler handler)
{
yield return handler.GetType().Name;
while (null != handler.InnerHandler)
{
yield return handler.InnerHandler.GetType().Name;
handler = handler.InnerHandler as DelegatingHandler;
if (null == handler)
{
break;
}
}
}
示例14: ExecuteRequest
public HttpResponseMessage ExecuteRequest(DelegatingHandler testTarget, HttpRequestMessage requestMessage)
{
testTarget.InnerHandler = new OKHandler();
InnerHandler = testTarget;
var requestTask = SendAsync(requestMessage, new CancellationToken());
while (!requestTask.IsCompleted)
{
}
return requestTask.Result;
}
示例15: Main
static void Main(string[] args)
{
var baseAddress = "http://localhost:8080";
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(baseAddress);
// config.MessageHandlers.Add(new ProgressMessageHandler() { });
// Web API 路由
config.MapHttpAttributeRoutes();
//全局允许CROS
// config.EnableCors();//启用跨域
config.Routes.MapHttpRoute
(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MaxConcurrentRequests = 1000;
var handlers = new DelegatingHandler[] { new PassiveAuthenticationMessageHandler(), new HttpServer() };
var routeHandlers = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(config), handlers);
config.Routes.MapHttpRoute(
name: "CustomerRouter",
routeTemplate: "MyAPI/{Controller}/{Action}/Id",
defaults: new { Id = RouteParameter.Optional },
constraints: null,
handler: routeHandlers
);
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync();
TaskManager.Instance.Initialize();
TaskManager.Instance.Start();
DefaultHttpBatchHandler batchHandler = new DefaultHttpBatchHandler(server);
Console.WriteLine("Server http://localhost:8080 Open now ....at {0}..", server.Configuration.VirtualPathRoot);
config.EnsureInitialized();
foreach (var route in config.Routes)
{
System.Diagnostics.Debug.WriteLine(route);
}
Console.ReadLine();
}