{"record":{"id":"391570394aa9768e","repo":"BornToBeRoot/NETworkManager","slug":"could-not-load-psdiscoveryprotocol-psm1","errorCode":null,"errorMessage":"Could not load PSDiscoveryProtocol.psm1","messagePattern":"Could not load PSDiscoveryProtocol\\.psm1","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"Source/NETworkManager.Models/Network/DiscoveryProtocol.cs","lineNumber":29,"sourceCode":"///     Class to capture network discovery protocol packages.\n/// </summary>\npublic class DiscoveryProtocolCapture\n{\n    /// <summary>\n    ///     Holds the PowerShell script which is loaded when the class is initialized.\n    /// </summary>\n    private readonly string _psDiscoveryProtocolModule;\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"DiscoveryProtocol\" /> class.\n    /// </summary>\n    public DiscoveryProtocolCapture()\n    {\n        using var stream = Assembly.GetExecutingAssembly()\n            .GetManifestResourceStream(\"NETworkManager.Models.Resources.PSDiscoveryProtocol.psm1\");\n\n        using StreamReader reader =\n            new(stream ?? throw new InvalidOperationException(\"Could not load PSDiscoveryProtocol.psm1\"));\n\n        _psDiscoveryProtocolModule = reader.ReadToEnd();\n    }\n\n    /// <summary>\n    ///     Is triggered when a network package with a discovery protocol is received.\n    /// </summary>\n    public event EventHandler<DiscoveryProtocolPackageArgs> PackageReceived;\n\n    /// <summary>\n    ///     Triggers the <see cref=\"PackageReceived\" /> event.\n    /// </summary>\n    /// <param name=\"e\">Passes <see cref=\"DiscoveryProtocolPackageArgs\" /> to the event.</param>\n    protected virtual void OnPackageReceived(DiscoveryProtocolPackageArgs e)\n    {\n        PackageReceived?.Invoke(this, e);\n    }\n","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/BornToBeRoot/NETworkManager/blob/2780d65469917a296dbc15f206107d72e2d0115c/Source/NETworkManager.Models/Network/DiscoveryProtocol.cs#L11-L47","documentation":"The DiscoveryProtocolCapture constructor reads the embedded resource NETworkManager.Models.Resources.PSDiscoveryProtocol.psm1 from the executing assembly. If the manifest resource stream is null (resource missing from the build or assembly), it throws InvalidOperationException('Could not load PSDiscoveryProtocol.psm1'), preventing any LLDP/CDP capture from starting.","triggerScenarios":"Constructing DiscoveryProtocolCapture when the assembly was built without the embedded .psm1 resource (wrong project/RESOURCE include), or loading the assembly from a modified/incomplete deployment (e.g. trimmed publish or a mismatched NETworkManager.Models.dll).","commonSituations":"Custom builds where Resources\\PSDiscoveryProtocol.psm1 was not embedded (EmbeddedResource missing in csproj); publishing with trimming that stripped resources; running a partially copied portable deployment.","solutions":["Rebuild NETworkManager.Models ensuring PSDiscoveryProtocol.psm1 is included as EmbeddedResource in the csproj.","Verify the resource exists: Assembly.GetExecutingAssembly().GetManifestResourceNames() should contain the full name.","Redeploy the complete application (no trimmed/partial assemblies).","Catch the exception in the caller and disable the LLDP/CDP feature with a clear message."],"exampleFix":"// before\nvar capture = new DiscoveryProtocolCapture(); // throws if resource missing\n// after\ntry\n{\n    var capture = new DiscoveryProtocolCapture();\n}\ncatch (InvalidOperationException ex)\n{\n    Log.Error(\"LLDP/CDP capture unavailable: embedded PowerShell module missing\", ex);\n}","handlingStrategy":"fallback","validationCode":"// verify the embedded module is present before constructing\nvar names = typeof(DiscoveryProtocolCapture).Assembly.GetManifestResourceNames();\nbool ok = names.Contains(\"NETworkManager.Models.Resources.PSDiscoveryProtocol.psm1\");","typeGuard":"null","tryCatchPattern":"try\n{\n    var capture = new DiscoveryProtocolCapture();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"PSDiscoveryProtocol.psm1\"))\n{\n    Log.Error(\"Embedded discovery protocol module missing; LLDP/CDP disabled\", ex);\n    // disable the feature in the UI instead of crashing\n}","preventionTips":["Confirm the csproj marks PSDiscoveryProtocol.psm1 as EmbeddedResource.","Smoke-test GetManifestResourceNames() in a build/CI step.","Avoid trimming/partial deployments of NETworkManager.Models.dll."],"tags":["powershell","embedded-resource","assembly"],"backgroundTag":"missing-dependency","analyzedSha":"2780d65469917a296dbc15f206107d72e2d0115c","analyzedAt":"2026-09-12T17:00:17.986Z","contentChangedAt":"2026-09-12T17:00:17.986Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}