提交 0f41e2ba 编写于 作者: 若汝棋茗

修复容器

上级 056e06e0
......@@ -12,6 +12,9 @@
//------------------------------------------------------------------------------
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TouchSocket.Core.Dependency;
namespace TouchSocket.Core.AspNetCore
......@@ -22,7 +25,6 @@ namespace TouchSocket.Core.AspNetCore
public class AspNetCoreContainer : IContainer
{
private readonly IServiceCollection m_services;
private readonly Container m_container = new Container();
/// <summary>
/// 初始化一个IServiceCollection的容器。
......@@ -30,9 +32,24 @@ namespace TouchSocket.Core.AspNetCore
/// <param name="services"></param>
public AspNetCoreContainer(IServiceCollection services)
{
services.AddSingleton<IContainer>(this);
this.m_services = services;
}
/// <summary>
/// 返回迭代器
/// </summary>
/// <returns></returns>
public IEnumerator<DependencyDescriptor> GetEnumerator()
{
return this.m_services.ToList().Select(s =>
{
DependencyDescriptor descriptor = new DependencyDescriptor(s.ServiceType, s.ImplementationType, (Lifetime)((int)s.Lifetime));
descriptor.ToInstance = s.ImplementationInstance;
return descriptor;
}).GetEnumerator();
}
/// <summary>
/// <inheritdoc/>
/// </summary>
......@@ -41,7 +58,19 @@ namespace TouchSocket.Core.AspNetCore
/// <returns></returns>
public bool IsRegistered(Type fromType, string key = "")
{
return ((IContainerProvider)this.m_container).IsRegistered(fromType, key);
if (fromType.IsGenericType)
{
fromType = fromType.GetGenericTypeDefinition();
}
var array = m_services.ToArray();
foreach (var item in array)
{
if (item.ServiceType == fromType)
{
return true;
}
}
return false;
}
/// <summary>
......@@ -51,7 +80,14 @@ namespace TouchSocket.Core.AspNetCore
/// <param name="key"></param>
public void Register(DependencyDescriptor descriptor, string key = "")
{
this.m_container.Register(descriptor, key);
if (!key.IsNullOrEmpty())
{
throw new NotSupportedException($"{this.GetType().Name}不支持包含Key的多实现注入");
}
if (this.IsRegistered(descriptor.FromType))
{
this.Unregister(descriptor.FromType);
}
switch (descriptor.Lifetime)
{
case Lifetime.Singleton:
......@@ -64,6 +100,7 @@ namespace TouchSocket.Core.AspNetCore
this.m_services.AddSingleton(descriptor.FromType, descriptor.ToType);
}
break;
case Lifetime.Transient:
default:
this.m_services.AddTransient(descriptor.FromType, descriptor.ToType);
......@@ -80,15 +117,17 @@ namespace TouchSocket.Core.AspNetCore
/// <returns></returns>
public object Resolve(Type fromType, object[] ps = null, string key = "")
{
ServiceProvider provider = this.m_services.BuildServiceProvider();
object obj = provider.GetService(fromType);
if (obj != null)
if (fromType == typeof(IContainer))
{
return this;
}
if (fromType == typeof(IServiceProvider))
{
return obj;
return this.m_services.BuildServiceProvider();
}
ServiceProvider provider = this.m_services.BuildServiceProvider();
return this.m_container.Resolve(fromType, ps, key);
return provider.GetService(fromType);
}
/// <summary>
......@@ -98,7 +137,20 @@ namespace TouchSocket.Core.AspNetCore
/// <param name="key"></param>
public void Unregister(DependencyDescriptor descriptor, string key = "")
{
((IContainer)this.m_container).Unregister(descriptor, key);
var array = m_services.ToArray();
foreach (var item in array)
{
if (item.ServiceType == descriptor.FromType)
{
m_services.Remove(item);
return;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}
\ No newline at end of file
......@@ -69,7 +69,7 @@ namespace TouchSocket.Rpc.TouchRpc.AspNetCore
/// <summary>
/// 断开连接
/// </summary>
public event ClientDisconnectedEventHandler<WSTouchRpcClient> Disconnected;
public ClientDisconnectedEventHandler<WSTouchRpcClient> Disconnected { get; set; }
/// <summary>
/// 方法映射表
......
......@@ -5,7 +5,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.7.0</Version>
<Version>0.7.1</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
......@@ -29,14 +29,6 @@ API:https://www.yuque.com/rrqm/touchsocket/index</Description>
<DebugSymbols>true</DebugSymbols>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>
<Deterministic>true</Deterministic>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6|AnyCPU'">
<DocumentationFile>bin\Debug\net6\TouchSocket.AspNetCore.xml</DocumentationFile>
......@@ -77,6 +69,6 @@ API:https://www.yuque.com/rrqm/touchsocket/index</Description>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="3.1.26" />
<PackageReference Include="Microsoft.Extensions.Options" Version="3.0.0" />
<PackageReference Include="TouchSocket" Version="0.7.0" />
<PackageReference Include="TouchSocket" Version="0.7.1" />
</ItemGroup>
</Project>
......@@ -27,16 +27,9 @@ API:https://www.yuque.com/rrqm/touchsocket/index
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
<RepositoryUrl>https://gitee.com/dotnetchina/touchsocket</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
<EmbedUntrackedSource>true</EmbedUntrackedSource>
<EmbedAllSources>true</EmbedAllSources>
<RepositoryType>Gitee</RepositoryType>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netstandard2.0|AnyCPU'">
<DocumentationFile>bin\Debug\netstandard2.0\TouchSocket.xml</DocumentationFile>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册