Using Z3 with ASP.NET Core - c#

Can the Microsoft Z3 .NET API handle .NET Core? We're using it in a scheduling algorithm for a school project, and we believe when the project was upgraded to .net core, z3 stopped working. We can't find any information on z3 being used with .net core.

Z3 uses code contracts, which are not available in .NET core. However, we have a dummy class that replaces them, and which comes with the source code, see src/api/dotnet/core/DummyContracts.cs.
At the moment, this is not tied into the rest of our build infrastructure, but you can build them thusly:
cd src/api/dotnet/core
dotnet restore
dotnet build
(Make sure you update your copy of the source code as I just committed a fix for the Core build.)

For Z3 to work in a .Net Core 2 project you need the following things:
The Microsoft.Z3.dll in your project and add a reference to it in the project. Place it in the project root if unsure.
The libz3.dll as well but this doesn't need to be referenced (won't work anyways).
Add the libz3.dll and z3.exe to your PATH, either through code or by your OS. (this part is the one often resulting in dll not found errors).
** My code in C#
// Convinient metod to decide OS. false => linux in this case.
public static bool IsWindows() =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
private static void AddZ3ToProcessPath()
{
// We store our OS-dependent Z3 DLLs in diffrent folders in our root.
var solverZ3Path = IsWindows() ? "z3winx64_485" : "z3linuxx64_485";
var z3X64BinariesPath = "";
if (IsWindows())
{
z3X64BinariesPath = $"{solverZ3Path}"; // Windows friendly
}
else
{
z3X64BinariesPath =$"/{solverZ3Path}"; // Unix friendly
}
var path = Uri.UnescapeDataString(z3X64BinariesPath); // Escape chars
var name = "PATH"; // Add dlls to this environment variable
var target = EnvironmentVariableTarget.Process; // But only for this process and not entire machine or user
Environment.SetEnvironmentVariable(name, path, target);
}
What OS are you using, what version of .Net Core? Any link to code?

Related

How to add pending changes or checkout files in TFVC programmatically (netstandard2.0)

The package we used before (Microsoft.TeamFoundationServer.ExtendedClient), doesn't support netstandard2.0 and it seems that the new REST APIs doesn't support the tasks we want to accomplish either.
So what's the recommended way to add new files as pending changes or checkout existing files in tfvc when targeting netstandard2.0?
TF.exe should be capable of doing the aforementioned tasks, but to get the path to it we would somehow need vsWhere.exe and so that whole approach seems a little bit cumbersome.
There is no, and won't be, a .NET Core compatible version of the TFVC client object model. The API used to interact with TFVC is a SOAP based API and it has never been ported to REST.
You can use vswhere to find tf.exe or build a custom .NET 4.x executable using the Client Object Model and call from your .NET Core app.
You're allowed to redistribute a copy of vswhere with your application. You can grab the latest version from here:
- powershell: |
$vswhereLatest = "https://github.com/Microsoft/vswhere/releases/latest/download/vswhere.exe"
$vswherePath = "$(Build.SourcesDirectory)\tools\vswhere.exe"
remove-item $vswherePath
invoke-webrequest $vswhereLatest -OutFile $vswherePath
test-path $vswherePath -PathType Leaf
displayName: 'Grab the latest version of vswhere.exe'
As you can see, I run this in my Azure Pipeline to inject the latest version into my build directory prior to packaging my app.
And this is the code I use do find tf.exe afterwards:
$path = & $PSScriptRoot/vswhere.exe -latest -products * `
-requires Microsoft.VisualStudio.TeamExplorer `
-find "Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\TF.exe"

Trying to call x86 .Net 3.5 SDK in more recent .Net versions

This may be a long shot... I need to create a simple-ish web app that needs to call an OEM supplied SDK that is 10-15, or more, years old (no updates are possible). The SDK is 32bit and is pegged to .Net Framework 3.5. There is still an active NDA for using the SDK so there is not much that I can share.
I have access to Visual Studio 2015, 2017 and 2019. In each version I can go through the process of creating a C# web app and I can select .Net 3.5 as the target framework but the only available template is a "Blank" one. Are there MVC based templates for 3.5? I have googled and have not found any.
That is my first problem. I decided to just move on and try newer .Net versions that might have a more complete template set. I have tried 4.8 most recently.
Using .Net 4.8 and specifying an "x86" platform I can create a simple app. I can call the SDK lib and get values from the simple library "get" methods like "getHeight", "getWidth", etc. The SDK is for an old computer controlled device that still works like a charm. It has a camera and one of the available functions is GetCameraImage.
The SDK comes with documentation and some sample C# "SLN"s. One of those has the following sample code:
var idata = new byte[numOfBytes];
unsafe {
fixed (byte* fixedPtr = idata) {
channel.GetCameraImage((int) numOfBytes, out idata[0]);
}
}
In my code the above just crashes with an "Access Violation". No exceptions are thrown in the debugger. It just dies.
The docs that come with the SDK suggest that the following is valid:
var idata = new byte[numOfBytes];
channel.GetCameraImage((int) numOfBytes, out idata[0]);
but I get the same Access Violation.
Any ideas about the Access Violation?
EDIT
#Serg: Addressing a couple of comments: var idata = new byte[numOfBytes*3] did not work but was a great idea.
#DekuDesu: I am not sure how to exactly verify that the memory is allocated so I did the following:
var idata = new byte[numOfBytes];
for (var i=0;i<numOfBytes;i++) {
idata[1] += 0;
}
channel.GetCameraImage((int) numOfBytes, out idata[0]);
I also tried this with #Serg's offset idea and the violation is definately triggered by the GetCameraImage call.

How to find type of project with cake build system?

ITNOA
I use cake build system to automate building my solution and I want to automate testing my project with cake in our CI,
My NUnit projects have many types such as asp.net dot net framework 4.6.1 or dot net core 2.1 or ...
my problem is when I want to run our unit test with NUnit3 cake dsl we need to know dll path of each project, so I need to know what is type of each project, because for example for dot net core 2.1 dll is under netcoreapp directory and for another project dll is under somewhere else.
for better demonstration please see below image
As you can see in Test Solution Folder we have many test projects with different type framework and ... (for example one of them is dotnetcore2.1 another one is dot net framework 4.6.1 and etc.)
So my question is how to find type of project?
Is there any solution to NUnit3 found dll by self?
my GitHub discussion about this question
thanks
You should use a project reference for your unit test projects as opposed to an explicit DLL reference.
When you're using .Net Core, Use XUnit testing for allow for near identical process for your test methods.
By using a project reference and having your test projects as part of the same Solution (sln) file, you'll greatly simplify your work.
It looks like what you are trying to do is to get a list of all the assemblies for your unit tests, across any targets that the project might have.
If that's the case, you could use the GetFiles alias to find the assemblies, and then inspect the FullPath of each assembly to check the TFM...
var files = GetFiles("./**/*.Tests.dll");
foreach(var file in files)
{
Information("File: {0}", file.FullPath);
if (file.FullPath.Contains("/net461/")
{
// ... Assembly is for .NET 4.6.1
}
else if (file.FullPath.Contains("/netcoreapp2.1/")
{
// ... Assembly is for .NET Core 2.1
}
// etc...
}
You could utilize a globber pattern to find all test projects, don't know you're exakt folder structure but you could do something like below
Task("Test")
.Does(() =>
{
var settings = new DotNetCoreTestSettings
{
Configuration = "Release"
};
var projectFiles = GetFiles("./**/Test/UnitTests/*.csproj");
foreach(var file in projectFiles)
{
DotNetCoreTest(file.FullPath, settings);
}
});

Programmatically Set VS Installer Project Version on Build Action

I have a C# WPF application, which is written in Visual Studio 2017 (15.9.9) using .Net 4.7. The solution for the application contains two projects - the WPF Project itself and also a Microsoft VS Installer Project that creates a .msi file on the build action.
I have stored the version information for the application project and can return it using the following method
public static string GetVersion()
{
try
{
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
string version = fvi.FileVersion;
return $"Version {version}";
}
catch(Exception ex)
{
//send error notif to IT dept.
Alert_IT(ex, GetCurrentMethod(), false);
return "0";
}
}
I would like to use the result of this to set the the version information in the Setup project when I build the solution - at the moment it has to be manually entered into the properties of the setup project.
Since we are planning on using setup projects for all our applications, it would be really nice to only have to alter the version in one place for all our projects, ensuring that there are no inconsistencies.
Is it possible to set the version on a setup project programmatically? It would have to be done before the .msi is created.
If this is not possible, do you have any other suggestions as to how to achieve the desired outcome - that the version information for the application need only be set in one place before it is built?
I have seen the advice here, but did not find it specific enough to be helpful.

Get the current dot net version of my application

How do I get the running dot net version of my asp.net application.
I tried the solution from here
Is there an easy way to check the .NET Framework version?
It gives the highest version installed but I need the running version.
Use Environment.Version for getting the run time version. It will give the version number of .Net CLR which is being used for executing current application.
You need to be careful here, it will only return run time version not framework version. The CLR for .NET 3.0 and .NET 3.5 is the same CLR from .NET 2.0.
Use Environment.Version - it gives you the exact version of .NET running the application.
Hope this one helps,
DirectoryEntry site = new DirectoryEntry(#"IIS://localhost/w3svc/1/Root");
PropertyValueCollection values = site.Properties["ScriptMaps"];
foreach (string val in values)
{
if (val.StartsWith(".aspx"))
{
string version = val.Substring(val.IndexOf("Framework") + 10, 9);
MessageBox.Show(String.Format("ASP.Net Version is {0}", version));
}
}
The script map property is an array of strings. If the app supports asp.net one of those strings will be a mapping of the aspx file extension to the asp.net handler which will a the full path to a DLL. The path will be something like
%windir%/Microsoft.NET/Framework//aspnet_isapi.dll.
You can get the version out of this string with some simple parsing.

Categories