UniversalApiHubFactory.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using Edge.Core.Processor.Dispatcher;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. namespace Edge.Core.UniversalApi
  8. {
  9. public static class UniversalApiHubFactory
  10. {
  11. public static UniversalApiHub Create(IServiceProvider services)
  12. {
  13. var communicationProviderTypes = new List<Type>();
  14. foreach (var ass in ObjectInstanceCreator.CurrentDomainAssemblies)
  15. {
  16. try
  17. {
  18. //var ass = Assembly.LoadFrom(ai.FullName);
  19. var t = ass.GetTypes().Where(t => typeof(ICommunicationProvider).IsAssignableFrom(t)
  20. && !t.IsInterface);
  21. if (t != null && t.Any()) communicationProviderTypes.AddRange(t);
  22. }
  23. catch
  24. { }
  25. }
  26. var communicationProviders = new List<ICommunicationProvider>();
  27. foreach (var pType in communicationProviderTypes)
  28. {
  29. //here assume ICommunicationProvider must have 1 parameter constructor of IServiceProvider, need refine.
  30. //var providerInstance =
  31. // Activator.CreateInstance(pType, new[] { services.BuildServiceProvider() }) as ICommunicationProvider;
  32. var providerInstance = ObjectInstanceCreator.Create(pType.FullName, "", new Type[] { }, new object[] { services });
  33. //Activator.CreateInstance(pType, null) as ICommunicationProvider;
  34. communicationProviders.Add((ICommunicationProvider)providerInstance);
  35. }
  36. return new Edge.Core.UniversalApi.UniversalApiHub(communicationProviders, services);
  37. }
  38. }
  39. }