Monday, January 18, 2010

Fiddler and ASP.NET Development Server

ASP.NET development server only accept urls that starts with localhost. Therefor when your request comes from fiddler it become 127.0.0.1 and ASP.NET development server will reject it.
To enable fiddler to work open from menu TOOLS | Hosts and enter

127.0.0.1 localhost


Now, every address from 127.0.0.1 will become localhost just what ASP.NET development servers like :)

Submit this story to DotNetKicks

Friday, January 15, 2010

Write c# code instead of JavaScript

Very, very interesting

http://sharpkit.net/

Submit this story to DotNetKicks

Wednesday, December 23, 2009

Flatten array of arrays using linq

If you have list of object that contains list of other objects and you want to collect all children in one list linq can make it simple. For example:


List<int> listOfInt = new List<int>();
List<List<int>> listOfListOfInts = new List<List<int>>();
foreach (List<int> ints in listOfListOfInts)
{
foreach (int i in ints)
{
listOfInt.Add(i);
}
}


but using linq the following statements become:


List<int> listOfInt = listOfListOfInts.SelectMany(ints => ints).ToList();

Submit this story to DotNetKicks

Thursday, December 17, 2009

"Missing XML comment..." in service reference file

When I added service reference in my project I have got a lot of "Missing XML comment for public visible type..." warning messages in Reference.cs file. Those warnings was didn't allow me to see real important warning messages. To disable this add


#pragma warning disable 1591


at the beginning of Resource.cs file and


#pragma warning restore 1591


at the end of Resource.cs file.

And off course you will need to add this every time you update reference.

Submit this story to DotNetKicks

Use svn revision number for assembly version

If you want to use svn revision number as revision number of assembly version this post can be helpful.
I have used information from http://carlosrivero.com/add-svn-revision-macro-for-visual-studio-2005-2008 and http://stackoverflow.com/questions/1157485/whats-regular-expression-for-update-assembly-build-number-in-assemblyinfo-cs-fil to create macro for this.

Here is the macro:



Public Sub UpdateRevisionNumber()


        Dim proj As Project


        Dim objProj As Object()


        objProj = DTE.ActiveSolutionProjects


 


        If objProj.Length = 0 Then


            Exit Sub


        End If


        proj = DTE.ActiveSolutionProjects(0) 'gets the path of the web project to write to the web.config


        Dim x As String = proj.ProjectItems.ContainingProject.FullName


 


        Dim p As New System.Diagnostics.Process


        'first update the root to get the latest revision


        p.StartInfo.UseShellExecute = False


        p.StartInfo.RedirectStandardOutput = True


        p.StartInfo.RedirectStandardError = True


        p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden


        p.StartInfo.FileName = "svn.exe"


        p.StartInfo.WorkingDirectory = proj.Properties.Item("FullPath").Value()


        p.StartInfo.Arguments = "info"


        p.Start()


        Dim strerror As String = p.StandardError().ReadToEnd()


        Dim output As String = p.StandardOutput().ReadToEnd()


        p.WaitForExit()


        'get the revision


        Dim re As New Regex("Revision:\s+(\d+)\s*")


        Dim ver As String


        ver = re.Match(output).Groups(1).Value


        'add one so the number is not one revision behind


        Dim version As Integer


        version = Convert.ToInt32(ver) + 1


 


        Dim propPath As String


        propPath = proj.Properties.Item("FullPath").Value() + "Properties\AssemblyInfo.cs"


 


        Dim globalAssemblyContent As String = File.ReadAllText(propPath)


        Dim rVersionAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")


        Dim rVersionInfoAttribute As Regex = New Regex("\[[\s]*(\/\*[\s\S]*?\*\/)?[\s]*assembly[\s]*(\/\*[\s\S]*?\*\/)?[\s]*:[\s]*(\/\*[\s\S]*?\*\/)?[\s]*AssemblyFileVersion[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\([\s]*(\/\*[\s\S]*?\*\/)?[\s]*\""([0-9]+)\.([0-9]+)(.([0-9]+))?(.([0-9]+))?\""[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\)[\s]*(\/\*[\s\S]*?\*\/)?[\s]*\]")


 


        'Find Version Attribute for Updating Build Number


        Dim mVersionAttributes As MatchCollection = rVersionAttribute.Matches(globalAssemblyContent)


        Dim mVersionAttribute As Match = GetFirstUnCommentedMatch(mVersionAttributes, globalAssemblyContent)


        Dim gBuildNumber As Group = mVersionAttribute.Groups(11)


        Dim newBuildNumber As String


 


        'Replace Version Attribute for Updating Build Number


        If (gBuildNumber.Success) Then


            newBuildNumber = version.ToString()


            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber.Index + gBuildNumber.Length)


        End If


 


        'Find Version Info Attribute for Updating Build Number


        Dim mVersionInfoAttributes As MatchCollection = rVersionInfoAttribute.Matches(globalAssemblyContent)


        Dim mVersionInfoAttribute As Match = GetFirstUnCommentedMatch(mVersionInfoAttributes, globalAssemblyContent)


        Dim gBuildNumber2 As Group = mVersionInfoAttribute.Groups(11)


 


        'Replace AssemblyFileVersion


        If (gBuildNumber2.Success) Then


            If String.IsNullOrEmpty(newBuildNumber) Then


                newBuildNumber = version.ToString()


            End If


 


            globalAssemblyContent = globalAssemblyContent.Substring(0, gBuildNumber2.Index) + newBuildNumber + globalAssemblyContent.Substring(gBuildNumber2.Index + gBuildNumber2.Length)


        End If


 


        File.WriteAllText(propPath, globalAssemblyContent)


    End Sub


 


 


    Private Function GetFirstUnCommentedMatch(ByRef mc As MatchCollection, ByVal content As String) As Match


        Dim rSingleLineComment As Regex = New Regex("\/\/.*$")


        Dim rMultiLineComment As Regex = New Regex("\/\*[\s\S]*?\*\/")


 


        Dim mSingleLineComments As MatchCollection = rSingleLineComment.Matches(content)


        Dim mMultiLineComments As MatchCollection = rMultiLineComment.Matches(content)


 


        For Each m As Match In mc


            If m.Success Then


                For Each singleLine As Match In mSingleLineComments


                    If singleLine.Success Then


                        If m.Index >= singleLine.Index And m.Index + m.Length <= singleLine.Index + singleLine.Length Then


                            GoTo NextAttribute


                        End If


                    End If


                Next


 


                For Each multiLine As Match In mMultiLineComments


                    If multiLine.Success Then


                        If m.Index >= multiLine.Index And m.Index + m.Length <= multiLine.Index + multiLine.Length Then


                            GoTo NextAttribute


                        End If


                    End If


                Next


 


                Return m


            End If


NextAttribute:


        Next


 


        Return Nothing


 


    End Function


Submit this story to DotNetKicks

Monday, November 30, 2009

Unable to find service IVsDiscoveryService

I had a problem with Visual Studio 2008. When I try to update service reference I have got following error message: "Unable to find service IVsDiscoveryService"...

To solve this do the following:
Open command prompt, go to the devenv folder (usually C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE) ande type following command:

devenv /resetskippkgs


You can save some time for finding visual studio disk and reinstalling visual studio.

Submit this story to DotNetKicks

Friday, November 20, 2009

Visual Studio macro for opening file from unit test folder

My test creates log file in current test execution folder dir. And sometime I need to open that file to see content. So, I made macro in visual studio for that:


Public Sub OpenLog()
Dim solutionPath As String
Dim solutionDir As String
Dim dirInSolution As String()

solutionPath = DTE.Solution.FullName
solutionDir = Path.GetDirectoryName(solutionPath)
solutionPath = solutionDir + "\TestResults"
dirInSolution = Directory.GetDirectories(solutionPath)

Dim lastTestDir As String
Dim currentDT As Date

For Each currentDir As String In dirInSolution
Dim ct As Date
ct = Directory.GetCreationTime(currentDir)
If (ct > currentDT) Then
currentDT = ct
lastTestDir = currentDir
End If
Next

' we have dir name
' open log file from that dir
Dim finalPath = lastTestDir + "\Out\trace.log"
DTE.ItemOperations.OpenFile(finalPath, ViewKind:=EnvDTE.Constants.vsViewKindTextView).Activate()
End Sub

Submit this story to DotNetKicks

Sunday, November 15, 2009

Using linq to select nodes in tree structure

If we have tree structure of object like this:

class C1
{
public List Children {get; set;}
public string Name {get; set;}
}


and have list of C1 object for example "listOfC1" we can use Linq to select object that match specific name path, for example "N1/N2/N3":


var result =
from c1 in listOfC1
where c1.Name == "N1"
from c2 in c1.Children
where c2.Name == "N2"
from c3 in c2.Children
where c3.Name == "N3"
select c3;

Submit this story to DotNetKicks