WHAT'S NEW?
Loading...

MCSD Web Applications: Assemblies, WinMD Diagnostics and Instrumentations

Index


  • Intro
  • What are assemblies
  • Sharing assemblies
  • WinMD
  • What is instrumentation
  • References

Intro

Today we'll talk about assemblies and their relation with namespaces. How to share code between different projects in Windows 8 and what instrumentation refers to (see image below)


What are assemblies

Assemblies:
  • An assembly is a container for a set of resources and types.
  • Assemblies can be referenced by other assemblies.
  • Some assemblies are specific to certain technologies.
  • In Visual Studio, an assembly equates to a Project, so normally, you will find different namespaces in your solution, but that solution will create just a single assembly. 
Namespaces:
An example of a typical namespace is "System". At some point, you'll finish creating different namespaces like a web component, a data access layer, a windows service and all of them will be interacting between them, but not in the same place, not in the same namespace. Namespace is a logical container.

Assembly vs Namespace

Assembly Namespace
An assembly is a grouping or physical container of code A namespace is a grouping or logical container of code
Can contain one or more namespace Can span multiple files
Can be granted security permissions Can span multiple assemblies
Can be digitally signed Defines a scope
Defines a scope

You need to be careful with assemblies you want to add to your solution and you get from a web site you're not sure can trust. Even if they provide the functionality they are supposed, that assembly could have some nefarious code or be a back-door to your application creating a huge vulnerability in your app. That's why the assemblies normally are digitally signed. 

So, we got an assembly and we can mark it as strong named, which will allow us to specify a particular private key, that will be used into the compilation process. Visual Studio will inject that signature (RSA remember our previous class about encryption) into our component, so if we purchased that signature from a trusted site then you are digitally singing that assembly with your identifier so anyone can verify this assembly comes from you. It's also a prerequisite to sign your assemblies in order to install them into the GAC (Global Assembly Cache).

The GAC will help us to add "using" statements at the top of our classes which will be referencing a particular class that could be placed in the GAC folder of our computer.

Sharing assemblies

Class Libraries are the Visual Studio project type that creates an assembly that can be shared

Portable Class Libraries are the Visual Studio project type that creates an assembly that can be shared across platforms. Imagine you create some logic for a desktop application but you want to share it with an Xbox and Windows Mobile applications. 

Unfortunately, you don't have all the functionality in a desktop application for the other two projects because they use different versions of the .Net framework. You could finish using a portable class library (uses only the lowest-common-denominator), specify the projects you want to target (Xbox, mobile...) and done. Bare in mind, you need to use the shared features between those separated project, which means, the more projects you want to target the less functionalities available you will be using.

WinMD

WinMD (Windows Meta Data) applications (...or Windows RunTime component) are used specifically for Windows 8 development. It comes really handy when you want to share code among your different project applications. They create an assembly for Windows Store Apps and can be shared across languages. JavaScript cannot be used to create WinMDs (but can use other WinMD projects), only the .Net languages or C++.

Example: simple sum function defined in a Windows RunTime Component project between a Windows Store application and a JavaScript application.

  1. First, you'll need to create a new Windows Store project (XAML). Within this project add the minimum graphic components to perform a sum operation (three textboxes for each number, one for the '+' and one last box for the equal). 
  2. Now we can add the logic to perform that numerical operation but we don't want to add that business logic into our interface, we want to do it in a separate project. Create a new Windows RunTime Component Project and add the following logic to perform sum operations:
  3. namespace WindowsRuntimeComponent1
    {
        public sealed class Operator
        {
            public static double SumThemUp(double v1, double v2)
            {
                return v1 + v2;
            }
        }
    }
    
  4. Now is the time to add a call to our sum method from our Window Store project. First of all, you'll need to a reference to the Windows Runtime project into our windows store solutions, see the code below:
  5. using Windows.UI.Xaml.Navigation;
    
    namespace App1
    {
        public sealed partial class MainPage : Page
        {
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void Button_Click_1(object sender, RoutedEventArgs e)
            {
                var v1 = double.Parse(t1.Text);
                var v2 = double.Parse(t2.Text);
                t3.Text = WindowsRuntimeComponent1.Class1.SumThemUp(v1, v2);
            }
        }
    
    }
    
  6. Now we have our graphic interface (Windows Store App) and our business logic (the sum op.) split, we can add a new project and make a similar call to our plus operation. This will show you how to share assemblies and more important, how to split functionality logically. 
  7. Add a new project and select Other languages/JavaScript/Windows Store/Blank app. See how the magic of WinMD allow us to call a C# method from a JavaScript file:
  8. <!DOCTYPE html>
    
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="utf-8" />
        <title>App2</title>
    
        <script>
            function dowork() {
                t3.value = WindowsRuntimeComponent1.Class1
                    .sumThemUp(t1.value, t2.value);
            }
        </script>
    </head>
    <body>
    <input type="text" name="t1" value="1"/>
    +
    <input type="text" name="t2" value="2"/>/>
    <button onclick="dowork();">=</button>
        <input type="text" name="t3" value="0" />
    </body>
    </html>
    
    


  9. What is instrumentation

    Instrumentation is code that reports performance information. We are not interested in every part of our program, that's why we select only some important parts: a method, a db call... Analytics for your code.
    Telemetry aggregates instrumentation for analysis. In case of some applications, you can find out some menus not that accessible for the user and react changing the interface. You might want to create a baseline first time you run your code using telemetry, in order to know if the performance goes better or not.
    Diagnostics or Analysis is the use a telemetry to track causes of errors or identify trends. Finally, the main purpose is provide a great UX, by designing our interface or validating its performance.

    Performance Counter is a key point in instrumentation and it refers to sampling for a single operation. It can be the time your app takes to authenticate a user login, the time a password encryption process takes, anything. It's helpful to show the trends in a process and find out performance gaps. You need to have special permissions to use them and use counters responsibly, you don't want to take down the performance of you app while you are measuring the performance of your app. See an example of using PerformanceCounter class in a .Net app: 

    var perfCounter = new PerformanceCounter {
         CategoryName = category,
         CounterName = counter,
         MachineName = machine,
         ReadOnly = false,
    };
    perfCounter.IncrementBy(10);
    

    References



0 comments:

Post a Comment