🎄 Happy Holidays! 🥳
Most of Solace is closed December 24–January 1 so our employees can spend time with their families. We will re-open Thursday, January 2, 2024. Please expect slower response times during this period and open a support ticket for anything needing immediate assistance.
Happy Holidays!
Please note: most of Solace is closed December 25–January 2, and will re-open Tuesday, January 3, 2023.
.NET - Context.Init throws exception when used with dependency injecetion
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 callsContextFactory.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!
Comments
-
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?
1 -
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 checkedUse 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-assemblyNot even sure if it was an error with Solace per se, just happened to be where this occurred maybe?
0