CS0103: The name ‘InitializeComponent’ does not exist in the current context

Posted at: 2/8/2011 at 9:05 PM by saravana

When you are trying to build a solution that contains Silverlight or WPF projects using MsBuild you may encounter the error "CS0103: The name ‘InitializeComponent’ does not exist in the current context". While searching I have found various tactics that involves editing the project files, including namespaces etc.

 

But the real reason is you are using the wrong version of Msbuild. Multiple versions of Msbuild gets installs in your machine, through various versions of .NET framework, Visual Studio etc. So, check the path variables and make sure you are using the correct version. If you are using Silverlight 4, you need to use MsBuild.

Nandri!

Saravana

Tags:  Categories: .NET | Silverlight
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(0) | Comments RSS

Timestamp to word phrase in C#

Posted at: 2/6/2011 at 6:23 AM by saravana

Ok, this is not rocket science topic, but when I looked for a sample I couldn't find any decent one. With the popularity of facebook, twitter, stackoverflow etc its becoming more of a norm to represent the time stamp in words rather than simply date time value.

image

image

This is what I come up with, people simply forget how powerful the .NET programming stack is. It didn't take more than 15 minutes to come up with this helper.

 public static string DateDiffInWords(DateTime dateTimeBegin, DateTime dateTimeEnd)
        {
            TimeSpan diff = dateTimeBegin.Subtract(dateTimeEnd);

            if (diff.Days > 10)
            {
                string s = string.Format("{0} '{1} at {2}"
                    , dateTimeEnd.ToString("MMM dd")
                    , dateTimeEnd.ToString("yy")
                    , dateTimeEnd.ToString("HH:mm"));
                return  s;
            }
            if (diff.Days > 1)
                return string.Format("{0} days ago", Convert.ToInt32(diff.Days));
            if (diff.Days == 1) 
                return "Yesterday";
            if (diff.Hours > 1) 
                return string.Format("{0} hours ago", Convert.ToInt32(diff.Hours));
            if (diff.Minutes > 1)
                return string.Format("{0} minutes ago", Convert.ToInt32(diff.Minutes));
            if(diff.Seconds > 1)
                return string.Format("{0} seconds ago", Convert.ToInt32(diff.Seconds));
            
            //we should not reach here
            return string.Empty;
        }
Few sample calls:
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 2, 5, 14, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 1, 28, 14, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 2, 5, 05, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 2, 4, 14, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 2, 3, 14, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 2, 2, 14, 45, 45)));
Console.WriteLine(Date.DateDiffInWords(DateTime.Now, new DateTime(2011, 1, 2, 14, 45, 45)));
And the Outcome:

image

Nandri,

Saravana

Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(0) | Comments RSS

Code Snippet: Sort nodes in an Xml Document

Posted at: 7/13/2007 at 4:40 PM by saravana

Scenario: Need to produce a new xml document with particular node list sorted in ascending/descending order

Code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"Input.xml");

XmlDocument xmlDocCopy = new XmlDocument();
xmlDocCopy.LoadXml(xmlDoc.OuterXml);
xmlDocCopy.SelectSingleNode("//Links").RemoveAll();

XmlNode node = xmlDoc.SelectSingleNode("//Links");
XPathNavigator navigator = node.CreateNavigator();
XPathExpression selectExpression = navigator.Compile("Link/Title");
selectExpression.AddSort(".", XmlSortOrder.Ascending, XmlCaseOrder.None, "", XmlDataType.Text);
XPathNodeIterator nodeIterator = navigator.Select(selectExpression);
while (nodeIterator.MoveNext())
{
    XmlNode linkNode = xmlDoc.SelectSingleNode("//Link[Title=\"" + nodeIterator.Current.Value + "\"]");
    XmlNode importedLinkNode = xmlDocCopy.ImportNode(linkNode, true);
    xmlDocCopy.SelectSingleNode("//Links").AppendChild(importedLinkNode);
}

xmlDocCopy.Save(@"Output.xml");

Input:

<Section>
    <Links>
        <Link Id="1">
            <Title>Jupiter Line</Title>
            <AddedDate>27/05/2007</AddedDate>
        </Link>
        <Link Id="2">
            <Title>Alfa Line</Title>
            <AddedDate>27/05/2007</AddedDate>
        </Link>
        <Link Id="3">
            <Title>Zebra Line</Title>
            <AddedDate>27/05/2007</AddedDate>
        </Link>
        <Link Id="4">
            <Title>Copper Line</Title>
            <AddedDate>27/05/2007</AddedDate>
        </Link>
    </Links>
</Section>

Output:

<Section>
  <Links>
    <Link Id="2">
      <Title>Alfa Line</Title>
      <AddedDate>27/05/2007</AddedDate>
    </Link>
    <Link Id="4">
      <Title>Copper Line</Title>
      <AddedDate>27/05/2007</AddedDate>
    </Link>
    <Link Id="1">
      <Title>Jupiter Line</Title>
      <AddedDate>27/05/2007</AddedDate>
    </Link>
    <Link Id="3">
      <Title>Zebra Line</Title>
      <AddedDate>27/05/2007</AddedDate>
    </Link>
  </Links>
</Section>

Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(4) | Comments RSS

Determine whether the BizTalk assembly is Debug or Release build at runtime.

Posted at: 6/2/2007 at 7:56 AM by saravana

Recently someone raised this question in the newsgroup, they wanted to branch inside the orchestration based on the build of the assembly itself. Whenever an assembly is build with "Debug" mode, some System.Diagnostics.DebuggableAttributes are added to the assembly. One such attribute is "IsJITTrackingEnabled", which will track information during code generation (MSIL) for the debugger.

So, we can use the simple technique of reflection to determine whether the assembly is build against "Debug" or "Release" mode by the presence or absence of IsJITTrackingEnabled attribute. The below method does exactly the same:

public static bool IsDebugBuild(System.Reflection.Assembly assembly)
{
object[] attributes = assembly.GetCustomAttributes(typeof(DebuggableAttribute), false);

if (attributes.Length == 0)
return false;//No debug attibutes, so release build

foreach (Attribute attr in attributes)
{
if (attr is DebuggableAttribute)
{
DebuggableAttribute d = attr as DebuggableAttribute;
if (d.IsJITTrackingEnabled == true) //this flag is set only for debug builds
return true; //Debug
else
return false; //Release
}
}
throw new Exception("Cannot determine the build type");
}

Place the above code in an utility class (external assembly), build and GAC it. Inside your Orchestration, reference the assembly, define a variable (ex: assm) of type "System.Reflection.Assembly" and call the method IsDebugBuild as shown below inside your Expression shape

assm = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.Debug.WriteLine(Utility.DetermineDebugOrRelease.IsDebugBuild(assm));

Place the above code in an utility class (external assembly), build and GAC it. Inside your Orchestration, reference the assembly, define a variable (ex: assm) of type "System.Reflection.Assembly" and call the method IsDebugBuild as shown below inside your Expression shape

assm = System.Reflection.Assembly.GetExecutingAssembly();
System.Diagnostics.Debug.WriteLine(Utility.DetermineDebugOrRelease.IsDebugBuild(assm));

NOTE: This example is just to address someone's question. You should try NOT to use this type of code in production. Because reflection is an expensive process and it doesn't really suits well for high throughput BizTalk applications.

Nandri!
Saravana

Tags:  Categories: .NET | BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(3) | Comments RSS

Can't see Debug statements in DebugView?

Posted at: 5/24/2007 at 6:49 AM by saravana

First of all to see debug statements inside DebugView you need to compile your assemblies in "Debug" mode. For optimization reasons if you compile your assemblies in "Release" mode all the System.Diagnostics.Debug.WriteLine statements will be ignored by the compiler and you won't see any output in the DebugView.

When it comes to BizTalk project sometimes even though the tool bar will say the build is "Debug" as shown in the figure

 

When you actually look into the properties of the BizTalk project it may be configured for "Deployment", which is equivalent to the "Release" build and it won't emit any debug symbols.

So, in this case you need to set the configuration to "Development", which is equivalent to "Debug" build.

Nandri!

Saravana

Tags: |  Categories: .NET | BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(3) | Comments RSS

Create safe File Name from string using .NET 2.0

Posted at: 5/19/2007 at 10:32 AM by saravana

Recently for one of my weekend project I wanted to create a safe file name from a string. In the past people used to do lot of regular expressions and conditional testing to meet this requirement. Still the filename won't be safe across multiple environments (Windows 2000/XP/2003 etc). But with .NET 2.0 using couple of inbuild functions (Path.GetInvalidFileNameChars and Path.GetInvalidPathChars) you can create safe filenames. Hope this piece of code will be useful to someone.

private string CreateValidFileName(string title, string extension)
{
string validFileName = title.Trim();

foreach (char invalChar in Path.GetInvalidFileNameChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}
foreach (char invalChar in Path.GetInvalidPathChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}

if (validFileName.Length > 160) //safe value threshold is 260
validFileName = validFileName.Remove(156) + "..."

return validFileName + "." + extension;
}

Nandri!

Saravana

Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(3) | Comments RSS

Now, I'm a "MCPD - Enterprise Application Developer for .NET 2.0"

Posted at: 5/15/2007 at 6:58 PM by saravana

I recently obtained my MCPD (Enterprise Applications Developer) credentials. I'll admit probably this is one thing which stayed in my TODO list for more than a year. Microsoft re-organised their certification program after the release of Visual Studio 2005, .NET 2.0 and SQL Server 2005. Not everyone is aware of this new certification model and its requires some explanation here.

According to new certification model, the two main categories of certification for people on Software development falls under either

Microsoft Certified Technology Specialist (MCTS), and eventually upgrading to Microsoft Certified Professional Developer (MCPD)

Microsoft Certified Technology Specialist (MCTS)

MCTS are targeted to specific technology so that you can demonstrate your expertise in that area. You can get your MCTS credentials in one of the following areas.

MCTS: .NET Framework 2.0 Web Applications
MCTS: .NET Framework 2.0 Windows Applications
MCTS: .NET Framework 2.0 Distributed Applications

In addition to the above credentials, which applies to all the .NET 2.0 developers based on their choosen field (Windows, Web and Distributed), developers can also obtain MCTS credential for some of the server products example:

MCTS: SQL Server 2005

MCTS: BizTalk Server 2006

Microsoft Certified Professional Developer: Enterprise Applications Developer (MCPD):

MCPD demonstrates comprehensive skills that are required to perform the job successfully, and must remain current in best practices and technologies. It also demonstrates that you have the comprehensive skills required to build n-tier solutions targeting both Web and rich-client user experiences. In order to obtain MCPD: Enterprise Applications Developer candidates must first meet the requirements for the following three Microsoft Certified Technology Specialist (MCTS) credentials for the Microsoft .NET Framework 2.0.

MCTS: .NET Framework 2.0 Web Applications
MCTS: .NET Framework 2.0 Windows Applications
MCTS: .NET Framework 2.0 Distributed Applications

In addition the candiate also required to pass a design paper "PRO: Designing and Developing Enterprise Applications by Using the Microsoft .NET Framework"

--Saravana

Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(0) | Comments RSS

Detect CLR version under which your BizTalk service is running.

Posted at: 5/12/2007 at 4:44 PM by saravana

It's becoming more and more eminent to understand under which CLR version (1.0, 1.1 or 2.0) the BizTalk runtime, In-Process host is running. I've explained in my previous post the various factors that influence loading appropriate CLR versions and also how you can make use of the config file to force a specific version of CLR is always loaded.

There is one more query raised, what happens if you install .NET Framework 3.0 on your BizTalk server (both 2004 and 2006)? The effect of installing .NET 3.0 doesn't introduce that much behavior difference when compared to installing .NET 2.0 on a BizTalk 2004 machine (built using .NET 1.1 with CLR 1.0). Because .NET 3.0 is still based on .NET 2.0 and shares the same compilers and Common Language Runtime (CLR 2.0). See the following posts from Somasegar and Jason Zanders (comments are more interesting on the both the posts)to see the effect of .NET 3.0.

With all this confusions around different versions of .NET and different versions of BizTalk and different Service Packs, there will certainly be a situation where developers and administrators need to figure out the version of CLR under which the BizTalk Runtime is running. This is where Process Explorer comes to our rescue.

Process explorer's top window shows a list of currently active processes, and the bottom windows shows either the handles opened by the selected  process or it shows the DLL's and memory mapped files that the selected process has loaded. You need to set the appropriate mode from the menu item "View-> Lower Pane View". In our case we need to figure out the version of Common Language Runtime core dll (mscorwks.dll) loaded into the BizTalk runtime process (BtsNtSvc.exe). So, we'll set the "Lower Pane View" to DLL's (or Ctrl + D).

Also, make sure the following columns are visible Description, Image Path, and Version. To do so, click on "View" menu and select "Select Columns" and in the "Process Image" tab select appropriate columns.

 The following screen shot is taken from a machine running BizTalk 2006. You can see clearly from the bottom pane the version of CLR running is 2.0 and physical dll is loaded from the location "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll". BTW, .NET 3.0 is installed in this particular machine, which also proves .NET 3.0 still uses CLR 2.0. In BizTalk 2006 box its quite straight forward it's all .NET 2.0. Things will become more interesting in BizTalk 2004 scenario.

Nandri!

Saravana

Tags:  Categories: .NET | BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(2) | Comments RSS

How to create an invisible .NET Application?

Posted at: 2/5/2007 at 6:35 PM by saravana

Formless application or in my terms invisible .NET applications. I recently end up in a situation where I want to run my console application on a regular schedule via Scheduled task. Problem with that is, it opens ups the schedule every time it runs the application.

There were few suggestions in the newsgroups to create a Windows NT service, or WinForms application and make the form invisible etc, etc. None of them were appropriate for me.

At last I ended up with a quick and efficient fix.

Go to project properties and set "Output Type" to "Windows Application" for your Console application.

Now, I can't see the console anymore, and the application runs in the background quietly.

Nandri!

Saravana

Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(0) | Comments RSS

Microsoft.BizTalk.Gac.Fusion, Access denied Exception and GAC is empty

Posted at: 1/8/2007 at 7:56 PM by saravana

 

Once in a while you get the following error while trying to compile/build the BizTalk project (With few more Access denied errors and some HRESULT values).

Error 2 at Microsoft.BizTalk.Gac.Fusion.IAssemblyCache.InstallAssembly(AssemblyCacheInstallFlag flags, String manifestFilePath, FusionInstallReference referenceData)
at Microsoft.BizTalk.Gac.Gac.InstallAssembly(String assemblyPathname, Boolean force)
at Microsoft.BizTalk.Deployment.BizTalkAssembly.GacInstall(String assemblyLocation)
at Microsoft.BizTalk.Deployment.BizTalkAssembly.PrivateDeploy(String server, String database, String assemblyPathname, String applicationName)
at Microsoft.BizTalk.Deployment.BizTalkAssembly.Deploy(Boolean redeploy, String server, String database, String assemblyPathname, String group, String applicationName, ApplicationLog log)

The error messages were very generic and its hard to diagnose anything with those messages. As a developer you tend to try few things like 1. Shutting down Visual Studio, 2. Restarting Biztalk, 3. Restarting IIS, 4. Stopping Virus scanners, etc etc and at one stage even restarting Windows. After doing one, two or all of the above steps, you'll be ok at some point (but you never know the root cause). The project will recompile without any issues this time.  Whenever you are experiencing this issue if you look at the GAC, the GAC will be empty (Yes! believe me, scary stuff, but that's true. I put two pictures below to show that).

You'll experience similar errors once in a while, when you have some Visual Studio "Built Events" scripts that deploy assemblies into GAC. One day I decided to spend some time and find out the root cause for the problem. SysInternals (Oops! now Microsoft's) FileMon came to rescue, I put the filter on FileMon to watch for the GAC folder and started to rebuild the assemblies, all of a sudden I started to see the process cisvc.exe popping up the screen, a bit of search revealed it the "Indexing Service" that blocks the GAC folder temporarily. I don't know whether "Indexing Service" is part of Windows XP or it got installed as part of some other updates. If in case you have it installed on your PC, that's the services you need to start and stop if you are experiencing the problem outlined in this article.

Nandri!

Saravana

Tags: | |  Categories: .NET | BizTalk General
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(9) | Comments RSS