Welcome to MSDN Blogs Sign in | Join | Help

How do I retrieve a list of all web services on a machine?

If I have a machine say FOO and I want to retrieve all web services on that machine how do I do it?

I can use WS-Discovery and I know how to do this with the .NET 4.0 Discovery API. However, the only sample I could find is multicast, which in my use case is not sufficient because:-

  1. I already know which machines may be hosting the service.
  2. UDP multicast is going to be unreliable given that I have potentially many machines on the network, but I am targetting only a limited subset.
  3. Managed mode with a proxy is not going to work either, because this is yet another "pre-requisite" I have to setup.

This should be easy right? Startup, send a message to one or more machines and wait for a specified period of time for a reply.

For some reason, however, that doesn't suffice. Any ideas?

Redirecting Standard Error & Output For A Child Process

If your application or library wants to kick off a child process C and you want to redirect standard output and error, this is what you do:

ProcessStartInfo psi = new ProcessStartInfo();

		    
		    psi.Arguments = this.CreateArgumentsString();
		    psi.FileName = this.ImageFile;
		    psi.CreateNoWindow = true;
		    psi.RedirectStandardError = true;
		    psi.RedirectStandardInput = true;
		    psi.RedirectStandardOutput = true;
		    psi.WorkingDirectory = this.WorkingDirectory;
		    psi.UseShellExecute = false;
		    
		    Process p = new Process();
		    
		    p.OutputDataReceived += new DataReceivedEventHandler(ProcessOutputDataReceived);
		    p.ErrorDataReceived += new DataReceivedEventHandler(ProcessErrorDataReceived);
		    p.StartInfo = psi;
		    p.EnableRaisingEvents = true;

p.Start();

p.BeginErrorReadLine();

		    p.BeginOutputReadLine();
		    p.WaitForExit();
 
That will redirect output and error data from your child process to the console asynchronously (er, depending on the implementation of ProcessOutputDataReceived and ProcessErrorDataReceived). Which is good because if the invoking application is a console application and you want to see the output/error in real time then you want it to work like this.

Debuggee Process Terminated Abruptly

I have been getting this error lately when trying to debug a test. I would be stepping over line by line and suddenly the debuggee process will exit suddenly. The proces exits with the error code is -2147023895 (0x800703e9).  There are only a few reasons why a process would exit suddenly. One of them is a stack overflow and this error code is exactly that.

But, I don't have any recursive or other code that could cause a stack overflow. So what could be the problem? Apparently, when you have implicit function evaluation is enabled in VS (Tools -> Options -> Debugging) and the evaluated function/property has a recursion (type?) this could happen. Another reason it could happen is if you have the "DebuggerDisplay" attribute on your type. I do this often because I would like to glance at the instance and get an idea of what it is. Maybe this is the problem? It is in fact the problem.

IGNORE_DUP_KEY, LINQ & Deadlocks

I am working on a project. The idea is to persist stack trace information for each test failure in a database so that we can automate/facilitate analysis of related test failures. For example, we can query for all tests that fail at a particular method or that have a subset of the given strack trace and so on. This allows testers to easily group together multiple test failures that arise due to the same reason and classify them as a single failure.

As I implemented these changes, I used LINQ from the beginning. But, recently an issue arose when I was testing reporting with multiple parallel test agents. The problem was that the entries for the same entity inserted twice. This would break analysis and so I created a unique index key on the columns that uniquely identify an entity. I set the IGNORE_DUP_KEY option on this index as well. Unfortunately, LINQ can't handle this. LINQ won't gracefully handle a duplicate insert.

On a side note, to handle this, I create a stored procedure to perform the check and insert within a serializable transaction and invoked this via LINQ. However, again, when going at it with multiple test agents, this approach breaks down because the database server kills one of the transaction as a deadlock victim. This will be propagated as a SqlException with an error code of 1209 which you may want to handle if you want to retry the submit after some random interval.

Posted by Vijai Kalyan | 1 Comments

Implementing a Custom Test Type - Part 3 (Dissecting the MyTest Sample - 1)

This post deals with the MyTest sample. I will start at the beginning of time.

Pre Requisites

I copied over the MyTest folder in the Visual Studio SDK to a separate directory. In doing so, I discovered some issues with the MyTestUI project. You will have to open up the MyTestUI.vcproj file in NotePad and replace the "..\..\..\..\VisualStudioIntegration\" parts with PATHTOVSSDK\VisualStudioIntegration. This ensures that the copied over solution builds correctly.

Another point to note is that I moved the entire solution to a location with no spaces in the path. That way I don't have deal with all the long name related issues. (As a matter of principle, I usually never put anything except the default stuff in locations with spaces in the name.)

A third action is to go through the registry searching for anything with a "MyTest*" entry and deleting them. This ensures that we are starting with a clean slate.

Finally, open up MyTest.sln. Fix your solution configuration. MyTestUI.vcproj is a VC project targeting Win32 and MyTest.csproj is a C# project targeting AnyCPU. Make sure your solution has only one configuration that builds the (Debug, Release) configurations with the Win32 and AnyCPU platforms for these projects respectively.

OK. Now open up MyTest.sln and rebuild everything. Open a VS command shell (in your Start menu folder for Visual Studio 2008, there is a Visual Studio Tools folder). Navigate to the location you copied the MyTest sample to and execute deploy.bat Debug. That should succeed.

Then execute devenv /setup. This ensures that your project and item templates are rebuilt. Start up Visual Studio, create a test project. Then add a new test item and voila! you should see MyTest and a MyTest Wizard entry in the test types dialog.

More on what happens after you select the MyTest test in the next post.

Implementing a Custom Test Type - Part 2

I have been investigating this topic for the past couple of days. Not much progress, but some interesting things to note.

  1. It might be an illuminating experience to use .NET Reflector to disassembly and study the unit test type implementation. The relevant assemblies are
  2. Microsoft.VisualStudio.QualityTools.UnitTestFramework
    Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter
    Microsoft.VisualStudio.QualityTools.Tips.UnitTest.AssemblyResolver
    Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel
    Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Tip
    Microsoft.VisualStudio.QualityTools.AgentObject
    Microsoft.VisualStudio.QualityTools.ExecutionCommon
    Microsoft.VisualStudio.QualityTools.Common
    Microsoft.VisualStudio.QualityTools.Resource
  3. Study the MyTest implementation provided in the Visual Studio SDK. Note that they have included some code that is related to the test project (the wizard and so on) that are not directly related to the test type implementation itself. This is the MyTestWizard class.

The MSDN documentation is sparse on this topic which is kind of frustating, but figuring this out should be fun.

Also, note that there are deploy.bat and undeploy.bat files in the MyTest sample which indicate what needs to be done in order to register and unregister a new test type.

However, my last attempt to register and used the MyTest sample failed which again, is kind of frustrating.

Update:

I managed to get the MyTest sample working. So, I will post a step by step account of how it seems to work in a subsequent post.

How To Get Native Processor Architecture

As anyone who has been looking at my blog knows, I have been mostly working on VS integration of the test tools used in our team (apart from setting up and managing TFS for the builds and various other initiatives.)

Anyway, so, I started to author a project template for VS. What's a project without a wizard? So, there is a wizard where you input some basic information and it will generate skeleton test classes, test areas, setup the references, the target platform etc.

The crux of the issue is, the wizard is 32bit. However, it needs to setup the project to target 32bit or 64bit. Depending on the target, the references to the core test infrastructure assemblies have to be added. However, the setup for the test infrastructure will add keys in the following pattern:

x64

HKLM\SOFTWARE\Microsoft\Mantis\x64\InstallDir

HKLM\SOFTWARE\Wow6432Node\Mantis\x86\InstallDir

x86

HKLM\SOFTWARE\Microsoft\Mantis\x86\InstallDir

There is the issue. Let us suppose, I am targetting the 64bit platform from my 32bit wizard. The wizard has to know that the installation folder for the x64 binaries reside in the folder pointed to by the value of the key HKLM\SOFTWARE\Microsoft\Mantis\x64\InstallDir. AFAIK, to do this correctly, the wizard has to figure out whether it is executing as a 32bit binary on WoW64 or as a 32bit binary on a 32bit machine.

After a lot of digging around and playing around with an interop layer (and not being successful I might add), I figured out a solution. I have replicated the code below (it is provided AS-IS).

namespace SystemInfo
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    /// 
    /// Interop layer to get information about the host system.
    /// 
    public sealed class SystemInfo
    {
        #region Enumerations

        /// 
        /// Maps to the native processor architecture values.
        /// 
        public enum ProcessorArchitecture : int
        {
            /// PROCESSOR_ARCHITECTURE_UNKNOWN
            None = -1,

            /// PROCESSOR_ARCHITECTURE_INTEL
            Intel = 0,

            /// PROCESSOR_ARCHITECTURE_IA64
            IA64 = 6,

            /// PROCESSOR_ARCHITECTURE_AMD64
            AMD64 = 9
        }

        #endregion

        #region Private Data

        /// The native system info instance.
        private SystemInfoNative nativeInfo;

        #endregion

        #region Constructors

        /// 
        /// Initializes a new instance of the  class.
        /// 
        /// The native system info.
        private SystemInfo ( SystemInfoNative nativeInfo )
        {
            this.nativeInfo = nativeInfo;
        }

        #endregion

        #region Properties

        /// 
        /// Gets the processor architecture.
        /// 
        /// The processor architecture.
        public ProcessorArchitecture Architecture
        {
            get
            {
                return (ProcessorArchitecture) Enum.Parse ( typeof ( ProcessorArchitecture ),
                                                            Enum.GetName ( typeof ( ProcessorArchitecture ),
                                                                           this.nativeInfo.processorArchitecture ) );
            }
        }

        #endregion

        #region Static Methods

        /// 
        /// Gets the native system info.
        /// 
        /// 
        public static SystemInfo GetNativeSystemInfo ()
        {
            SystemInfoNative native = new SystemInfoNative ();

            GetNativeSystemInfo ( ref native );

            return new SystemInfo ( native );
        }

        #endregion

        #region Native

        [StructLayout ( LayoutKind.Sequential )]
        private struct SystemInfoNative
        {
            internal ushort processorArchitecture;
            private  ushort reserved;
            internal uint   pageSize;
            internal IntPtr minimumApplicationAddress;
            internal IntPtr maximumApplicationAddress;
            internal IntPtr activeProcessorMask;
            internal uint   numberOfProcessors;
            internal uint   processorType;
            internal uint   allocationGranularity;
            internal ushort processorLevel;
            internal ushort processorRevision;
        }

        [DllImport ( "kernel32.dll" )]
        private static extern void GetNativeSystemInfo ( ref SystemInfoNative lpSystemInfo );

        #endregion
    }
}

Implementing a Custom Test Type - Part 1

Introduction

This and the next few blog posts will be focussed on implementing a custom test type for Visual Studio 2008. We have an internal tool that our team uses for testing quite extensively and which pre-dates the testing capabilities introduced in Visual Studio. Consequently, we have a considerable investment in our test code. However, given the broad range of other features that the Visual Studio test tools provide, we obviously do not want to lose out on harnessing them. For example, one scenario would be to combine our tests into load tests and execute them VS. Another scenario would be to use TFS to execute our tests every few product builds. Although the latter can be done with a custom MSBuild task, it would be great for other integration efforts if our tests were treated like a native VS test type.

Getting Started

The first thing that the test type extensibility documentation fails to mention is that a custom test type is implemented in a VS Integration Package. Consequently, although you might be tempted to write a class library containing your implementation of the various interfaces detailed here

http://msdn.microsoft.com/en-us/library/bb166343.aspx#TestElementClass

and follow the instructions given at the end for registering your test type. However, the sample provided in the VS SDK at

%HOMEDRIVE%:\%PROGRAMFILESDIR%\Microsoft Visual Studio 2008 SDK\VisualStudioTeamSystemIntegration\Test Tool Extensibility\MyTest 

makes it quite evident that you need a VSIP. So, we will start by creating one.

Posted by Vijai Kalyan | 2 Comments

Ideas For The Next Generation Of Programming Languages

This blog post deals with a vareity of things I feel would be nice to have features in the next generation of programming languages. I keep learning about compiler technology and I am hoping that in the next few years, I will be able to build a compiler for a full fledged language that incorporates the following ideas as well as great ideas from other programming languages.

    Integrated Factory Pattern

This is one feature I have no idea about how to implement. But, it would be nice to have.

The idea is that one can write this code

public interface IElementFactory

{

public static IElement CreateElement (string name);

}

[DefaultFactory(typeof(SimpleElementFactory))]

public static class AbstractElementFactory : IElementFactory

{

public static IElement CreateElement (string name);

}

Then, the compiler can generate the appropriate glue code that looks something like this

[DefaultFactory(typeof(SimpleElementFactory))]

public static class AbstractElementFactory : IElementFactory

{

[MethodImpl(MethodImplOptions.Synchronized)]

public static IElement CreateElement (string name)

{

if (null == Instance)

Instance = new default();

return Instance.CreateElement (name);

}

private static IElementFactory Instance

{

get;

set;

}

public static void Register (IElementFactory factory)

{

Debug.Assert(null != factory);

if ( null == factory )

throw new ArgumentNullException ("factory");

Instance = factory;

}

}

and further, recognize the following entry in the configuration to instantiate and register the appropriate factory instance by default.

<factoryConfig name="X.Y.Z.AbstractElementFactory" register="XYZElementFactory" params="1,2,3,4,5"/>

    Meta-data describing parameter usage.

A programming language like C# already has some of this. For example, you can specify parameters as being out, in, ref. But, what if we could do something like

public bool [checked] CreateObject ( string name [!= null, .Length > 0, ~= '(?:parse)'], readonly ref IList<string> stringList, ref readonly IList<string> sourceList [!= null, .Count > 0, foreach (~= '(?:Test)')]

In the above, the [checked] meta-keyword on the return type indicates to the compiler that the return value from this method must be checked by the caller. For the first parameter, 'name', the restrictions between [ and ] are a combination of properties and meta's. I think the implication of what this is enforcing is clear. In this case, the compiler automatically generates the appropriate Debug.Assert and throw new ArgumentException statements for the given restrictions. Besides, having this as part of the code makes it abundantly clear to developers perusing the code as well as those looking at the documentation and most importantly testers what the expected input is. This makes it even easier for automatic test generators to generate both positive and negative test data thus obviating the need to write boiler plate code. We can of course extend this type of declarative restriction system using more complex rules.

   Validation for automatic properties.

I would love to retain the syntactic sugar of automatic-properties coupled with meta-data specified validation and initialization method invocation.

How could this work? Consider the following example:

public class Foo

{

public string Name

{

get;

set;

}

}

We can do this today in C#. However, AFAIK, if you wanted to add validation to the above, you would have to then provide the field that the property wraps, as well as adding in the validation code. I would rather have this specified like this:

public string Name

{

get;

set;

validate ( string value )

{

if ( null == value || value.Length <= 0)

throw new ArgumentException("Cannot be NULL or empty.", "Name");

}

initialize ()

{

return "FooBar";

}

}

where the methods validate and initialize are implicitly private, implicitly return void and string respectively, are invoked when the setter is invoked and invoked the first time the getter is invoked respectively. This is far more self-contained and makes more readable code since I don't have to

  1. Implement a private field to provide storage for the property.
  2. Implement private methods separate from the property and hence potentially invocable by others.
  3. Scroll back and forth in code.

   Syntactic sugar for auto-delegation.

It would be so good to have syntactic sugar for auto-forwarding non-implemented interface/abstract inherited methods to a contained instance.

For example, let us say I have an interface I. There is an existing class hierarchy consisting of classes X, Y and Z where Z is-a Y and Y is-a X. Now, I want to introduce a bunch of additional classes C1 through CF (that's 16 of them) each of which inherit from Z and also implement the interface I. Now, let's say, all I am doing is defining some properties in I. Ideally, I would have been able to implement all the properties in a class IImpl and then allow C1 through CF to multiple inherit from Z and IImpl. Since that is increasingly becoming not possible in many languages such as Java and C#, it would be nice to have this instead:

public class Ci : Z, I [forward: IImpl:iImplementationInstance]

{

// just Ci behavior.

What's the sugar here? I interface method invocations on Ci get auto-forwarded to the contained instance iImplementationInstance of type IImpl. Whoohoo!

Access control on fields by methods.

Defaulting of default values for properties and fields through meta-data.

Declarative syntax for graph like object hierarchy navigation.

Aspect oriented programming features.

Executing program inspection built into the language.

Program directed compilation.

Code templatization invoked by the compiler.

Pattern systems built into the language.

Rule system for specifying dependencies among types in generic types.

Rule system for specifying inter-dependencies among values for data types.

 

Duh! InitializeShapeFields is only invoked once!

Beware!!!

 The method InitializeShapeFields appears in each shape class that is generated by the DSL tools. However, this method is only invoked ONCE per shape. It is due to this code

/// <summary>

/// Per-class ShapeFields for this shape.

/// </summary>

public override global::System.Collections.Generic.IList<DslDiagrams::ShapeField> ShapeFields

{

get

{

if (shapeFields == null)

{

shapeFields = CreateShapeFields();

}

return shapeFields;

}

}

 

 Note what the comment says. It says "/// Per-class ShapeFields for this shape." Thus, a static collection is being initialized by a member method.

Thus, if you are rolling your own shape fields and expect them to work correctly, make sure you know that the shape field won't be a per shape instance. It will be a per class instance. Thus, you will need to incorporate the appropriate logic for ensuring that your shape field will correctly.

 For example, in our DSL, we have a "hyperlink field" which when clicked on pops up an in-place grid editor. The grid derives its data from the model element linked to the shape element on which our "hyperlink field" is parented. Oops! We initially wrote the implementation assuming that there would be one field per shape element. However, the fix was not hard. The field

DiagramItem item = e.HitDiagramItem;

where e is the DiagramEventArgs passed to the OnClick method is what we needed.

 

Auto Layout Diagram

In the DSL project I am working on, I am performing layout for the model diagram myself. This problem mostly arises because we want two-way synchronization between model and code. Hence, after one of our users has modified the diagram by hand, if we update the model from code, the diagram can get screwed up. The DSL tools do not seem to have any  way to direct layout of a model automatically after it is loaded but before it is displayed. If you use the rule engine to layout model shapes using the RuleOn attribute, we discovered that the rules can get fired when the model is being built (there may be a way to eliminate this behavior however). Thus, we ended up with having to special case our layout rules with repeated adjustments. A second approach we took is to split the problem.

  1. When the model is already displayed and the user adds an element, then we only layout that shape and adjust the subsequent shapes. We assume that the model before the new shape was added is laid out correctly. Thus, we did not have to special case our rules and we were assured of a model that had been laid out well enough.
  2. When the model is being loaded, none of the layout rules fire. However, layout is performed after the model data has been fully loaded, but before the designer surface is displayed.

We achieved part (2) by overriding the LoadView method in the ModelNameDocView type. Something like this:

protected override bool LoadView()

{

bool status = base.LoadView();

if (status)

{

MantisTestDiagram diagram = this.Diagram as MantisTestDiagram;

diagram.LayoutEngine.Layout();

}

return status;

}

The LayoutEngine::Layout method walks through all the model shapes and relocations them correctly. A brief skeleton for this method looks like this:

 

this.parentDiagram.Store.RuleManager.SuspendRuleNotification();

 

try

{

using (Transaction t = this.parentDiagram.Store.TransactionManager.BeginTransaction("Model-Auto-Layout-" + this.parentDiagram.Id))

{

// do layout

t.Commit();

}

}

catch (Exception ex)

{

Utility.DisplayException(ex); // debug help

throw;

}

finally

{

this.parentDiagram.Store.RuleManager.ResumeRuleNotification();

}

64 Bits and DSL Tools

The DSL projects created by the DSL package wizard are configured to target AnyCPU.However, if you write a command line tool that references the generated DSL library, you should set up your project to target x86 even if you are working on a 64-bit machine. The reason is that there is no 64 bit version of Visual Studio. Consequently, you will get a type load exception which isn't much help.

DTE Events Are Different

As I learned today, DTE events are different. Event handlers that are local are marked for garbage collection when the handler goes out of scope. That means the event handler is likely to be fired at most once or never.

 

Modifying the Item Context Menu

I was trying to implement an AddIn that modified the context menu for project items. Specifically, C# files. After poking around for a couple of hours, I succeeded. Adding a menu item (or CommandBarControl) is relatively simple. What was complicated was adding it to the right context menu.

In my case, the menu I wanted was the Item menu. So you do this with the following code

CommandBar cbar = (GlobalInstanceCache.MainApplication.CommandBars as CommandBars)["Item"];

CommandBarControl ctrl = cbar.Controls.Add(MsoControlType.msoControlButton, Missing.Value, Missing.Value, 1, true) as CommandBarControl;

ctrl.Caption = "My Ctx Menu Command";

That worked.

Posted by Vijai Kalyan | 0 Comments

Visual Studio Project Creation Wizard and Templates

I found an interesting feature when creating a Visual Studio project template. (Details about how to create a template is left to a forthcoming post.) I was working on a wizard for the project template.

In the IWizard::RunStarted method for the wizard, when you add items to the replacementsDictionary parameter, Visual Studio seems to take the values and substitute them into the template files. This is not surprising since this is what it is advertised to do. What is interesting is the difference in how the variables are named.

 For example, when referring to the project name in a template file, you would use the construct $safeprojectname$. However, let's say you have something in a template file such as "myVariableName". If you add an entry in the replacementsDictionary with a key named myVariableName with value someVariable, then the fragment "myVariableName" in the template file is turned into "someVariable". This is different from the other construct's. Which is interesting since I am not sure if this is documented anywhere.

 
Page view tracker