.NET - Context.Init throws exception when used with dependency injecetion

John
John Member Posts: 3

Hi!

I am trying to setup a .NET Framework 4.7.2 application for publishing to an Solace Event Broker.
I have made a class with the role to configure and contain the SolaceContext and provide it whenever I'd like to create a session to publish a message.

    public class SolaceContextContainer : ISolaceContextContainer
    {
        private readonly IContext _context;
        public SolaceContextContainer()
        {
            _context = CreateContext();
        }
        public IContext GetContext()
        {
            return _context;
        }
        private IContext CreateContext()
        {
            ConfigureContext();
            return ContextFactory.Instance.CreateContext(new ContextProperties(), null);
        }
        private void ConfigureContext()
        {

            var contextFactoryProperties = new ContextFactoryProperties()
            {
                SolClientLogLevel = SolLogLevel.Warning
            };
            contextFactoryProperties.LogToConsoleError();
            contextFactoryProperties.LogDelegate = info => LogManager.GetLogger(GetType()).Info(LogMessage.Create(info.ToString()));
            try
            {
                ContextFactory.Instance.Init(contextFactoryProperties);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }

My problem is that an exception when the instance of this class is created and it calls
ContextFactory.Instance.Init(contextFactoryProperties);

Error encountered in interop adapter An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) In method: Init   |   System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
 at SolaceSystems.Solclient.Messaging.Native.Interop.SolaceNativeAPI.LogSetCallback(LogCallbackHandler callback, IntPtr user)
at SolaceSystems.Solclient.Messaging.Native.MAdapter.MSolClientSetLogCallback(LogCallbackHandler callback, IntPtr user)

I have tried googling around but it everything I find says it has to do with CPU build targets or the like and that seems wierd.

Further, I get a working context in a test method in the same solution, where I manage to publish messages successfully to the Solace Cloud.

Can this be due to calling Init during when the instance of my container class is created?
Is this a stupid way of handling the context?

Thankful for any help!

Tagged:

Comments

  • TomF
    TomF Member, Employee Posts: 406 Solace Employee

    Hi @John, interesting. I can't see anything wrong with calling Init the way you do but I wouldn't describe my opinion as definitive on that. I'm interested in your logging delegate - if you don't set the delegate, do you still get the same error?

  • John
    John Member Posts: 3

    Hi @TomF! Thank you for the quick reply.
    Yes, I get the same error unfortunately, I added it in hope of getting more information from the API but it was basically the same stack trace there as the one that gets caught in the Catch-statement.

  • John
    John Member Posts: 3

    I think we found out a fix to the issue. Though I am still a bit confused as to why it threw the exception in this part of the application.

    I went to
    Tools > Options > Projects and Solutions > Web Projects
    and checked
    Use the 64 bit version of IIS Express for web sites and projects

    This just worked and I am not really sure why.
    Found this fix here: https://stackoverflow.com/questions/5229310/system-badimageformatexception-could-not-load-file-or-assembly

    Not even sure if it was an error with Solace per se, just happened to be where this occurred maybe?

  • TomF
    TomF Member, Employee Posts: 406 Solace Employee

    Thanks for sharing @John. We should make this an article.