Monday 30 April 2012

MS Build Commands and Properties

 Reference build commands for MS Build

MacroDescription
$(RemoteMachine)Set to the value of the Remote Machine property on the Debug property page. See Changing Project Settings for a C/C++ Debug Configuration3 for more information.
$(Configuration)The name of the current project configuration (for example, "Debug").
$(Platform)The name of current project platform (for example, "Win32").
$(ParentName)(Deprecated.) Name of the item containing this project item. This will be the parent folder name, or project name.
$(RootNameSpace)The namespace, if any, containing the application.
$(IntDir)Path to the directory specified for intermediate files relative to the project directory. This path should have a trailing slash. This resolves to the value for the Intermediate Directory property.
$(OutDir)Path to the output file directory, relative to the project directory. This path should have a trailing slash. This resolves to the value for the Output Directory property.
$(DevEnvDir)The installation directory of Visual Studio 2010 (defined as drive + path); includes the trailing backslash '\'.
$(InputDir)(Deprecated; migrated.) The directory of the input file (defined as drive + path); includes the trailing backslash '\'. If the project is the input, then this macro is equivalent to $(ProjectDir).
$(InputPath)(Deprecated; migrated.) The absolute path name of the input file (defined as drive + path + base name + file extension). If the project is the input, then this macro is equivalent to $(ProjectPath).
$(InputName)(Deprecated; migrated.) The base name of the input file. If the project is the input, then this macro is equivalent to $(ProjectName).
$(InputFileName)(Deprecated; migrated.) The file name of the input file (defined as base name + file extension). If the project is the input, then this macro is equivalent to $(ProjectFileName).
$(InputExt)(Deprecated; migrated.) The file extension of the input file. It includes the '.' before the file extension. If the project is the input, then this macro is equivalent to $(ProjectExt).
$(ProjectDir)The directory of the project (defined as drive + path); includes the trailing backslash '\'.
$(ProjectPath)The absolute path name of the project (defined as drive + path + base name + file extension).
$(ProjectName)The base name of the project.
$(ProjectFileName)The file name of the project (defined as base name + file extension).
$(ProjectExt)The file extension of the project. It includes the '.' before the file extension.
$(SolutionDir)The directory of the solution (defined as drive + path); includes the trailing backslash '\'.
$(SolutionPath)The absolute path name of the solution (defined as drive + path + base name + file extension).
$(SolutionName)The base name of the solution.
$(SolutionFileName)The file name of the solution (defined as base name + file extension).
$(SolutionExt)The file extension of the solution. It includes the '.' before the file extension.
$(TargetDir)The directory of the primary output file for the build (defined as drive + path); includes the trailing backslash '\'.
$(TargetPath)The absolute path name of the primary output file for the build (defined as drive + path + base name + file extension).
$(TargetName)The base name of the primary output file for the build.
$(TargetFileName)The file name of the primary output file for the build (defined as base name + file extension).
$(TargetExt)The file extension of the primary output file for the build. It includes the '.' before the file extension.
$(VSInstallDir)The directory into which you installed Visual Studio 2010.
This property contains the version of the targeted Visual Studio, which might be different that the host Visual Studio. For example, when building with $(PlatformToolset) = v90, $(VSInstallDir) contains the path to the Visual Studio 2008 installation.
$(VCInstallDir)The directory into which you installed Visual C++ 2010.
This property contains the version of the targeted Visual C++, which might be different that the host Visual Studio. For example, when building with $(PlatformToolset) = v90, $(VCInstallDir) contains the path to the Visual C++ 2008 installation.
$(FrameworkDir)The directory into which the .NET Framework was installed.
$(FrameworkVersion)The version of the .NET Framework used by Visual Studio. Combined with $(FrameworkDir), the full path to the version of the .NET Framework use by Visual Studio.
$(FrameworkSDKDir)The directory into which you installed the .NET Framework. The .NET Framework could have been installed as part of Visual Studio 2010 or separately.
$(WebDeployPath)The relative path from the web deployment root to where the project outputs belong. Returns the same value as RelativePath4.
$(WebDeployRoot)The absolute path to the location of <localhost>. For example, c:\inetpub\wwwroot.
$(SafeParentName)(Deprecated.) The name of the immediate parent in valid name format. For example, a form is the parent of a .resx file.
$(SafeInputName)(Deprecated.) The name of the file as a valid class name, minus file extension.
$(SafeRootNamespace)(Deprecated.) The namespace name in which the project wizards will add code. This namespace name will only contain characters that would be permitted in a valid C++ identifier.
$(FxCopDir)The path to the fxcop.cmd file. The fxcop.cmd file is not installed with all Visual C++ editions.














Source is from :http://msdn.microsoft.com/en-us/library/c02as0cs.aspx

Implementing MEF with list of attributes

After working with Managed Extensibility Framework or MEF (http://mef.codeplex.com/) for a while, I came across a case when I needed to add attributes to different classes in order to distinguish implementation from classes.

As sample scenario you have different implementation of service and you want to set specific Ids of services to run in different way.

To do this using MEF is a bit different as we need to find out which implementation the one we want to use.

As solution I have chosen to use decoration something like this:
    [SignalSystemData(name, array)]
Where variables are:
  1.  name: variable name so we can identify the string between our classes and implementations.
  2.  array: related items for this implementation
And we come up with something like:

    [SignalSystemData("ServiceIds", new int[]{2,5,3 , 15,16,300,301,302,305})]




After searching modification of code the solution is here:




namespace ConsoleApplication1
{
   using System;
   using System.Collections.Generic;
   using System.ComponentModel.Composition;
   using System.ComponentModel.Composition.Hosting;
   using System.Linq;
   using System.Reflection;

    internal class Program
    {
        private static void Main(string[] args)
        {
            int serviceIdToCall = 305;

            var c = new Class1();
            var v = c.EditorSystemList;
           
            foreach (var lazy in v.Where(x=>x.Metadata.LongName=="ServiceIds"))
            {
                if (lazy.Metadata.ServiceId.Contains(serviceIdToCall))
                {
                    var v2 = lazy.Value;
                    // v2 is the instance of MyEditorSystem
                    Console.WriteLine(serviceIdToCall.ToString() + " found");

                }else
                {
                    Console.WriteLine(serviceIdToCall.ToString() + " not found");
                }
            }

            Console.ReadKey();
        }
    }

    public class Class1
    {
        [ImportMany]
        public IEnumerable<Lazy<IEditorSystem, IEditorSystemMetadata>> EditorSystemList;

        public Class1()
        {
            var catalog = new AggregateCatalog(
                new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
            Console.Write("Composition completed");
        }
    }

    [Export(typeof (IEditorSystem))]
    [SignalSystemData("ServiceIds", new int[]{2,5,3 , 15,16,300,301,302,305})]
    public class MyEditorSystem2 : IEditorSystem
    {
        public void Test ()
        {
            Console.WriteLine("ServiceID : 2");
        }
    }

    [Export(typeof(IEditorSystem))]
    [SignalSystemData("ServiceIds", new [] {1, 20})]
    public class MyEditorSystem1 : IEditorSystem
    {
       
        #region Implementation of IEditorSystem

        public void Test()
        {
            Console.WriteLine("ServiceID : 1");
        }

        #endregion
    }


    public interface IEditorSystem
    {
        void Test();
    }

    [MetadataAttribute]
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
    public class SignalSystemDataAttribute : ExportAttribute
    {
        public SignalSystemDataAttribute(string longName, params int[] serviceId)
            : base(typeof (IEditorSystem))
        {
            LongName = longName;
            ServiceId = serviceId;
        }

        public string LongName { get; set; }
        public int[] ServiceId { get; set; }

    }

    public interface IEditorSystemMetadata
    {
        string LongName { get; }
        int[] ServiceId { get; }
    }   
}

Tuesday 10 April 2012

TFS project node with red cross


There is an issue which can arise with Visual Studio 2010 Team Explorer where the TFS project node will not expand. There is a red cross on the project node to indicate that it hasn't loaded correctly.

In addition, it is not possible to publish your web applications.

Both of these issues are caused by the same root problem.

Visual studio uses an internet explorer dll to do certain things, and it can happen that this dll becomes unregistered.

Simply open a normal command prompt (does not need to be a Visual Studio command prompt) and enter the following

regsvr32 "C:\Program Files (x86)\Internet Explorer\ieproxy.dll" 
or
regsvr32 "C:\Program Files\Internet Explorer\ieproxy.dll"

Depending on your version of Windows x86 or 64bit .

Saturday 7 April 2012

Implemeting Remote Validation ASP.NET MVC3

I have finally found some time to look at implementation of Remote Validation in ASP.NET MVC3

Definition of a entity DummyUser

public class DummyUser
    {
        [Required]
        [StringLength(6, ErrorMessage = "User name has to be longer than 6 characters.")]
        public string UserName { getset; }
 
        public string Name { getset; }
 
        public string Surname { getset; }
 
        public string Age { getset; }
    }

In Home controller I have created basic Create function. and the view as is displayed

    /// <summary>
    /// Default controller for remote validation.
    /// </summary>
    public class HomeController : Controller
    {
        /// <summary>
        /// Creates new dummy user.
        /// </summary>
        /// <returns> 
        /// View for creating new dummy user. 
        /// </returns>
        [HttpGet]
        public ActionResult Create()
        {
            return View(new DummyUser());
        }
    }

And generate new view : Create for entity.


Update Global.asax to start on this action as:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
            routes.MapRoute(
                "Default"// Route name
                "{controller}/{action}/{id}"// URL with parameters
                new { controller = "Home"
                      action = "Create"
                      id = UrlParameter.Optional } // Parameter defaults
            );
        }


As next step we have to make sure that web.config has set up remote validation.
Which is done in section of App Settings.

 <appSettings>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>


Now we have set up everything to start implementing remote validation.

We will add new attribute to UserName.
The attribute "Remote"  uses the jQuery validation plug-in. Read more about remote attribute
 Where we will define action and controller in order [Remote("Action","Controller")]

  public class DummyUser
    {
        [Required]
        [StringLength(6, ErrorMessage = "User name has to be longer than 6 characters.")]
        [Remote("ValidUserName","HomeValidation")]
        public string UserName { getset; }
 
        public string Name { getset; }
 
        public string Surname { getset; }
 
        public string Age { getset; }
    }



Now we need to Create our validation controller.
The recommendation is to create separate validation controller to separate logic from actions and validation, but it is not a rule and you can put your validation into same controller.
In my case i will be using HomeValidationController for this example

    /// <summary>
    /// Home validation controller with no output cache and disabled storing values.
    /// </summary>
    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
    public class HomeValidationController : Controller
    {
        /// <summary>
        /// Valids the name of the user.
        /// </summary>
        /// <param name="userName">Name of the user.</param>
        /// <returns>Result validation</returns>
        public JsonResult ValidUserName(string userName)
        {
            if (!string.IsNullOrEmpty(userName))
            {
                // we can allow get because there is no data saving
                return Json(trueJsonRequestBehavior.AllowGet);
            }
 
            return Json(falseJsonRequestBehavior.AllowGet);
        }
    }

Now when you start typing into the user name selection you will see request going to validation controller and hitting action in your controller.



The best part on this validation is that you do not have to modify anything in your view and this will just work. That's what i call magic:)