Coding For a Living

rss

A place to share and comment about coding for a living.


VB RSS Feed Aggregator Mike McIntyre

A RSS feed aggregator aggregates syndicated web content such as online newspapers, blogs, podcasts, and video blogs (vlogs) for various purposes e.g. to provide one location for easy viewing.

The VB module provides a code sample for one way you can create a RSS feed aggregator with VisualBasic.

Module Module1
    Const feedUrl As String = "http://+:8086/VBfeeds/"


    Sub Main()
        Dim listener As New HttpListener

        listener.Prefixes.Add(feedUrl)
        listener.Start()

        'Open a browser pointing at the feeds being served.
        System.Diagnostics.Process.Start("iexplore.exe", "http://localhost:8086/VBfeeds/")

        'Serve requests.
        While True
            Dim context As HttpListenerContext = listener.GetContext()
            Dim body As XElement = GetReplyBody()
            context.Response.ContentType = "text/xml"

            Using writer As XmlWriter = New XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8)
                body.WriteTo(writer)
            End Using
        End While

    End Sub

    Function GetReplyBody() As XElement
        Dim feeds() As String = {"http://blogs.msdn.com/vbteam/rss.aspx?Tags=VB6_5F00_Migration_2F00_
                                 "http://blogs.msdn.com/vsdata/rss.xml", _
                                 "http://blogs.msdn.com/vbteam/rss.aspx?Tags=IDE&AndTags=1"}

        Return 
                   
                       VB Genius
                       http://+:8086/VBfeeds/
                       VB Team Members
                       XLinq-based RSS aggregator using VB Only Super Cool XML Literals Feature
                       
                   
               
    End Function
End Module

Glossary–One ASP.NET Mike McIntyre

clip_image002One ASP.NET - Unifies the experience of using ASP.NET technologies, so that you can easily mix and match the ones you want. Introduced in Visual Studio 2013. Introducing One ASP.NET // Hands On Lab: One ASP.NET More...


Flexible ActionResult Type Mike McIntyre

Below is a class and enum that together provide a flexible way to return an action result to a caller.

It can be used to return error or success results.

It contains multiple constructors that give the flexibility of instantiating the ActionResult object in the context of the called method and possible actions that will take place.

Instantiate before making a call to pass it into a method call or instantiate within a method.

namespace aZSoftware
{
    public enum ActionResultEnum
    {
        Unknown = 0, Success = 1, Failure = 2
    }
    public class ActionResult
    {
        public ActionResultEnum ActionResultEnum { get; set; }

        public string Message { get; set; }
        public object ResultObject { get; set; }
        public string ErrorLog { get; set; }

        public ActionResult(ActionResultEnum actionResult)
        {
            this.ActionResultEnum = actionResult;
            Message = string.Empty;
            ResultObject = null;
            ErrorLog = string.Empty;
        }

        public ActionResult(ActionResultEnum actionResult,string message)
        {
            this.ActionResultEnum = actionResult;
            Message = message;
            ResultObject = null;
            ErrorLog = string.Empty;
        }

        public ActionResult(ActionResultEnum actionResult, string message,object resultObject)
        {
            this.ActionResultEnum = actionResult;
            Message = message;
            ResultObject = resultObject;
            ErrorLog = string.Empty;
        }

        public ActionResult(ActionResultEnum actionResult, string message, object resultObject,string errorLog)
        {
            this.ActionResultEnum = actionResult;
            Message = message;
            ResultObject = resultObject;
            ErrorLog = errorLog;
        }
    }
}

Roslyn Update Mike McIntyre

Roslyn is Microsoft's effort is about re-architecting the C# and VB compliers so others can use them to create compiler services.

At Build 2014 this month Microsoft previewed language integration and made Roslyn an open source project.

Business rules engines, dynamic scripting, macros, and code refactoring are just a few of the ways Roslyn can be used.

Learn more by reading ‘Taking a tour of Rosylyn’.


Glossary–ASP.NET Scaffolding Mike McIntyre

clip_image002 

ASP.NET Scaffolding - ASP.NET Scaffolding is a code generation framework for ASP.NET Web applications. It makes it easy to add boilerplate code to your project that interacts with a data model. More...

Visual Studio 2013 – New Map Mode Scroll Bars Mike McIntyre

imageEnhanced scrollbars, to help developers be more productive, were incorporated into Visual Studio 2013.

Scrollbars can viewed in ‘map mode’.

In map mode, the traditional scroll bar is replaced by a visualization of the code in-place in the scroll position.

The visualization displays annotations,  small color markers within the scrollbar showing areas of interest within your code, such as the current caret position, code changed since last save, the position of errors or warnings or the position of breakpoints within code.

The scroll bar can be viewed in a preview mode that turns the scrollbar into a shrunk down visualization of your code in-place in the scroll window. As you scroll the visualization synchronize with your code so you can more quickly find what you are looking for.


ASP.NET Identity Mike McIntyre

imageThe ASP.NET Identity system, new in Visual Studio 2013,  is designed to replace the previous ASP.NET Membership and Simple Membership systems. It includes profile support, OAuth integration, works with OWIN, and is included with the ASP.NET templates shipped with Visual Studio 2013.

My experience with it is that it is simpler to use yet provides more options and easier extensibility.

ASP.NET Identity can be used with all of the ASP.NET frameworks, such as ASP.NET MVC, Web Forms, Web Pages, Web API, and SignalR.

Check out it’s claims based authentication. It gives you the ability to use more than just roles to grant access/permission to a user.

More….


Visual Studio 2013 Edit and Continue 64bit Mike McIntyre

 image

What’s new in Visual Studio 2013?  For one thing, an old friend – edit and continue – for 64bit native application debugging.


Use Linq with Reflection in Visual Basic Mike McIntyre

The Reflector class below generates an HTML document outlining the public API for a given assembly.

First, an XML document is emitted in memory (see Emit*()).

The document captures the relevant aspects of the assembly's metadata.

Next, an HTML document is extracted from the XML document (see Extract*()).

I love Linq.

Public Class Reflector
    Public document As XDocument

    Public Sub Reflect(ByVal assemblyFile As String)
        Dim Assembly As Assembly = Assembly.LoadFrom(assemblyFile)
        document = New XDocument(EmitAssembly(Assembly))
    End Sub

    Public Sub Transform(ByVal writer As XmlWriter)
        If document Is Nothing Then Return
        Dim assembly As XElement = document.Element("assembly")
        Dim transform As New XDocument(ExtractAssembly(assembly))
        transform.Save(writer)
    End Sub

    Public Function EmitAssembly(ByVal assembly As Assembly) As XElement
        Return 
                   
               
    End Function

    Public Function EmitNamespace(ByVal ns As String, ByVal types As IEnumerable(Of Type)) As XElement
        Return 
                   
               
    End Function

    Public Function EmitType(ByVal type As Type) As XElement
        Return < name=>
                   
                   
                   
                   
                   
                   
                   
                   
                   
               
    End Function

    Public Function EmitGenericArguments(ByVal args As IEnumerable(Of Type)) As IEnumerable(Of XElement)
        Return From arg In args _
                Select 
                           
                       
    End Function

    Public Function EmitModifiers(ByVal type As Type) As XElement
        Dim builder As StringBuilder = New StringBuilder()
        If type.IsPublic Then
            builder.Append("public")
        ElseIf type.IsNestedPublic Then
            builder.Append("public")
        ElseIf type.IsNestedFamily Then
            builder.Append("protected")
        ElseIf type.IsNestedFamANDAssem Then
            builder.Append("protected internal")
        End If
        If type.IsSealed Then builder.Append(" sealed")
        If type.IsAbstract Then builder.Append(" abstract")

        Return 
                   
               
    End Function

    Public Function EmitExtends(ByVal baseType As Type) As XElement
        If baseType Is Nothing Or baseType Is GetType(Object) Or baseType Is GetType(ValueType) Or baseType Is GetType(System.Enum) Then Return Nothing
        Return 
                   
               
    End Function

    Public Function EmitImplements(ByVal ifaces As IEnumerable(Of Type)) As IEnumerable(Of XElement)
        Return From iface In ifaces _
               Select 
                          
                      
    End Function

    Public Function EmitDeclaringType(ByVal declaringType As Type) As XElement
        If declaringType Is Nothing Then Return Nothing
        Return 
                   
               
    End Function

    Public Function EmitNestedTypes(ByVal ntypes As IEnumerable(Of Type)) As IEnumerable(Of XElement)
        Return From ntype In ntypes _
                Where GetVisible(ntype) _
                Select EmitType(ntype)
    End Function

    Public Function EmitMethods(ByVal metds As IEnumerable(Of MethodBase)) As IEnumerable(Of XElement)
        Return From metd In metds _
        Where GetVisible(metd) _
        Select 
                   
                   
                   
                   
                   
               
    End Function

    Public Function EmitProperties(ByVal props As IEnumerable(Of PropertyInfo)) As IEnumerable(Of XElement)

        Return From prop In props _
                    Where GetVisible(prop.GetGetMethod()) Or _
                    GetVisible(prop.GetSetMethod()) _
                    Select 
                               
                               
                           


    End Function

    Public Function EmitReference(ByVal type As Type) As IEnumerable(Of Object)

        If (Not type.IsGenericType) Then
            Return New Object() {New XAttribute("name", type.Name), _
                                 New XAttribute("namespace", GetNamespace(type))}
        Else
            Return New Object() {New XAttribute("name", type.Name), _
                                 New XAttribute("namespace", GetNamespace(type)), _
                                 EmitGenericArguments(type.GetGenericArguments())}
        End If
    End Function

    Public Function EmitModifiers(ByVal metd As MethodBase) As XElement
        Dim builder As StringBuilder = New StringBuilder()
        If metd.IsPublic Then
            builder.Append("public")
        ElseIf metd.IsFamily Then
            builder.Append("protected")
        ElseIf metd.IsFamilyAndAssembly Then
            builder.Append("protected internal")
        End If

        If metd.IsAbstract Then builder.Append(" abstract")
        If metd.IsStatic Then builder.Append(" static")
        If metd.IsVirtual Then builder.Append(" virtual")

        Return New XElement("modifiers", builder.ToString())
    End Function

    Public Function EmitReturnType(ByVal metd As MethodBase) As XElement
        Dim metdInfo As MethodInfo = TryCast(metd, MethodInfo)
        If metdInfo Is Nothing Then Return Nothing
        Return 
                   
               
    End Function

    Public Function EmitExtension(ByVal metd As MethodBase) As XElement
        Return 
               
    End Function

    Public Function EmitParameters(ByVal parms As IEnumerable(Of ParameterInfo)) As IEnumerable(Of XElement)
        Return From parm In parms _
               Select 
                          
                              
                          
                      
    End Function

    Public Shared Function GetNamespace(ByVal type As Type) As String
        Dim ns As String = type.Namespace
        Return If(ns IsNot Nothing, ns, String.Empty)
    End Function

    Public Shared Function GetVisible(ByVal type As Type) As Boolean
        Return type.IsPublic Or type.IsNestedPublic Or type.IsNestedFamily Or type.IsNestedFamANDAssem
    End Function

    Public Shared Function GetVisible(ByVal metd As MethodBase) As Boolean
        Return metd IsNot Nothing AndAlso (metd.IsPublic Or metd.IsFamily Or metd.IsFamilyAndAssembly)
    End Function

    Public Function ExtractAssembly(ByVal assembly As XElement) As XElement
        Return 
                   
                       
                           
                       
                   
                   
                       

Assembly:

End Function Public Function ExtractNamespace(ByVal ns As XElement) As XElement Return

Namespace:

End Function Public Function ExtractType(ByVal type As XElement) As XElement Return

End Function Public Function ExtractModifiers(ByVal element As XElement) As String Return element.Element("modifiers").Value End Function Public Function ExtractName(ByVal element As XElement) As String Dim name As String = element.Attribute("name").Value Dim i As Integer = name.LastIndexOf("`") If i > 0 Then name = name.Substring(0, i) ' fix generic name Return name End Function Public Function ExtractGenericArguments(ByVal element As XElement) As String If Not element.Elements("genericArgument").Any() Then Return String.Empty Dim builder As StringBuilder = New StringBuilder("<") For Each genericArgument As XElement In element.Elements("genericArgument") If builder.Length <> 1 Then builder.Append(", ") builder.Append(ExtractReference(genericArgument)) Next builder.Append(">") Return builder.ToString() End Function Public Function ExtractReference(ByVal element As XElement) As String Return ExtractName(element) + ExtractGenericArguments(element) End Function Public Function ExtractInherits(ByVal type As XElement) As String If Not type.Elements("extends").Concat(type.Elements("implements")).Any() Then Return String.Empty Dim builder As New StringBuilder() For Each [inherits] As XElement In type.Elements("extends").Concat(type.Elements("implements")) If builder.Length = 0 Then builder.Append(" : ") Else builder.Append(", ") End If builder.Append(ExtractReference([inherits])) Next Return builder.ToString() End Function Public Function ExtractConstructors(ByVal type As XElement) As XElement Dim ctors = From ctor In type.Elements("method") _ Where ExtractName(ctor) = ".ctor" _ Select
  • If Not ctors.Any() Then Return Nothing Return

    Constructors:

      End Function Public Function ExtractProperties(ByVal type As XElement) As XElement Dim props = From prop In type.Elements("property") _ Let propName = ExtractName(prop) _ Let getter = "get_" + propName _ Let setter = "set_" + propName _ Select
      If Not props.Any() Then Return Nothing Return

      Properties:

      End Function Public Function ExtractOperators(ByVal type As XElement) As XElement Dim ops = From op In type.Elements("method") _ Let name = ExtractName(op) _ Where name.StartsWith("op_") _ Select
      If Not ops.Any() Then Return Nothing Return

      Operators:

      End Function Public Function ExtractMethods(ByVal type As XElement) As XElement Dim metds = From metd In type.Elements("method") _ Let name = ExtractName(metd) _ Where name <> ".ctor" And _ Not type.Elements("property").Where(Function(prop) name = "get_" & ExtractName(prop) Or _ name = "set_" + ExtractName(prop)).Any() And Not name.StartsWith("op_") _ Select ExtractMethod(metd) If Not metds.Any() Then Return Nothing Return New XElement("div", New XElement("h4", "Methods: "), New XElement("ul", metds)) End Function Public Function ExtractMethod(ByVal metd As XElement) As XElement Return
    • End Function Public Function ExtractParameters(ByVal metd As XElement) As String Dim builder As StringBuilder = New StringBuilder("(") For Each parm As XElement In metd.Elements("parameter") If builder.Length = 1 Then If metd.Element("extension") IsNot Nothing Then builder.Append("this ") Else builder.Append(", ") End If builder.Append(ExtractReference(parm.Element("parameterType"))) builder.Append(" ") builder.Append(ExtractName(parm)) Next builder.Append(")") Return builder.ToString() End Function End Class