Files
StockSharp/Configuration/InMemoryMessageAdapterProvider.cs

70 lines
2.3 KiB
C#

namespace StockSharp.Configuration;
/// <summary>
/// In memory configuration message adapter's provider.
/// </summary>
public class InMemoryMessageAdapterProvider : IMessageAdapterProvider
{
static InMemoryMessageAdapterProvider()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll");
return File.Exists(assemblyPath)
? Assembly.LoadFrom(assemblyPath)
: null;
}
private readonly Type _transportAdapter;
/// <summary>
/// Initialize <see cref="InMemoryMessageAdapterProvider"/>.
/// </summary>
/// <param name="currentAdapters">All currently available adapters.</param>
/// <param name="transportAdapter"><see cref="CreateTransportAdapter"/></param>
public InMemoryMessageAdapterProvider(IEnumerable<IMessageAdapter> currentAdapters, Type transportAdapter = null)
{
CurrentAdapters = currentAdapters ?? throw new ArgumentNullException(nameof(currentAdapters));
_transportAdapter = transportAdapter;
var idGenerator = new IncrementalIdGenerator();
// what a user may choose from is what ships with the application, so the scan follows the
// binaries rather than the folder the process happened to be started from.
PossibleAdapters = [.. AppContext.BaseDirectory.FindAdapters(ex => ex.LogError()).Select(t =>
{
try
{
return t.CreateAdapter(idGenerator);
}
catch (Exception ex)
{
ex.LogError();
return null;
}
}).WhereNotNull()];
}
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> CurrentAdapters { get; }
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> PossibleAdapters { get; }
/// <inheritdoc />
public virtual IEnumerable<IMessageAdapter> CreateStockSharpAdapters(IdGenerator transactionIdGenerator, string login, SecureString password) => [];
/// <inheritdoc />
public virtual IMessageAdapter CreateTransportAdapter(IdGenerator transactionIdGenerator)
{
if (_transportAdapter is null)
throw new NotSupportedException();
return _transportAdapter.CreateInstance<IMessageAdapter>(transactionIdGenerator);
}
}