diff --git a/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs b/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs
deleted file mode 100644
index 0c639db77f9..00000000000
--- a/src/BootstrapBlazor/Extensions/ITcpSocketFactoryExtensions.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the Apache 2.0 License
-// See the LICENSE file in the project root for more information.
-// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
-
-using System.Runtime.Versioning;
-
-namespace BootstrapBlazor.Components;
-
-///
-/// 扩展方法类
-///
-[UnsupportedOSPlatform("browser")]
-public static class ITcpSocketFactoryExtensions
-{
- ///
- /// Retrieves an existing TCP socket client by name or creates a new one using the specified IP address and port.
- ///
- /// The instance used to manage TCP socket clients.
- /// The unique name identifying the TCP socket client. Cannot be null or empty.
- /// The IP address of the endpoint to connect to. Must be a valid IPv4 or IPv6 address.
- /// The port number of the endpoint to connect to. Must be within the range 0 to 65535.
- /// An instance representing the TCP socket client associated with the specified
- /// name. If no client exists with the given name, a new client is created and returned.
- public static ITcpSocketClient GetOrCreate(this ITcpSocketFactory factory, string name, string ipAddress, int port)
- {
- var endPoint = Utility.ConvertToIpEndPoint(ipAddress, port);
- return factory.GetOrCreate(name, endPoint);
- }
-}
diff --git a/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs b/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs
index 3ce84679043..4ff18905def 100644
--- a/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs
+++ b/src/BootstrapBlazor/Services/TcpSocket/DefaultTcpSocketFactory.cs
@@ -16,10 +16,11 @@ class DefaultTcpSocketFactory(IServiceProvider provider) : ITcpSocketFactory
{
private readonly ConcurrentDictionary _pool = new();
- public ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint)
+ public ITcpSocketClient GetOrCreate(string name, Func valueFactory)
{
return _pool.GetOrAdd(name, key =>
{
+ var endPoint = valueFactory(key);
var client = new DefaultTcpSocketClient(endPoint)
{
Logger = provider.GetService>()
diff --git a/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs b/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
index f479250a5d1..44db6076ca1 100644
--- a/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
+++ b/src/BootstrapBlazor/Services/TcpSocket/ITcpSocketFactory.cs
@@ -13,15 +13,14 @@ namespace BootstrapBlazor.Components;
public interface ITcpSocketFactory : IDisposable
{
///
- /// Retrieves an existing TCP socket client by name or creates a new one if it does not exist.
+ /// Retrieves an existing TCP socket client by name or creates a new one using the specified factory function.
///
- /// The unique name used to identify the TCP socket client. Cannot be null or empty.
- /// The network endpoint to associate with the TCP socket client. Must be a valid instance.
- /// An instance of representing the TCP socket client associated with the specified
- /// name and endpoint. If a client with the given name already exists, the existing instance is returned; otherwise,
- /// a new client is created.
- ITcpSocketClient GetOrCreate(string name, IPEndPoint endPoint);
+ /// The unique name identifying the TCP socket client. Cannot be null or empty.
+ /// A factory function that generates an for the client. The function is invoked if a
+ /// client with the specified name does not already exist.
+ /// An instance of associated with the specified name. If a client with the given
+ /// name already exists, the existing instance is returned; otherwise, a new instance is created.
+ ITcpSocketClient GetOrCreate(string name, Func valueFactory);
///
/// Removes the TCP socket client associated with the specified name.
diff --git a/test/UnitTest/Services/TcpSocketFactoryTest.cs b/test/UnitTest/Services/TcpSocketFactoryTest.cs
index dc5ca89ecbc..d0567beb6eb 100644
--- a/test/UnitTest/Services/TcpSocketFactoryTest.cs
+++ b/test/UnitTest/Services/TcpSocketFactoryTest.cs
@@ -24,17 +24,17 @@ public void GetOrCreate_Ok()
sc.AddBootstrapBlazorTcpSocketFactory();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService();
- var client1 = factory.GetOrCreate("demo", "localhost", 0);
+ var client1 = factory.GetOrCreate("demo", key => Utility.ConvertToIpEndPoint("localhost", 0));
client1.Close();
- var client2 = factory.GetOrCreate("demo", "localhost", 0);
+ var client2 = factory.GetOrCreate("demo", key => Utility.ConvertToIpEndPoint("localhost", 0));
Assert.Equal(client1, client2);
var ip = Dns.GetHostAddresses(Dns.GetHostName(), AddressFamily.InterNetwork).FirstOrDefault() ?? IPAddress.Loopback;
- var client3 = factory.GetOrCreate("demo1", ip.ToString(), 0);
+ var client3 = factory.GetOrCreate("demo1", key => Utility.ConvertToIpEndPoint(ip.ToString(), 0));
// 测试不合格 IP 地址
- var client4 = factory.GetOrCreate("demo2", "256.0.0.1", 0);
+ var client4 = factory.GetOrCreate("demo2", key => Utility.ConvertToIpEndPoint("256.0.0.1", 0));
var client5 = factory.Remove("demo2");
Assert.Equal(client4, client5);
@@ -450,7 +450,7 @@ private static ITcpSocketClient CreateClient()
sc.AddBootstrapBlazorTcpSocketFactory();
var provider = sc.BuildServiceProvider();
var factory = provider.GetRequiredService();
- var client = factory.GetOrCreate("test", "localhost", 0);
+ var client = factory.GetOrCreate("test", key => Utility.ConvertToIpEndPoint("localhost", 0));
return client;
}