Merge pull request #561 from digitalfuller/httpclientfactory-with-generic-interfaces
Ensure Refit clients can be registered in the DI container for HttpClientFactory.
This commit is contained in:
Коммит
e7339a5572
|
@ -15,7 +15,7 @@ namespace Refit
|
|||
{
|
||||
services.AddSingleton(provider => RequestBuilder.ForType<T>(settings));
|
||||
|
||||
return services.AddHttpClient(typeof(T).Name)
|
||||
return services.AddHttpClient(UniqueName.ForType<T>())
|
||||
.AddTypedClient((client, serviceProvider) => RestService.For<T>(client, serviceProvider.GetService<IRequestBuilder<T>>()));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
namespace Refit.Tests
|
||||
{
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit;
|
||||
|
||||
public class HttpClientFactoryExtensionsTests
|
||||
{
|
||||
class User
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
class Role
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericHttpClientsAreAssignedUniqueNames()
|
||||
{
|
||||
var services = new ServiceCollection();
|
||||
|
||||
var userClientName = services.AddRefitClient<IBoringCrudApi<User, string>>().Name;
|
||||
var roleClientName = services.AddRefitClient<IBoringCrudApi<Role, string>>().Name;
|
||||
|
||||
Assert.NotEqual(userClientName, roleClientName);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0-preview-20180610-02" />
|
||||
<PackageReference Include="xunit" Version="2.4.0-beta.2.build4010" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0-beta.2.build4010" />
|
||||
|
|
|
@ -0,0 +1,68 @@
|
|||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
|
||||
namespace Refit.Tests
|
||||
{
|
||||
namespace Http
|
||||
{
|
||||
class Client
|
||||
{
|
||||
public class Request { }
|
||||
|
||||
public class Response { }
|
||||
}
|
||||
}
|
||||
|
||||
namespace Tcp
|
||||
{
|
||||
class Client { }
|
||||
}
|
||||
|
||||
public class UniqueNameTests
|
||||
{
|
||||
[Fact]
|
||||
public void SystemTypeAndLanguageTypeHaveSameNames()
|
||||
{
|
||||
var name1 = UniqueName.ForType<System.Int32>();
|
||||
var name2 = UniqueName.ForType<int>();
|
||||
|
||||
Assert.Equal(name1, name2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GenericClassWithDifferentTypesHaveUniqueNames()
|
||||
{
|
||||
var name1 = UniqueName.ForType<List<long>>();
|
||||
var name2 = UniqueName.ForType<List<int>>();
|
||||
|
||||
Assert.NotEqual(name1, name2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SameClassNameInDifferentNamespacesHaveUniqueNames()
|
||||
{
|
||||
var name1 = UniqueName.ForType<Http.Client>();
|
||||
var name2 = UniqueName.ForType<Tcp.Client>();
|
||||
|
||||
Assert.NotEqual(name1, name2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ClassesWithNestedClassesHaveUniqueNames()
|
||||
{
|
||||
var name1 = UniqueName.ForType<Http.Client>();
|
||||
var name2 = UniqueName.ForType<Http.Client.Request>();
|
||||
|
||||
Assert.NotEqual(name1, name2);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void NestedClassesHaveUniqueNames()
|
||||
{
|
||||
var name1 = UniqueName.ForType<Http.Client.Request>();
|
||||
var name2 = UniqueName.ForType<Http.Client.Response>();
|
||||
|
||||
Assert.NotEqual(name1, name2);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
using System.Reflection;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Refit.Tests")]
|
||||
[assembly: InternalsVisibleTo("Refit.Tests")]
|
||||
[assembly: InternalsVisibleTo("Refit.HttpClientFactory")]
|
||||
|
|
|
@ -57,24 +57,7 @@ namespace Refit
|
|||
|
||||
static Type GetGeneratedType<T>()
|
||||
{
|
||||
string typeName = string.Empty;
|
||||
|
||||
if (typeof(T).IsNested)
|
||||
{
|
||||
var className = "AutoGenerated" + typeof(T).DeclaringType.Name + typeof(T).Name;
|
||||
typeName = typeof(T).AssemblyQualifiedName.Replace(typeof(T).DeclaringType.FullName + "+" + typeof(T).Name, typeof(T).Namespace + "." + className);
|
||||
}
|
||||
else
|
||||
{
|
||||
var className = "AutoGenerated" + typeof(T).Name;
|
||||
|
||||
if (typeof(T).Namespace == null)
|
||||
{
|
||||
className = $"{className}.{className}";
|
||||
}
|
||||
|
||||
typeName = typeof(T).AssemblyQualifiedName.Replace(typeof(T).Name, className);
|
||||
}
|
||||
string typeName = UniqueName.ForType<T>();
|
||||
|
||||
var generatedType = Type.GetType(typeName);
|
||||
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
namespace Refit
|
||||
{
|
||||
internal class UniqueName
|
||||
{
|
||||
public static string ForType<T>()
|
||||
{
|
||||
string typeName;
|
||||
|
||||
if (typeof(T).IsNested)
|
||||
{
|
||||
var className = "AutoGenerated" + typeof(T).DeclaringType.Name + typeof(T).Name;
|
||||
typeName = typeof(T).AssemblyQualifiedName.Replace(typeof(T).DeclaringType.FullName + "+" + typeof(T).Name, typeof(T).Namespace + "." + className);
|
||||
}
|
||||
else
|
||||
{
|
||||
var className = "AutoGenerated" + typeof(T).Name;
|
||||
|
||||
if (typeof(T).Namespace == null)
|
||||
{
|
||||
className = $"{className}.{className}";
|
||||
}
|
||||
|
||||
typeName = typeof(T).AssemblyQualifiedName.Replace(typeof(T).Name, className);
|
||||
}
|
||||
|
||||
return typeName;
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче