Receive Test Run start/finish with DTE2 interface in Visual Studio extension - c#

is there a way to subscribe to Test Explorer events in visual studio extension?
I didn't find anything like that in DTE2 interface. My goal is to trigger some function from extension when Test run completed (for the test that were ran from Test Explorer)
Thank you!

Thanks 280Z28 for your answer. Working code by using application object DTE:
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.TestWindow.Extensibility;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.TestTools.Execution;
public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
Microsoft.VisualStudio.OLE.Interop.IServiceProvider InteropServiceProvider = application as Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
_ServiceProvider = new ServiceProvider(InteropServiceProvider);
_ComponentModel = (IComponentModel)_ServiceProvider.GetService(typeof(SComponentModel));
_OperationState = _ComponentModel.GetService<IOperationState>();
_OperationState.StateChanged += _OperationState_StateChanged;
}
void _OperationState_StateChanged(object sender, OperationStateChangedEventArgs e)
{
}
It is also possible to access currently discovered test by ITestsService.
_TestsService = _ComponentModel.GetService<Microsoft.VisualStudio.TestWindow.Extensibility.ITestsService>();
var GetTestTask = _TestsService.GetTests();
GetTestTask.ContinueWith(Task =>
{
var DiscoveredTests = Task.Results.ToList();
});

The interfaces you need are available through MEF in the Microsoft.VisualStudio.TestWindow.Interfaces.dll assembly.
You need to expose your extension through MEF and [Import] an instance of IOperationState, or use the IComponentModel interface (returned for the SComponentModel service) to access the IOperationState. From there, you want to add an event handler to the IOperationState.StateChanged event, and look for the State property to include the TestOperationStates.TestExecutionFinished flag.
I'm terribly sorry for the lack of links, but I couldn't find any information about this in MSDN.
Edit: Two remarks about compatibility.
This is only available in Visual Studio 2012 and newer.
The necessary assembly (mentioned above) has a different strong name in the two versions of Visual Studio, and there is no bindingRedirect in Visual Studio 2013. What this means is you will be forced to deploy separate extensions for Visual Studio 2012 and Visual Studio 2013, or get "clever" about the way you dynamically load your extension code (the latter is way beyond the scope of this answer, but I've used it for some cases like Inheritance Margin extension that requires access to version-specific IntelliSense resources).
Sample VS 2017
A sample using MEF and the ITestContainerDiscoverer exported type. But be aware this may be gone in VS 2019!
[Export(typeof(ITestContainerDiscoverer))]
[Export(typeof(Testything))]
internal class Testything : ITestContainerDiscoverer
{
[ImportingConstructor]
internal Testything([Import(typeof(IOperationState))]IOperationState operationState)
{
operationState.StateChanged += OperationState_StateChanged;
}
public Uri ExecutorUri => new Uri("executor://PrestoCoverageExecutor/v1");
public IEnumerable<ITestContainer> TestContainers
{
get
{
return new ITestContainer[0].AsEnumerable();
}
}
public event EventHandler TestContainersUpdated;
private void OperationState_StateChanged(object sender, OperationStateChangedEventArgs e)
{
if (e.State == TestOperationStates.TestExecutionFinished)
{
var s = e.Operation;
}
}
}
Some more things could be found here
https://www.fuget.org/packages/Microsoft.VisualStudio.TestWindow.Interfaces/

Related

Custom addon not displayed in the addons menu in G1ANT studio

I am trying to create a new addon but the addon is not being displayed in the addons menu in G1ANT Studio. Even other addons installed from the marketplace are also not displayed. I am using the latest version. I have tried running G1ANT studio as administrator. Yet it makes no difference.
Here is the Addon.cs file of my addon:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using G1ANT.Language;
// Please remember to refresh G1ANT.Language.dll in references
namespace G1ANT.Addon.LibreOffice
{
[Addon(Name = "libreoffice", Tooltip = "Provides commands to automate LibreOffice")]
[Copyright(Author = "G1ANT LTD", Copyright = "G1ANT LTD", Email = "support#g1ant.com", Website = "www.g1ant.com")]
[License(Type = "LGPL", ResourceName = "License.txt")]
[CommandGroup(Name = "calc", Tooltip = "Commands connected with creating editing and generally working on calc")]
public class LibreOfficeAddon : Language.Addon
{
public override void Check()
{
base.Check();
// Check integrity of your Addon
// Throw exception if this Addon needs something that doesn't exists
}
public override void LoadDlls()
{
base.LoadDlls();
// All dlls embeded in resources will be loaded automatically,
// but you can load here some additional dlls:
// Assembly.Load("...")
}
public override void Initialize()
{
base.Initialize();
// Insert some code here to initialize Addon's objects
}
public override void Dispose()
{
base.Dispose();
// Insert some code here which will dispose all unnecessary objects when this Addon will be unloaded
}
}
}
The addon also references some other DLLs as dependencies.
There are no errors in your code. Have you ever compiled the HelloWorld example from this tutorial? https://github.com/G1ANT-Robot/G1ANT.Addon.Tutorials/tree/master/G1ANT.Addon.Command.HelloWorld
Remember
1. All dlls in the solution should be marked as "Resource" and will be embeded into your addon
2. The target .NET Framework of your project should be 4.6.1
I figured out what the issue was. The G1ANT.Language.dll was in the same directory as the addons, it seems to have been causing the issue.

Adding Windows Azure Caching crashes Visual Studio 2012 with datacachefactory argument null exception

I've added the Windows Azure Cache 1.8.0 nuget package to my solution, but it ends up crashing Visual studio when I load the project. I've found that I can "prevent" the crashing by removing the dlls from the bin folder, and then again when visual studio adds them back to the bin while the project loads.
The dlls I'm removing are:
Microsoft.ApplicationServer.Caching.AzureClientHelper.dll
Microsoft.ApplicationServer.Caching.AzureCommon.dll
Microsoft.ApplicationServer.Caching.Client.dll
Microsoft.ApplicationServer.Caching.Core.dll
When I look at the event viewer for the visual studio crash I get this:
Application: devenv.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentNullException
Stack:
at System.Threading.Monitor.Enter(System.Object)
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Close()
at Microsoft.ApplicationServer.Caching.DataCacheFactory.Finalize()
I'm uncertain why VS is doing things with the dlls while the project is loading, but I admit I'm not an expert on that.
I've basically followed the process described on this page to add a dedicated cache worker role for caching:
http://www.windowsazure.com/en-us/develop/net/how-to-guides/cache/
I've tried removing and reinstalling the package, removing and reinstalling the Visual Studio SDK (Oct 2012), but the problem comes back.
Also, I don't have the App Fabric Server installed.
Thanks in advance for your help!
In case anyone else runs into this problem, I'm providing what seemed to work for me here.
In order to get visual studio to load the project, I removed the DLLs from the project. I also removed them as the project was loading and VS put the dlls back in the bin folder.
I removed the references to the dlls. Then I removed my code that was using the datacachefactory.
In the end I believe that it was caused by an improper use of the cache in my code that I had performed a build on. I was able to correct the usage of it, build the solution and get all the dlls back into the project.
Previously by datacache object had not be static.
here's my correct usage of the datacache factory:
using System;
using Microsoft.ApplicationServer.Caching;
namespace WebRole1.Classes
{
public class AzureCache
{
public static DataCache cache { get; set; }
public static DataCacheFactory dataCacheFactory { get; set; }
public AzureCache()
{
if (cache == null){
DataCacheFactoryConfiguration cfg = new DataCacheFactoryConfiguration();
cfg.AutoDiscoverProperty = new DataCacheAutoDiscoverProperty(true, "CacheWorkerRole1");
dataCacheFactory = new DataCacheFactory(cfg);
cache = dataCacheFactory.GetDefaultCache();
}
}
public void Add(string item, object value)
{
cache.Add(item, value);
}
public void Add(string item, object value, TimeSpan timeout)
{
cache.Put(item, value, timeout);
}
public object Get(string item)
{
return cache.Get(item);
}
public TimeSpan TimeRemaining (string item)
{
DataCacheItem DCitem = cache.GetCacheItem(item);
return DCitem.Timeout;
}
public void Flush()
{
cache.Clear();
DataCacheFactory cacheFactory = new DataCacheFactory();
cache = cacheFactory.GetDefaultCache();
}
}
}
We may need to capture a dump of the visual studio process(devenv.exe) to see what is causing this exception. You can use debugdiag("http://www.microsoft.com/en-us/download/details.aspx?id=26798") to capture the dump.
You may need to involve Microsoft support services("http://www.windowsazure.com/en-us/support/contact/") to further investigate this since the exception is coming from the cache code itself.
It's not an issue exclusively related to Visual Studio. I get it while running my webrole.
Check out this other article.

Obtaining Solution Name in ToolWindow of VSpackage

I'm new to Visual Studio Extensibility Framework to use VSPackage Extension. I want to obtain a DTE Object inside the user-control which is called inside MyToolWindow class.
I tried all the below possibilities:
1.EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
string solutionPath=dte2.Solution.FullName;
The above trial gives only SolutionPath of the 1st instance of the visual studio which is opened.
For EX: If Math1.sln and Math2.sln is opened,only Math1.sln path is assigned to solutionPath.
From the MSDN link, "this.GetService" cannot be recognized in the button1.click (Am I missing any thing here)
Once, DTE object is obtained I shall be easily be able to obtain solution Name of the current instance.
I must be able to obtain appropriate EnvDTE80.DTE object inside the click of the button which is inside User-control. This User-Control is called inside MyToolWindow.cs
Any help on this would be greatly appreciated.
First of all, Marshal.GetActiveObject("VisualStudio.DTE.10.0") is problematic because it will just give you a DTE of a random Visual Studio instance. You can obtain correct instance of DTE through the GetService call.
Then just enumerate the DTE.Solution.Properties, one of the properties should be the solution name. Probably DTE.Solution.Properties.Item("Name"), though I cannot check this at the moment.
As for your "GetService cannot be recognized", method is defined on an IServiceProvider which ToolWindowPane implements. If you want to use that in your control, you should pass the tool window instance to your control. Example:
public class MyToolWindow: ToolWindowPane {
void SomeMethod() {
var myControl = new MyControl(this);
}
}
public class MyControl: UserControl {
public MyControl(IServiceProvider serviceProvider) {
// Now you can call serviceProvider.GetService
}
}
I have an abstract VsPackage class I use within all my home-brewed extensions; and I obtain the application instance this way...
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
public abstract class VsPackage : Package
{
...
protected DTE2 GetApplication()
{
return this.GetService(typeof(SDTE)) as DTE2;
}
...
}

Where does OnAfterInstall event go?

I've had serious problems on how to solve this: I don't know where the OnAfterInstall event goes.
Let me explain myself. I created a C# project which compiles perfectly and built in Release mode. After that, I've created a Setup Project using the wizard. I have added an extra dialog, which lets the user choose between two languages. Now, my problem is that I want to store that language into the registry (or app.config file, the easier the better), and I've read that you need to detect it within the OnAfterInstall method in an inherited class of Installer.
Now, where should I put that class? Logic tells me it goes in the C# project, but it complains that neither Context nor Installer class exist. When I add this class to the Setup Project, it doesn't complain, but it doesn't work after that. Here's the class.
using System;
using System.Configuration.Install;
public class Install : Installer
{
public Install()
{
}
protected override void OnAfterInstall(IDictionary savedState)
{
string lang = Context.Parameters["lang"];
RegistryKey key = Registry.LocalMachine;
using (key = key.CreateSubKey(#"SOFTWARE\MyCompany\MyApp"))
{
key.SetValue("lang", lang);
key.Close();
}
base.OnAfterInstall(savedState);
}
}
PS: I'm already passing lang as CustomActionData using /lang=[LANG] (where LANG is the radio value)
First, you should add the RunInstallerAttribute to you class.
[RunInstaller(true)]
public class Install : Installer
...
Next, put the installer in a separate project (class library), e.g. MyCustomInstaller.
Finally, add the primary output of this project to a custom action in the custom actions editor of the setup project.
It's up to you in which custom action you want to use.

ScriptSharp ClockLabel example with 0.6.2

I'm developing in Visual Studio 2010 and I've just downloaded and installed Script# 0.6.2 for VS 2010. I'm trying to follow the clock example in the Read Me pdf but can't get it to compile.
I've created a new Script# Class Library project inside my solution called Clock, renamed the .cs file to ClockBehaviour and added the following code as per the example:
using System;
using System.DHTML;
using ScriptFX;
using ScriptFX.UI;
namespace Clock {
public class ClockBehavior : Behavior {
private int _intervalCookie;
public ClockBehavior(DOMElement domElement, string id) : base(domElement, id) {
_intervalCookie = Window.SetInterval(OnTimer, 1000);
}
public override void Dispose() {
if (_intervalCookie != 0) {
Window.ClearInterval(_intervalCookie);
} base.Dispose();
} private void OnTimer() { DateTime dateTime = new DateTime(); DOMElement.InnerHTML = dateTime.Format("T"); }
}
}
When I try and compile the project I get errors saying that the System.DHMTL, ScriptFX and ScriptFX.UI namespaces could not be found (and some others, but I guess by fixing these errors the others will fall out).
It feels like I'm not referencing the correct projects/dlls. In the References for the project I have mscorlib and Script.Web. I've tried using the object browser find the classes (such as Behavior) in other namespaces but with no luck. I've added all of the .dlls from the ScriptSharp folder in Program Files but the namespaces still can't be found.
Any help would be very much appreciated,
Thanks,
Hugh
the sample docs are a bit out of date - look at the phot sample in the samples download : http://projects.nikhilk.net/Content/Projects/ScriptSharp/Sample.zip
See http://projects.nikhilk.net/ScriptSharp/Conceptual-What
You need to reference ssfx.Core.dll which should be installed with Script#
(Alternatively, see pp 23-24 of the pdf you linked...)

Categories

Resources