namespace StockSharp.Configuration;
///
/// In memory configuration message adapter's provider.
///
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;
///
/// Initialize .
///
/// All currently available adapters.
///
public InMemoryMessageAdapterProvider(IEnumerable 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()];
}
///
public virtual IEnumerable CurrentAdapters { get; }
///
public virtual IEnumerable PossibleAdapters { get; }
///
public virtual IEnumerable CreateStockSharpAdapters(IdGenerator transactionIdGenerator, string login, SecureString password) => [];
///
public virtual IMessageAdapter CreateTransportAdapter(IdGenerator transactionIdGenerator)
{
if (_transportAdapter is null)
throw new NotSupportedException();
return _transportAdapter.CreateInstance(transactionIdGenerator);
}
}