refactor http client factory for mono
This commit is contained in:
parent
9f8a1b30a1
commit
bb4c918bf8
|
@ -332,7 +332,7 @@ namespace MediaBrowser.Common.Implementations
|
|||
|
||||
RegisterSingleInstance(TaskManager);
|
||||
|
||||
HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, GetHttpMessageHandler);
|
||||
HttpClient = new HttpClientManager.HttpClientManager(ApplicationPaths, Logger, CreateHttpClient);
|
||||
RegisterSingleInstance(HttpClient);
|
||||
|
||||
NetworkManager = new NetworkManager();
|
||||
|
@ -352,7 +352,7 @@ namespace MediaBrowser.Common.Implementations
|
|||
});
|
||||
}
|
||||
|
||||
protected abstract HttpMessageHandler GetHttpMessageHandler(bool enableHttpCompression);
|
||||
protected abstract HttpClient CreateHttpClient(bool enableHttpCompression);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list of types within an assembly
|
||||
|
|
|
@ -31,21 +31,22 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
|
|||
/// </summary>
|
||||
private readonly IApplicationPaths _appPaths;
|
||||
|
||||
public delegate HttpMessageHandler GetHttpMessageHandler(bool enableHttpCompression);
|
||||
public delegate HttpClient GetHttpClientHandler(bool enableHttpCompression);
|
||||
|
||||
private readonly GetHttpMessageHandler _getHttpMessageHandler;
|
||||
private readonly GetHttpClientHandler _getHttpClientHandler;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="HttpClientManager" /> class.
|
||||
/// Initializes a new instance of the <see cref="HttpClientManager"/> class.
|
||||
/// </summary>
|
||||
/// <param name="appPaths">The kernel.</param>
|
||||
/// <param name="appPaths">The app paths.</param>
|
||||
/// <param name="logger">The logger.</param>
|
||||
/// <param name="getHttpClientHandler">The get HTTP client handler.</param>
|
||||
/// <exception cref="System.ArgumentNullException">
|
||||
/// appPaths
|
||||
/// or
|
||||
/// logger
|
||||
/// </exception>
|
||||
public HttpClientManager(IApplicationPaths appPaths, ILogger logger, GetHttpMessageHandler getHttpMessageHandler)
|
||||
public HttpClientManager(IApplicationPaths appPaths, ILogger logger, GetHttpClientHandler getHttpClientHandler)
|
||||
{
|
||||
if (appPaths == null)
|
||||
{
|
||||
|
@ -57,7 +58,7 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
|
|||
}
|
||||
|
||||
_logger = logger;
|
||||
_getHttpMessageHandler = getHttpMessageHandler;
|
||||
_getHttpClientHandler = getHttpClientHandler;
|
||||
_appPaths = appPaths;
|
||||
}
|
||||
|
||||
|
@ -90,10 +91,8 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
|
|||
{
|
||||
client = new HttpClientInfo
|
||||
{
|
||||
HttpClient = new HttpClient(_getHttpMessageHandler(enableHttpCompression))
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(20)
|
||||
}
|
||||
|
||||
HttpClient = _getHttpClientHandler(enableHttpCompression)
|
||||
};
|
||||
_httpClients.TryAdd(key, client);
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
<Properties>
|
||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Release|x86" />
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="MediaBrowser.Server.Mono\Native\HttpMessageHandlerFactory.cs">
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="MediaBrowser.Server.Mono\Native\HttpClientFactory.cs">
|
||||
<Files>
|
||||
<File FileName="MediaBrowser.Server.Mono\app.config" Line="1" Column="1" />
|
||||
<File FileName="MediaBrowser.ServerApplication\ApplicationHost.cs" Line="1" Column="1" />
|
||||
<File FileName="MediaBrowser.Server.Mono\Program.cs" Line="1" Column="1" />
|
||||
<File FileName="MediaBrowser.Server.Mono\Native\HttpMessageHandlerFactory.cs" Line="20" Column="99" />
|
||||
<File FileName="MediaBrowser.Server.Mono\Native\HttpClientFactory.cs" Line="6" Column="15" />
|
||||
</Files>
|
||||
</MonoDevelop.Ide.Workbench>
|
||||
<MonoDevelop.Ide.DebuggingService.Breakpoints>
|
||||
|
|
|
@ -81,9 +81,9 @@
|
|||
<Compile Include="..\MediaBrowser.ServerApplication\ApplicationHost.cs">
|
||||
<Link>ApplicationHost.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Native\HttpMessageHandlerFactory.cs" />
|
||||
<Compile Include="Native\Assemblies.cs" />
|
||||
<Compile Include="Native\NativeApp.cs" />
|
||||
<Compile Include="Native\HttpClientFactory.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<ItemGroup>
|
||||
|
|
24
MediaBrowser.Server.Mono/Native/HttpClientFactory.cs
Normal file
24
MediaBrowser.Server.Mono/Native/HttpClientFactory.cs
Normal file
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace MediaBrowser.ServerApplication.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Class HttpClientFactory
|
||||
/// </summary>
|
||||
public static class HttpClientFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the HTTP client.
|
||||
/// </summary>
|
||||
/// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
|
||||
/// <returns>HttpClient.</returns>
|
||||
public static HttpClient GetHttpClient(bool enableHttpCompression)
|
||||
{
|
||||
return new HttpClient()
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(20)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,24 +0,0 @@
|
|||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace MediaBrowser.ServerApplication.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Class HttpMessageHandlerFactory
|
||||
/// </summary>
|
||||
public static class HttpMessageHandlerFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the HTTP message handler.
|
||||
/// </summary>
|
||||
/// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
|
||||
/// <returns>HttpMessageHandler.</returns>
|
||||
public static HttpMessageHandler GetHttpMessageHandler(bool enableHttpCompression)
|
||||
{
|
||||
return new HttpClientHandler
|
||||
{
|
||||
AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
|
@ -672,14 +672,9 @@ namespace MediaBrowser.ServerApplication
|
|||
OnApplicationUpdated(package.version);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the HTTP message handler.
|
||||
/// </summary>
|
||||
/// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
|
||||
/// <returns>HttpMessageHandler.</returns>
|
||||
protected override HttpMessageHandler GetHttpMessageHandler(bool enableHttpCompression)
|
||||
protected override HttpClient CreateHttpClient(bool enableHttpCompression)
|
||||
{
|
||||
return HttpMessageHandlerFactory.GetHttpMessageHandler(enableHttpCompression);
|
||||
return HttpClientFactory.GetHttpClient(enableHttpCompression);
|
||||
}
|
||||
|
||||
protected override void ConfigureAutoRunAtStartup(bool autorun)
|
||||
|
|
|
@ -188,7 +188,7 @@
|
|||
<Compile Include="EntryPoints\StartupWizard.cs" />
|
||||
<Compile Include="FFMpeg\FFMpegInfo.cs" />
|
||||
<Compile Include="Native\Assemblies.cs" />
|
||||
<Compile Include="Native\HttpMessageHandlerFactory.cs" />
|
||||
<Compile Include="Native\HttpClientFactory.cs" />
|
||||
<Compile Include="Native\NativeApp.cs" />
|
||||
<Compile Include="Native\ServerAuthorization.cs" />
|
||||
<Compile Include="Native\Autorun.cs" />
|
||||
|
|
|
@ -1,25 +1,29 @@
|
|||
using System.Net;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace MediaBrowser.ServerApplication.Native
|
||||
{
|
||||
/// <summary>
|
||||
/// Class HttpMessageHandlerFactory
|
||||
/// Class HttpClientFactory
|
||||
/// </summary>
|
||||
public static class HttpMessageHandlerFactory
|
||||
public static class HttpClientFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the HTTP message handler.
|
||||
/// Gets the HTTP client.
|
||||
/// </summary>
|
||||
/// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
|
||||
/// <returns>HttpMessageHandler.</returns>
|
||||
public static HttpMessageHandler GetHttpMessageHandler(bool enableHttpCompression)
|
||||
/// <returns>HttpClient.</returns>
|
||||
public static HttpClient GetHttpClient(bool enableHttpCompression)
|
||||
{
|
||||
return new WebRequestHandler
|
||||
return new HttpClient(new WebRequestHandler
|
||||
{
|
||||
CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate),
|
||||
AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None
|
||||
})
|
||||
{
|
||||
Timeout = TimeSpan.FromSeconds(20)
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user