Retrieve version of Nuget package used in console application - c#

I hope everyone is safe and healthy.
I am new to dot net core application and I have a question regarding command line parser.
I am using a NugetCommand line parser package created by a friend in my console application. This Command line parser has a version attribute which displays the version of the system.I want to use this version attribute to display the version of my console application. For example when I type -v or --version it displays the version of the console application. I do not know how I can use the version attribute from the nuget package.
Any leads or suggestions?
This is my interface :
public interface MyInterface1
{
Version Version { get; }
int Id get; }
string Name { get; }
}
This interface is wrapped in a self-created Nuget package for CommandLine Parser called myspace.commandlineparser.commands
I have installed this nuget package in my project.
So basically the interface is part of the Nuget package.

Related

Does SevenZipSharp work with .NETCore on Windows?

I have a .NETCore app which I am trying to add 7 zip functionality to.
Compiling gives this warning:
warning NU1701: Package 'SevenZipSharp 0.64.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework 'net5.0'. This package may not be fully compatible with your project.
So I presume the project is .NETCore v5.0. Can I run SevenZipSharp in this project?
Running the app gives an error at the call to CompressFiles: SevenZip.SevenZipLibraryException: 'Can not load 7-zip library or internal COM error! Message: failed to load library.'
public void ZipQOB(string sevenZipDllPath, string zippedQobPath, string unzippedQobFiles)//List<string> sourceFiles)
{
// throw exception if paths passed in are null, does 7zipsharp throw exceptions in this case?
try
{
if (System.IO.File.Exists(sevenZipDllPath) && System.IO.Directory.Exists(zippedQobPath))// && System.IO.Directory.Exists(unzippedQOBFiles))
{
string path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "7z.dll");
//SevenZipCompressor.SetLibraryPath(sevenZipDllPath);
SevenZipCompressor.SetLibraryPath(path);
SevenZipCompressor sevenZipCompressor = new()
{
CompressionLevel = SevenZip.CompressionLevel.Ultra,
CompressionMethod = CompressionMethod.Lzma
};
string[] files = System.IO.Directory.GetFiles(unzippedQobFiles);
sevenZipCompressor.CompressFiles(zippedQobPath + #"\zip.QOB", files);
//System.IO.Path.ChangeExtension(zippedQobPath, ".QOB");
}
This question How do I use 7zip in a .NET Core app running on Linux? mentions a CLI wrapper ported from .NET Framework to .NET Core, but I can't find any details - is this something I would have to write and how?
I have already tried things suggested elsewhere, I altered the project build setting to:
Platform Target = AnyCPU,
ticked Prefer 32-bit
Should I just look at a different option as this page seems lists some stating .netcore compatible: https://github.com/topics/7zip?l=c%23
Many thanks for any help :)

'AnsiConsole' does not contain a definition for 'Live'

I am using Spectre.Console extension for C# Console Application. And I wanted to use Live-Display module of Spectre.Console but I encoutered following problem:
'AnsiConsole' does not contain a definition for 'Live'
Here is my code, or better say, part of it which contains problem (... are parts which aren't required in sample, so during testing/debugging/modification remove them)
using System;
using Spectre.Console;
...
namespace ConsoleApp2
{
class Program
{
...
static void Main(...)
{
AnsiConsole.Live(Clock())
.Start(ctx =>
{
ctx.Refresh();
Thread.Sleep(1000);
});
...
}
...
public static void Clock()
{
Console.WriteLine(DateTime.Now.ToString());
}
...
}
...
}
Additional information
I installed extension using NuGet package manager with following command: PM> Install-Package Spectre.Console, not with dotnet add package Spectre.Console as indicated in docs, but latter option was unavailable for some reason so I used first.
I am using Visual Studio 2019 Community Edition
I am using .NET Framework 4.7.2
Ok, so thanks to #JonSkeet's and #yaakov's comments, I tried installing latest pre-release version 0.39.1-preview-0.31 instead of latest stable version 0.39 and it worked.
Looks like problem was with versions.

Can't locate installed package (type or namespace could not be found)

I'm trying to install a NuGet package and the installation seems to be going fine:
# D:\Dev\PGF_Test
$ dotnet add package PGFSharp --version 3.8.1-devel
Determining projects to restore...
Writing C:\Users\...\AppData\Local\Temp\tmp71D3.tmp
info : Adding PackageReference for package 'PGFSharp' into project 'D:\Dev\PGF_Test\PGF_Test.csproj'.
info : Restoring packages for D:\Dev\PGF_Test\PGF_Test.csproj...
info : Package 'PGFSharp' is compatible with all the specified frameworks in project 'D:\Dev\PGF_Test\PGF_Test.csproj'.
info : PackageReference for package 'PGFSharp' version '3.8.1-devel' added to file 'D:\Dev\PGF_Test\PGF_Test.csproj'.
info : Committing restore...
info : Generating MSBuild file D:\Dev\PGF_Test\obj\PGF_Test.csproj.nuget.g.targets.
info : Writing assets file to disk. Path: D:\Dev\PGF_Test\obj\project.assets.json
log : Restored D:\Dev\PGF_Test\PGF_Test.csproj (in 227 ms).
but when I write the using and dotnet build, I get a namespace could not be found. I've tried removing and re-adding, I've tried different names like PGF and PGFSharp.PGF, and I've tried making a fresh console application and adding it there to make sure nothing's interfering.
Anyone know what the issue might be?
I'm on .NET core on Windows 10, version 5.0.201. I checked the Github for the package I'm importing and it seems to be built in 4.5.1, could that be the issue? If so I might try recompiling the source locally.

Is it possible to install a local version of a package at runtime and use said local package?

As part of trying to test a NuGet package with a Console application, is it possible to have the console application get the latest version of said NuGet package programmatically, and then use said package to call various methods and what not? This is the flow that I'm trying to achieve...
A NuGet package is created locally (already done as part of a build process)
A console application installs this package
The same console application calls a few methods that are inside this package (if this uses reflection insdie the console app to achieve this it isn't an issue)
How can this be achieved? I've tried the following code using the NuGet.Core & NuGet.Protocol NuGet packages...
// exception thrown for trying to resolving newtonsoft
IPackageRepository packageRepository = new NuGet.LocalPackageRepository(directoryPath);
PackageManager pm = new PackageManager(packageRepository, GetExecutingAssemblyDirectory());
pm.InstallPackage("package_id", SemanticVersion.Parse("package_version"));
NuGet.Common.ILogger logger = new Logger();
IEnumerable<LocalPackageInfo> packageInfos = LocalFolderUtility.GetPackagesV2(directoryPath, logger);
foreach (LocalPackageInfo lpi in packageInfos)
{
// no obvious way to actually install the package
}
// never more than zero packages
var localRepo = new LocalPackageRepository(directoryPath);
var packages = localRepo.GetPackages();
if (packages?.Count() > 0)
{
var packageManager = new PackageManager(localRepo, GetExecutingAssemblyDirectory());
packageManager.InstallPackage(packages.ElementAt(0).Id);
}
I haven't been able to get any of those pieces of code to work. Is this actually possible? Would I have to look at using 2 Console apps (one to install into the other, which then does the calling of methods), and if so how would this be done?

how to create a package in swagger code gen tool in addition to the api and model package

I am learning to use swagger code gen to generate a bunch of csharp files from swagger.json file.
In the CSharpClientCodeGen.class , I noticed there are variables for apiPackage and modelPackage that generates the Api and Model cs files in them. I would like to create a new package similar to apiPackage and modelPackage.
I tried adding in the following code
public class csharpcodegen extends CSharpClientCodeGen
{
protected String newPackage="IO.Swagger.New";
}
And then built the client code, but I dont see the new package folder in there.
Not sure how to do this. It would be great if you can point me to resources on how to do this.
Thanks
If you want to customize the package name, you can pass a config file (e.g. config.json) to the swagger-codegen. Here is a full list of configuration option available for C# code generator:
swagger-codegen|master⚡ ⇒ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar config-help -l csharp
CONFIG OPTIONS
packageName
C# package name (convention: Camel.Case), default: IO.Swagger
packageVersion
C# package version, default: 1.0.0
For example, to generate C# SDK, prepare the config file as follows
{
"packageName": "Com.RestUnited",
}
Then run the following command:
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate \
-i /var/tmp/swagger_spec.yaml \
-l csharp \
-o /var/tmp/csharp/test/ \
-c /var/tmp/csharp.json
To request for new option on customizing the C# SDK, please submit via the Github page

Categories

Resources