lextudio / sharpsnmplib

Sharp SNMP Library- Open Source SNMP for .NET
https://sharpsnmp.com
MIT License
356 stars 152 forks source link

snmptrapd sample cannot find "UserRegistry" #217

Closed lextudio-support closed 4 hours ago

lextudio-support commented 4 hours ago

Hi

I'm trying to set up my application to listen to traps, and have used the sample code from snmptrapd (including the app.config file).

My application is throwing a 'System.InvalidOperationException' occurred in Microsoft.Practices.Unity.Configuration.dll (Additional information: The type name or alias UserRegistry could not be resolved. Please check your configuration file and verify this type name.)

At the line

Container = new UnityContainer().LoadConfiguration("snmptrapd");

It seems to be due to this type= entry from the users section, but for the life of me I can't figure out how to correct it.

 <!-- users -->
        <register type="UserRegistry">

I have simply copied the app.config file from the snmptrapd sample. I'm using whatever version of Unity was available via NuGet today, and the latest SharpSnmp, also from NuGet.

It might be something to do with referencing the assembly (StackOverflow article here), but I'm not sufficiently versed in configuration files to know what to enter.

What am I doing wrong?

Thanks. Giles.

Original Reported Date: 2015-04-28T13:36:40.177-07:00 Planned For Release:

Original CodePlex ID: 7272

lextudio-support commented 4 hours ago

Copied from CodePlex without authors:

Found it!

Change the app.config lines:

    <assembly name="SharpSnmpLib.Mib" />
    <assembly name="SharpSnmpLib" />

to:

    <assembly name="SharpSnmpLib.Full" />
    <assembly name="SharpSnmpLib.Portable" />

That fixes the name resolution. Makes sense now I think about it.

My application does not need to be configured at run time, indeed I'd much rather do away with the app.config configuration file entirely. I only need V2 traps. Is it easy to configure Unity without the configuration file?

Giles.

Original Posted Date: 2015-04-28T14:00:49.8-07:00

lextudio-support commented 4 hours ago

Copied from CodePlex without authors:

There will be a newer version to get rid of those configuration bits.

Original Posted Date: 2015-04-28T22:13:12.543-07:00

lextudio-support commented 4 hours ago

Copied from CodePlex without authors:

Thanks

Having looked through the code and at app.config I more or less worked out what was happening, so I managed to get my application to receive V2 traps, without needing Unity or app.config using the following.

This is posted here to help others, but I may have misunderstood bits! Your mileage may vary (immensely).

private void Init()
{
        // trap v2 message handler
        var trapV2Handler = new TrapV2MessageHandler();
        trapV2Handler.MessageReceived += trapv2_MessageReceived;
        var trapV2 = new HandlerMapping("v2,v3", "TRAPV2", trapV2Handler);
        var messageFactory = new MessageHandlerFactory(new HandlerMapping[] { trapV2 });

        // user authentication (only allows traps from these contexts - NB in my case traps are sent with empty/null context)
        var membershipProvider = new ComposedMembershipProvider(new[] { new Version2MembershipProvider(new OctetString(""), new OctetString("")) });

        // snmp engine and socket binding
        trapd = new SnmpEngine(new SnmpApplicationFactory(new ObjectStore(), membershipProvider, messageFactory), new Listener(), new EngineGroup());
        trapd.Listener.AddBinding(new IPEndPoint(IPAddress.Any, 162));                    
        trapd.ExceptionRaised += trapd_ExceptionRaised;
        trapd.Listener.MessageReceived += Listener_MessageReceived;
        trapd.Start();
}

void Listener_MessageReceived(object sender, MessageReceivedEventArgs e)
{
        // All messages come here, prior to being authorised and passed to the TrapMessageHandler(s)
}

void trapd_ExceptionRaised(object sender, ExceptionRaisedEventArgs e)
{
        // warn of a failure
}

void trapv2_MessageReceived(object sender, TrapV2MessageReceivedEventArgs e)
{
        // list of received variables in the trap is in e.TrapV2Message.Scope.Pdu.Variables
}

Cheers Giles.

Original Posted Date: 2015-04-29T03:23:48.943-07:00

lextudio-support commented 4 hours ago

Copied from CodePlex without authors:

What you did is not a complete port without Unity. But still thanks for sharing.

https://github.com/lextm/sharpsnmplib/commit/c06782884b031a068284e7f67fab034289218c86

The final bits I finished can be found at GitHub. You can check out the code and play with it.

Original Posted Date: 2015-05-05T23:07:05.797-07:00

lextudio-support commented 4 hours ago

Original Closed Date: 2015-05-05T23:07:13.953-07:00