How do I enable C# scripting in VS 2015? - c#

How do I configure VS 2015 to enable Roslyn's C# scripting capabilities?
I've tried installing various Nuget packages, including both the 1.0 and 1.1.0-beta1 versions of Microsoft.CodeAnalysis.CSharp, Microsoft.CodeAnalysis.Scripting.CSharp, etc., but I can't get any of the examples I've found online to work. I am getting "type not found" errors, i.e.,
var scriptEngine = new ScriptEngine();
... fails because the type "ScriptEngine" is not found.
Can someone provide as recipe that includes which nuget packages to install, which using statements are required, etc., to implement Roslyn scripting?
UPDATE #1:
I have made some progress, but still having issues. I get a bunch of compiler warnings and then a TypeInitilizationException which is apparently due to a component version mismatch.
I'm now using the following example code (taken from a test), and there's no missing types:
using System;
using Microsoft.CodeAnalysis.Scripting.CSharp;
namespace RoslynScriptingTest {
class Program {
static void Main(string[] args) {
var script = CSharpScript.Create("1 + 2");
var fn = script.CreateDelegate();
var value = fn();
Console.WriteLine("value={0}", value.ToString());
}
}
}
I've loaded all of the nightly packages that are available at https://www.myget.org/F/roslyn-nightly/.
I get a series of build warnings that refer to Microsoft.CodeAnalysis, v1.1.0.0.
Running the exe despite the warnings yields the TypeInitilizationException mentioned above. Based on the stacktrace, the TypeInitializationError is caused by a version mismatch for System.Reflection.Metadata.dll.
I am not sure where to go from here. I don't understand how the scripting-related packages/components fit together. I saw some posts from earlier this year that describe building Roslyn completely. I have not done that. Is that necessary?
This is reminding me of DLL hell from the old days.

The scripting APIs are still in progress, and were removed from the release packages.
Try the nightlies instead.

With visual studio 2015 update1 the REPL is back and scripting api's are enabled.
Here is what Microsoft says about it:
In this release, the C# Interactive Window is back in Visual Studio, as well as the command-line C# REPL window. (The Interactive Window is the REPL window inside Visual Studio.)
We've also released scripting APIs that enable you to build and run C# as a script. The scripting APIs are available on GitHub.
Additionally, we've released csi.exe, which is a tool that you can use to run a C# script file (.csx) from the Developer Command Prompt. For example, simply type csi myScript.csx to run your script file. Or, you can enter the command-line REPL mode to interactively evaluate snippets of C# code. To get to this mode, run the command csi without any arguments from the Developer Command Prompt.
Reference:
https://www.visualstudio.com/news/vs2015-update1-vs#Csharp

Related

Why does Visual Studio accept PriorityQueue?

I'm writing a program that needs a priority queue, and it would be nice if I could use the one that comes with .Net 6: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue-2?view=net-6.0
So I've tried writing some sample code (in F#, but this particular code fragment could be written just the same in C# and I would expect the results to be the same):
open System.Collections.Generic
...
let pq = PriorityQueue()
pq.Enqueue("apple", 5)
pq.Enqueue("banana", 6)
checkEq (pq.Dequeue()) "apple"
checkEq (pq.Dequeue()) "banana"
Visual Studio is perfectly happy with that, autocomplete worked and everything.
I try to compile it, and fsc is not at all happy:
C:\olivine\src\test.fs(1345,14): error FS0039: The value or constructor 'PriorityQueue' is not defined.
Maybe that's because I don't have the right version of .Net installed.
reg query "HKLM\SOFTWARE\Microsoft\Net Framework Setup\NDP" /s
Needless to say, there seem to be dozens of versions installed, but the highest version numbers I am seeing, appear to be 4.x, which would explain why fsc is not happy.
But then what's up with Visual Studio? How did it accept and understand the reference to PriorityQueue, despite .Net 6 being apparently not installed on the machine?

Azure function in VSCode gives reference errors

I have set up a local Azure Function project in VSCode as per the instructions here https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-csharp?tabs=in-process
The default templated function that is created works when I run it in VSCode, but I am seeing reference errors in the VSCode editor as per screen shot.
When I go to the definition of the HttpRequest class for example, I see no Query method.
How can I fix these?
The options I selected were C#, .NET 6, HTTP Trigger, Anonymous, Add to Workspace
I have installed .NET 6 SDK, Azure Function Tools, C# extension, Azure function extension.
Thank you
Edit:
The exact message I get when I mouseover one of the reference errors is
The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13 ...
I ran into the same problem today. I have the C# extension installed in VS Code and noticed this line in the extension description:
When I set that flag to true (and restarted OmniSharp or VSCode), the issue went away :)
I tried to reproduce the issue:
Firstly, I have selected my workspace folder from the VSCode,
Created the function project using func init command in the VS Code terminal.
Create the function using func new command in the terminal, selected Http Trigger from the dropdown display of triggers and given the function name as Hello.
Click on the Hello.cs file, then you will get a side dialog box on the bottom of the same VS Code window.
Click on Restore.
OR
If you get two dialog boxes, then click on Restore and Yes so that it will restore the NuGet packages and functions dependencies required for that environment.
You can also see in the output terminal opens immediately after clicking restore option:
After adding those, you will get clean error-free UI like below:
Note:
If you have written the class/function manually in the code, please check that you renamed the Function class name both in the code as well in the file explorer like:
Updated Answer:
As per your edits and comments, I also installed .Net SDK Version 6 and checked the project in VS Code and I got the restore dialog box again with that red squiggly lines. After clicking on restore option, all red squiggly lines went out and this is the Proof-Of-Concept:
I did get this resolved, though I am not sure exactly how/ why, so i'll just post what I did in case it helps someone else.
Firstly I removed the Microsoft C# extension and re-installed it. I dont think this had any effect and it didnt seem to fix the issue, I am just mentioning it as I will mention everything I did.
When I googled the error that is in my Edit above The type 'Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13 I found some references to the MS Build tool/ library shipped with Visual Studio (not VS Code) in regards to .NET 6 and how the .NET 6 requires a certain version of the Build utility.
The recommendation was to upgrade Visual Studio (not VSCode), which I already had installed and that would update the MS Build.
I didnt really understand quite a bit of what I read, but I updated my install of Visual Studio Community 2019 in the hope that it would update MS Build and it seems to have worked.
I expect there is another way to do this without Visual Studio, but I chose this way as I had it installed anyway.
One last thing was I went to bed and had a sleep, then checked it again in the morning.
Cant really say for sure which of the three things worked :)

Use the dynamic keyword/.NET 4.6 feature in Unity

I am trying to implement GraphQL into Unity3D (version 2017.1.0f3 Personal). I am using .NET 4.6 (Experimental), but despite this, Unity does not support dynamic keyword. Which is odd, since .NET 4.0 it is the part of .NET. Except in Unity. I was googling for some solution how to get it work, but no solutions to the dynamic keyword. The error is this:
Severity Code Description Project File Line Suppression State
Error CS1980 Cannot define a class or member that utilizes 'dynamic'
because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you
missing a reference? skiing-prototype (1) D:\skiing-prototype
(1)\Assets\Scripts\GraphQL.cs 62 Active
That is the only caveat of using GraphQL C# client. Has anyone tried it yet to get it work? I hadn't found any greater efforts to get it up and running yet.
EDIT:
I am using this client here: https://github.com/bkniffler/graphql-net-client
Also this is an error from visual studio, but in Unity console it shows errors too, will update what exactly momentarily
Assets/Scripts/GraphQL.cs(80,16): error CS1980: Dynamic keyword requires
`System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you
missing System.Core.dll assembly reference?
this is the unity editor error, which seems to be the same that in visual studio
The first step is to check if Unity recognizes these 2 basic C# 6 features from MS site.
1.Try "Index Initializers" feature:
private Dictionary<int, string> webErrors = new Dictionary<int, string>
{
[404] = "Page not Found",
[302] = "Page moved, but left a forwarding address.",
[500] = "The web server can't come out to play today."
};
2. then "String Interpolation" feature:
private string FirstName = "";
private string LastName = "";
public string FullName => $"{FirstName} {LastName}";
If they give you error then the problem is not just the dynamic keyword but a problem that Visual Studio cannot recognize the .NET version being set by Unity.
From the comment section your Unity failed to compile the first example.
Go through the steps one by one for a possible fix. Do not skip of them.
1.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> Experimental (.Net 4.6 Equivalent).
2.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Api Compatibility Level --> .NET 4.6
3.Restart Unity Editor and Visual Studio. You must restart both.
Test both C# features above. If they work then the dynamic keyword should as-well. If they don't then move on to #4.
4.Update Visual Studio. This is very important. Update the visual Studio to the latest version/patch.
5.If you can't still get both C#6 features above to compile then Re-install both Visual Studio and Unity then perform step #1 and #2 again as some files are missing.
6.Finally, if you get both C#6 features working but the dynamic keyword is still not working then update from Unity 2017.1 to Unity 2017.2. This version fixed many .NET issues.
Note that I am using Unity 2017.2 with the dynamic keyword without any issue. Also, GraphQL is working fine.
I seem to have found a solution
Navigate to Edit > Project Settings > Player > Other Settings > Configuration > API Compatibility Level and change from .NET Standard 2.0 to .NET 4.x
This immediately removed the compiler error and allowed me to run code using the dynamic keyword.
Let me know if that was useful

How to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7?

I am using IE11 in Windows 7.
Then I added a reference in C# project c:\Windows\System32\mshtml.tld and try to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7, but VS2013 doesn't see it.
I can only get mshtml.IHTMLDocument, mshtml.IHTMLDocument2 .. mshtml.IHTMLDocument5.
IHTMLDocument7 interface https://msdn.microsoft.com/ru-ru/library/windows/hardware/ff975572
Select the MSHTML assembly reference, look at its Path property and you'll see the problem:
C:\WINDOWS\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll
This is the PIA for the mshtml type library, installed on any programmer's machine that has VS installed. Version 7.0.3300 is a version number from the .NET 1.0 era, it is 14 years old. Predates IE11 of course, that's why it doesn't have the later interface types. Using the PIA is never the wrong thing to do, it ensures that your program will run with any version of IE, all the way back to IE6.
But that's not what you want, you'll have to generate your own interop library from the IE11 type library. Run the Visual Studio Command Prompt, navigate to your project directory and type this command:
Tlbimp c:\windows\system32\mshtml.tlb /out:Interop.mshtml.dll
It will trundle for quite a while, it is a big one, and spit out several warnings about the PIA being present and several obscure methods and types that cannot be directly used from a .NET program. You can ignore them, they didn't work in the PIA either.
You now have the Interop.mshtml.dll file in your project directory. Back to VS, delete the old MSHTML reference and use Project > Add Reference > Browse to select the new one you just created. Its "Embed Interop Types" property is True by default, no longer a need for a PIA at all and you don't have to deploy the file with your program. Check the file into source control so you only have to do it once.
And be careful, your program can die with an E_NOINTERFACE exception when it runs on machine that doesn't have the required version of IE installed. You can use the as operator to cast the document reference to IHtmlDocument7, it will be null if IE is old.

Howto successfully register certadm.dll in order to be able to use ICertView2 in C# Code

Motivation:
I want be able to retrieve and view Certificates from a Windows CA on a local Machine. I don't want to access the Browser-Keystores of the Machine, but the Windows CA (Windows Certifcate Authority (Service)).
I want to to do this in C#-Code.
My Investigation so far:
I found several Examples that are using the following line:
ICertView2 certView = new CERTADMINLib.CCertView();
...
I think i'm able to use this line and the ICertView2 Structure, i reached my Goal.
If a write this Line into my C#-Code in Visual Studio, it says to me, that it don't know ICertView2 and CERTADMINLib.
So if searched the Web again and found out the i need to import a Reference. I need the COM Library certadmin.dll, that fortunately exists in my C:\Windows\System32 Folder.
So i tried to add the Reference over the Solutionexplorer->Project->References->Add Reference->COM. But it is not listed there, only a similar looking Library called "CertCli 1.0 Type Library". I added this and also was able to type an
using CERTCLIENTLib;
but unfortuneatly, the needed "ICertView2" Class is not in there.
If i type
using CERTADMINLib;
that should be typed in order to be able to use the ICertView2, Visual Studio says to me, that it also don't know "CERTADMINLib".
Further i found hints on the net, that one need to register the certadm.dll beforehand, in order to make it available in Visual Studio. I tried to register the dll-File, but it doesn't work.
If i invoke the following Command:
C:\Windows\System32>regsvr32.exe C:\Windows\System32\certadm.dll
and get a Dialogbox telling me the following:
'Error while loading the Module "C:\Windows\System32\certadm.dll". ... The specified Module could not be found.'
The Version of certadm.dll ist "5.2.3790.3959".
I'm using "Windows 7 Enterpise SP1".
Can you tell me, how i'm able to register and futher make the appropriate Reference available in Visual Studio?
If i've forgotten further Information, please let me know, so i can add them.
Microsoft changed much from XP to Win7. To be able to reference it you will have to tlbimp certadm.dll. Tlbimp.exe can be found in your .NET SDK's and such. Then you will have to import this library in your .NET solution.
Although i have to warn you, i have not mangaged to get any code working in Win7 that works in XP.
You can also look at this link:
http://blogs.msdn.com/b/alejacma/archive/2012/04/04/how-to-get-info-from-client-certificates-issued-by-a-ca-c-vs-2010.aspx

Categories

Resources