Start all services filtering by Name and StartMode / StartupType using PowerShell

Sometimes I need to start or restart a lot of services belonging to my application, all having the product / company name as prefix, and I would like to start it all, but not the ones that are flagged as Manual.

To simple start all the services that match a prefix you would do something like this:

Start-Service "MyProduct*"

but when you need to start the service that are not flagged as Manual startup, there’s no command/filter directly in powershell command, but you need to use Wmi objects in this way:

Get-WmiObject win32_service | where { $_.Name -like "MyProduct*" -and $_.StartMode -ne "Manual" } | Start-Service

With the same syntax you can of course, do Get-Service, Stop-Service, and all the other related commands.

RabbitMQ: Completely Hard Reset a RabbitMQ Server Installation

clean-rabbitmq

Probably the most exciting thing of this post is the funny image of RabbitMQ’s cleaner :)

Yes, you can also reset the RabbitMQ instance, by running these commands:

rabbitmqctl.bat stop_app

rabbitmqctl.bat reset

rabbitmqctl.bat start_app

soft-reset-rabbitmq

…but sometimes could be necessary to clean up stuff, especially when you have played around cluster, multiple node on the same machine, ad so on.

It took me a while to figure out who was maintaining a reference to the %appdata%\RabbitMQ folder, even after I uninstalled Erlang, so here’s the simple steps:

  1. Uninstall RabbitMQ Server
  2. Kill the process epmd.exe (this was the process maintaning a reference to the RabbitMQ folder)
    rabbitmq-epmd-process
  3. Delete the folder RabbitMQ that is under %appdata%
    rabbitmq-folder-wont-delete If you also want to change the erlang cookie, you will need to delete also the file “.erlang.cookie” that you find under your users folder %userprofile%
    rabbitmq-erlang-cookie If you have a cluster and remove .erlang.cookie the node would no be able to rejoin the cluster
  4. Reinstall RabbitMQ Server

 

That’s all folks!

Use Microsoft App Studio to create professional Apps for Windows and Windows Phone

Originally posted on Robin Sedlaczek's Blog:

Introduction

In 2013 Microsoft published the App Studio Beta (http://appstudio.windows.com/). With the App Studio web application, it is very easy to create Windows and Windows Phone applications. You do not need to write a line of code to create professional apps. In this post I like to give a short overview of App Studio, how easy it is to create apps and the code behind your apps.

Overview

Once you have a Microsoft account, you can log on the App Studio and start creating projects. The image below shows the My projects page where all your apps are listed. You can create new apps, edit existing apps or delete them.

Image

App Studio Project List

To create a new app, simply click the “Start new project” button. The following page let you select a template for your app. There is a bunch of layout templates you can use depending on your…

View original 1,717 more words

Software on the hosted build server

Took directly from visual studio site: Hosted build controller.

You can check the detail list, and install the visual studio extension “Avanade Extensions for VS2012” (VS2013 version) to track the software installed on the hosted build server using the Software Inventory functionality.

The hosted build server is deployed with the following software:

  • Windows Server 2012 R2, 64-bit environment, with Windows PowerShell
  • Team Foundation Build (Team Foundation Server 2013)
  • Visual Studio
    • Visual Studio 2013 Update 2 RC
    • Visual Studio SDK 2013 RTM
    • Visual Studio 2012 Ultimate Update 4
    • Visual Studio SDK 2012 RTM
    • Visual Studio 2010 SP1
  • The .NET Framework
    • .NET 4.5.1
    • .NET 4.5
    • .NET 3.5 SP1
  • Microsoft Azure
    • SDK 2.3
    • SDK 2.2
    • SDK 2.1
    • SDK 2.0
    • SDK 1.8
    • SDK 1.7
  • Other components
    • Apache ANT 1.9.3
    • Apache Maven 3.1.1
    • Java Standard Edition Development Kit 1.7 U51
    • Microsoft Office Developer Tools for Visual Studio 2013 Update 1
    • Node.js 0.10.26
    • Node.js Tools 1.0 Alpha for Visual Studio 2013
    • SharePoint 2010 and SharePoint 2013
    • SQL Server Data Tools for Visual Studio 2010, Visual Studio 2012, and Visual Studio 2013
    • TFS Build Extensions
    • TypeScript 1.0
    • WIX Toolset 3.7
    • Web Deploy 3.0
    • Windows Phone SDK 8.0

 

RabbitMQ: Change the Hostname

rabbitmq-change-hostname

If you change the name of the machine where RabbitMQ server is installed, you will see that the service will no longer be startable, this because RabbitMQ relays on the machine name to create and connect to the node, even if it is local.

To fix this problem, you just have to reinstall the rabbitmq windows service, by using the proper command line utility rabbitmq-service.bat

*********************
Service control usage
*********************

rabbitmq-service help    - Display this help
rabbitmq-service install - Install the RabbitMQ service
rabbitmq-service remove  - Remove the RabbitMQ service

The following actions can also be accomplished by using
Windows Services Management Console (services.msc):

rabbitmq-service start   - Start the RabbitMQ service
rabbitmq-service stop    - Stop the RabbitMQ service
rabbitmq-service disable - Disable the RabbitMQ service
rabbitmq-service enable  - Enable the RabbitMQ service
  1. First start a command prompt as administrator and go to the rabbitmq-server-x sbin folder
    cd %programfiles(x86)%\RabbitMQ Server\rabbitmq_server-3.3.2\sbin
  2. Uninstall the service
    rabbitmq-service.bat remove
  3. Install the service
    rabbitmq-service.bat install

This should recreate the service and configure it to start the node with the correct name.

If this doesn’t work you can follow the instruction on how to completely reset a rabbitmq server installation.

Developing against Service Bus for Windows 1.1

Originally posted on Roysvork:

Wouldn’t it be great if we could work on applications that leverage Microsoft Service Bus locally without having to connect to and potentially pay for Microsoft Azure?

Not everyone knows this, but there’s a local counterpart to Microsoft Azure Service Bus in the form of Service Bus for Windows. Those that do know about it know that it doesn’t have a great development story and can be a pain in the arse to set up.

In this post we’ll take the sting out of the process and show you how you can get your local environment set up so you and the rest of your team can develop against Service Bus without using any Microsoft Azure services.

You’ll need an instance of SQL server to which you have admin rights to use Service Bus for Windows.

IMPORTANT: If you have any other service running that use the default AMQP ports 5671, & 5672 then the configuration…

View original 1,331 more words

Azure Public IP Addresses

If you like me you’re interested to know the ip ranges that windows azure uses to connect to the internet :) this is the right post ;)

Do not assume that traffic originating from these IP address ranges is trustworthy.

You can download Windows Azure Datacenter IP Ranges xml from the download center.

Here’s the list updated on 12th June 2014

<?xml version="1.0" encoding="utf-8"?>
<AzurePublicIpAddresses xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Region Name="europewest">
    <IpRange Subnet="157.55.9.112/28" />
    <IpRange Subnet="157.55.10.0/26" />
    <IpRange Subnet="65.52.128.0/19" />
    <IpRange Subnet="94.245.97.0/24" />
    <IpRange Subnet="137.116.192.0/19" />
    <IpRange Subnet="157.55.8.64/26" />
    <IpRange Subnet="157.55.8.128/27" />
    <IpRange Subnet="157.55.8.160/28" />
    <IpRange Subnet="168.63.0.0/19" />
    <IpRange Subnet="168.63.96.0/19" />
    <IpRange Subnet="193.149.80.0/22" />
    <IpRange Subnet="213.199.128.0/21" />
    <IpRange Subnet="213.199.136.0/22" />
    <IpRange Subnet="213.199.180.32/28" />
    <IpRange Subnet="213.199.180.96/27" />
    <IpRange Subnet="213.199.180.192/26" />
    <IpRange Subnet="213.199.183.0/24" />
    <IpRange Subnet="23.97.128.0/17" />
    <IpRange Subnet="23.100.0.0/20" />
    <IpRange Subnet="104.40.128.0/19" />
    <IpRange Subnet="137.117.128.0/17" />
    <IpRange Subnet="168.61.56.0/21" />
    <IpRange Subnet="191.233.64.0/18" />
    <IpRange Subnet="191.237.232.0/27" />
    <IpRange Subnet="191.237.232.64/26" />
    <IpRange Subnet="191.237.233.0/24" />
    <IpRange Subnet="191.237.234.0/23" />
    <IpRange Subnet="193.149.84.0/22" />
  </Region>
  <Region Name="useast">
    <IpRange Subnet="23.96.0.0/18" />
    <IpRange Subnet="23.96.64.0/28" />
    <IpRange Subnet="23.96.64.64/26" />
    <IpRange Subnet="23.96.64.128/27" />
    <IpRange Subnet="23.96.64.160/28" />
    <IpRange Subnet="23.96.80.0/20" />
    <IpRange Subnet="23.96.96.0/19" />
    <IpRange Subnet="23.100.16.0/20" />
    <IpRange Subnet="137.116.112.0/20" />
    <IpRange Subnet="137.117.32.0/19" />
    <IpRange Subnet="137.117.64.0/18" />
    <IpRange Subnet="137.135.64.0/18" />
    <IpRange Subnet="157.56.176.0/21" />
    <IpRange Subnet="168.61.32.0/20" />
    <IpRange Subnet="168.61.48.0/21" />
    <IpRange Subnet="168.62.32.0/19" />
    <IpRange Subnet="168.62.160.0/19" />
    <IpRange Subnet="138.91.96.0/25" />
    <IpRange Subnet="138.91.96.128/26" />
    <IpRange Subnet="138.91.96.192/28" />
    <IpRange Subnet="138.91.112.0/20" />
    <IpRange Subnet="191.234.32.0/19" />
    <IpRange Subnet="191.236.0.0/19" />
    <IpRange Subnet="191.238.0.0/25" />
    <IpRange Subnet="191.238.0.128/26" />
    <IpRange Subnet="191.238.0.192/27" />
    <IpRange Subnet="191.238.1.0/24" />
    <IpRange Subnet="191.238.2.0/23" />
    <IpRange Subnet="191.238.4.0/24" />
    <IpRange Subnet="191.238.8.0/21" />
    <IpRange Subnet="191.238.16.0/20" />
    <IpRange Subnet="191.238.32.0/19" />
  </Region>
  <Region Name="uswest">
    <IpRange Subnet="23.100.32.0/20" />
    <IpRange Subnet="137.116.184.0/21" />
    <IpRange Subnet="137.117.0.0/19" />
    <IpRange Subnet="137.117.208.128/28" />
    <IpRange Subnet="137.135.0.0/18" />
    <IpRange Subnet="138.91.64.0/19" />
    <IpRange Subnet="157.56.160.0/21" />
    <IpRange Subnet="168.61.0.0/19" />
    <IpRange Subnet="168.61.64.0/20" />
    <IpRange Subnet="168.62.0.0/19" />
    <IpRange Subnet="168.62.192.0/19" />
    <IpRange Subnet="168.63.88.0/24" />
    <IpRange Subnet="23.99.64.0/19" />
    <IpRange Subnet="138.91.128.0/24" />
    <IpRange Subnet="138.91.129.0/26" />
    <IpRange Subnet="138.91.129.64/28" />
    <IpRange Subnet="138.91.136.0/21" />
    <IpRange Subnet="138.91.144.0/20" />
    <IpRange Subnet="138.91.160.0/19" />
    <IpRange Subnet="138.91.192.0/21" />
    <IpRange Subnet="138.91.224.0/19" />
    <IpRange Subnet="191.238.70.0/23" />
    <IpRange Subnet="23.99.0.0/19" />
    <IpRange Subnet="23.99.32.0/25" />
    <IpRange Subnet="23.99.32.128/28" />
    <IpRange Subnet="23.99.33.0/28" />
    <IpRange Subnet="23.99.34.0/25" />
    <IpRange Subnet="23.99.34.128/26" />
    <IpRange Subnet="23.99.34.192/27" />
    <IpRange Subnet="23.99.35.0/24" />
    <IpRange Subnet="23.99.36.0/24" />
    <IpRange Subnet="23.99.37.0/26" />
    <IpRange Subnet="23.99.37.80/28" />
    <IpRange Subnet="23.99.38.0/24" />
    <IpRange Subnet="23.99.40.0/24" />
    <IpRange Subnet="23.99.48.0/20" />
    <IpRange Subnet="65.52.112.0/20" />
    <IpRange Subnet="104.40.0.0/19" />
    <IpRange Subnet="168.63.89.0/25" />
    <IpRange Subnet="168.63.89.128/26" />
    <IpRange Subnet="191.236.64.0/18" />
    <IpRange Subnet="191.239.0.0/18" />
  </Region>
  <Region Name="usnorth">
    <IpRange Subnet="23.100.72.0/21" />
    <IpRange Subnet="65.52.0.0/19" />
    <IpRange Subnet="65.52.192.0/19" />
    <IpRange Subnet="65.52.48.0/20" />
    <IpRange Subnet="209.240.220.0/23" />
    <IpRange Subnet="65.52.106.16/28" />
    <IpRange Subnet="65.52.106.32/27" />
    <IpRange Subnet="65.52.106.64/26" />
    <IpRange Subnet="65.52.106.128/25" />
    <IpRange Subnet="65.52.107.0/28" />
    <IpRange Subnet="65.52.232.0/21" />
    <IpRange Subnet="65.52.240.0/21" />
    <IpRange Subnet="157.55.24.0/21" />
    <IpRange Subnet="157.55.60.224/27" />
    <IpRange Subnet="157.55.73.32/28" />
    <IpRange Subnet="157.55.136.0/21" />
    <IpRange Subnet="157.55.151.0/28" />
    <IpRange Subnet="157.55.160.0/20" />
    <IpRange Subnet="157.55.208.0/20" />
    <IpRange Subnet="157.55.252.0/22" />
    <IpRange Subnet="157.56.8.0/21" />
    <IpRange Subnet="157.56.24.160/27" />
    <IpRange Subnet="157.56.24.192/28" />
    <IpRange Subnet="157.56.28.0/22" />
    <IpRange Subnet="168.62.96.0/19" />
    <IpRange Subnet="168.62.224.0/20" />
    <IpRange Subnet="168.62.240.0/21" />
    <IpRange Subnet="168.62.248.0/22" />
    <IpRange Subnet="207.46.192.0/20" />
    <IpRange Subnet="23.96.128.0/17" />
    <IpRange Subnet="23.98.48.0/25" />
    <IpRange Subnet="23.98.48.128/26" />
    <IpRange Subnet="23.98.48.192/27" />
    <IpRange Subnet="23.98.48.224/28" />
    <IpRange Subnet="23.98.49.0/24" />
    <IpRange Subnet="23.98.50.0/23" />
    <IpRange Subnet="23.98.52.0/23" />
    <IpRange Subnet="23.98.54.0/24" />
    <IpRange Subnet="23.98.55.0/26" />
    <IpRange Subnet="191.233.128.0/20" />
    <IpRange Subnet="191.236.128.0/18" />
  </Region>
  <Region Name="europenorth">
    <IpRange Subnet="65.52.64.0/20" />
    <IpRange Subnet="65.52.224.0/21" />
    <IpRange Subnet="65.52.248.0/21" />
    <IpRange Subnet="94.245.88.0/21" />
    <IpRange Subnet="94.245.104.0/21" />
    <IpRange Subnet="94.245.112.0/20" />
    <IpRange Subnet="137.116.224.0/19" />
    <IpRange Subnet="157.55.3.0/24" />
    <IpRange Subnet="157.55.230.160/27" />
    <IpRange Subnet="168.61.80.0/20" />
    <IpRange Subnet="168.61.96.0/19" />
    <IpRange Subnet="168.63.32.0/19" />
    <IpRange Subnet="168.63.64.0/20" />
    <IpRange Subnet="168.63.80.0/21" />
    <IpRange Subnet="168.63.92.0/22" />
    <IpRange Subnet="193.149.88.0/21" />
    <IpRange Subnet="23.100.48.0/20" />
    <IpRange Subnet="23.102.0.0/18" />
    <IpRange Subnet="137.135.128.0/17" />
    <IpRange Subnet="138.91.48.0/20" />
    <IpRange Subnet="191.235.128.0/18" />
    <IpRange Subnet="191.235.192.0/24" />
    <IpRange Subnet="191.235.193.0/27" />
    <IpRange Subnet="191.235.193.64/27" />
    <IpRange Subnet="191.235.193.96/28" />
    <IpRange Subnet="191.235.194.0/23" />
    <IpRange Subnet="191.235.208.0/20" />
    <IpRange Subnet="191.235.255.0/26" />
    <IpRange Subnet="191.235.255.64/27" />
    <IpRange Subnet="191.235.255.128/25" />
    <IpRange Subnet="191.237.192.0/23" />
    <IpRange Subnet="191.237.194.0/24" />
    <IpRange Subnet="191.237.208.0/20" />
    <IpRange Subnet="191.238.96.0/19" />
    <IpRange Subnet="191.239.208.0/20" />
  </Region>
  <Region Name="asiaeast">
    <IpRange Subnet="23.98.32.0/21" />
    <IpRange Subnet="23.98.40.0/22" />
    <IpRange Subnet="23.100.88.0/21" />
    <IpRange Subnet="65.52.160.0/19" />
    <IpRange Subnet="111.221.64.0/22" />
    <IpRange Subnet="111.221.69.0/25" />
    <IpRange Subnet="134.170.192.0/21" />
    <IpRange Subnet="137.116.160.0/20" />
    <IpRange Subnet="168.63.128.0/19" />
    <IpRange Subnet="168.63.192.0/19" />
    <IpRange Subnet="207.46.67.160/27" />
    <IpRange Subnet="207.46.67.192/27" />
    <IpRange Subnet="207.46.72.0/26" />
    <IpRange Subnet="207.46.77.224/28" />
    <IpRange Subnet="207.46.87.0/24" />
    <IpRange Subnet="207.46.89.16/28" />
    <IpRange Subnet="207.46.95.32/27" />
    <IpRange Subnet="207.46.128.0/19" />
    <IpRange Subnet="23.97.64.0/20" />
    <IpRange Subnet="23.97.80.0/28" />
    <IpRange Subnet="23.99.96.0/19" />
    <IpRange Subnet="191.234.2.16/28" />
    <IpRange Subnet="191.234.2.32/27" />
    <IpRange Subnet="191.234.2.64/26" />
    <IpRange Subnet="191.234.2.128/26" />
    <IpRange Subnet="191.234.3.0/24" />
    <IpRange Subnet="191.234.16.0/20" />
  </Region>
  <Region Name="asiasoutheast">
    <IpRange Subnet="23.97.48.0/20" />
    <IpRange Subnet="23.100.112.0/21" />
    <IpRange Subnet="111.221.16.0/21" />
    <IpRange Subnet="111.221.80.0/20" />
    <IpRange Subnet="111.221.96.0/20" />
    <IpRange Subnet="137.116.128.0/19" />
    <IpRange Subnet="138.91.32.0/20" />
    <IpRange Subnet="168.63.160.0/19" />
    <IpRange Subnet="168.63.224.0/19" />
    <IpRange Subnet="207.46.48.0/20" />
    <IpRange Subnet="23.98.64.0/20" />
    <IpRange Subnet="168.63.90.0/27" />
    <IpRange Subnet="168.63.90.32/28" />
    <IpRange Subnet="168.63.90.64/27" />
    <IpRange Subnet="191.238.64.0/25" />
    <IpRange Subnet="191.238.64.128/27" />
    <IpRange Subnet="191.238.64.160/28" />
    <IpRange Subnet="207.46.224.0/20" />
  </Region>
  <Region Name="ussouth">
    <IpRange Subnet="65.52.32.0/21" />
    <IpRange Subnet="65.54.48.0/22" />
    <IpRange Subnet="65.54.52.0/26" />
    <IpRange Subnet="65.54.52.64/27" />
    <IpRange Subnet="65.54.52.128/25" />
    <IpRange Subnet="65.54.53.0/24" />
    <IpRange Subnet="65.54.54.0/23" />
    <IpRange Subnet="65.55.64.0/20" />
    <IpRange Subnet="65.55.80.0/21" />
    <IpRange Subnet="65.55.88.0/22" />
    <IpRange Subnet="65.55.92.0/23" />
    <IpRange Subnet="65.55.94.0/24" />
    <IpRange Subnet="65.55.95.0/26" />
    <IpRange Subnet="65.55.95.64/27" />
    <IpRange Subnet="65.55.95.128/25" />
    <IpRange Subnet="70.37.48.0/20" />
    <IpRange Subnet="70.37.64.0/19" />
    <IpRange Subnet="70.37.96.0/20" />
    <IpRange Subnet="70.37.112.0/22" />
    <IpRange Subnet="70.37.116.0/23" />
    <IpRange Subnet="70.37.118.0/24" />
    <IpRange Subnet="70.37.119.64/26" />
    <IpRange Subnet="70.37.119.128/26" />
    <IpRange Subnet="70.37.119.208/28" />
    <IpRange Subnet="70.37.119.224/27" />
    <IpRange Subnet="70.37.120.0/22" />
    <IpRange Subnet="70.37.124.0/23" />
    <IpRange Subnet="70.37.126.0/26" />
    <IpRange Subnet="70.37.126.64/27" />
    <IpRange Subnet="70.37.126.128/25" />
    <IpRange Subnet="70.37.127.0/26" />
    <IpRange Subnet="70.37.127.240/28" />
    <IpRange Subnet="70.37.160.0/21" />
    <IpRange Subnet="157.55.80.0/21" />
    <IpRange Subnet="157.55.103.32/27" />
    <IpRange Subnet="157.55.153.224/28" />
    <IpRange Subnet="157.55.176.0/20" />
    <IpRange Subnet="157.55.192.0/21" />
    <IpRange Subnet="157.55.200.0/22" />
    <IpRange Subnet="168.62.128.0/19" />
    <IpRange Subnet="23.98.128.0/19" />
    <IpRange Subnet="23.98.160.0/23" />
    <IpRange Subnet="23.98.162.0/28" />
    <IpRange Subnet="23.98.162.32/28" />
    <IpRange Subnet="23.98.162.64/26" />
    <IpRange Subnet="23.98.162.128/28" />
    <IpRange Subnet="23.98.162.176/28" />
    <IpRange Subnet="23.98.164.0/23" />
    <IpRange Subnet="23.98.167.0/24" />
    <IpRange Subnet="23.98.169.0/24" />
    <IpRange Subnet="23.98.176.0/20" />
    <IpRange Subnet="23.98.192.0/25" />
    <IpRange Subnet="23.98.208.0/20" />
    <IpRange Subnet="23.98.255.0/27" />
    <IpRange Subnet="23.98.255.32/28" />
    <IpRange Subnet="23.98.255.64/26" />
    <IpRange Subnet="23.100.120.0/21" />
    <IpRange Subnet="23.102.128.0/18" />
    <IpRange Subnet="191.238.128.0/19" />
    <IpRange Subnet="191.238.160.0/20" />
    <IpRange Subnet="191.238.224.0/19" />
  </Region>
  <Region Name="japanwest">
    <IpRange Subnet="23.98.56.0/26" />
    <IpRange Subnet="23.98.56.64/27" />
    <IpRange Subnet="23.98.56.96/28" />
    <IpRange Subnet="23.98.56.128/26" />
    <IpRange Subnet="23.100.104.0/21" />
    <IpRange Subnet="138.91.16.0/20" />
    <IpRange Subnet="191.233.32.0/19" />
    <IpRange Subnet="191.237.236.0/24" />
    <IpRange Subnet="191.238.80.0/20" />
  </Region>
  <Region Name="japaneast">
    <IpRange Subnet="23.98.57.0/25" />
    <IpRange Subnet="23.98.57.128/27" />
    <IpRange Subnet="23.100.96.0/21" />
    <IpRange Subnet="138.91.0.0/20" />
    <IpRange Subnet="191.234.138.0/24" />
    <IpRange Subnet="23.102.64.0/20" />
    <IpRange Subnet="191.237.240.0/28" />
    <IpRange Subnet="191.237.241.0/24" />
  </Region>
  <Region Name="brazilsouth">
    <IpRange Subnet="23.97.96.0/20" />
    <IpRange Subnet="23.97.112.0/25" />
    <IpRange Subnet="23.97.112.128/28" />
    <IpRange Subnet="191.237.248.0/21" />
  </Region>
</AzurePublicIpAddresses>

Linq Take Random – Query to Get a Random Sub Collection – Random Order

While writing a load test today, I had to simulate different kind of customers with different kind of taste, and I had a collection of item ids, and to simulate a random distribution, I had to generate a sub collection taking random ids from the collection source, in that way I can simulate different kind of customers that will end-up querying the server requesting different item details.

Googling a little-bit I found an easy way to do this, and is to Order the collection by a different Guid for each row:

var result = collection.OrderBy(t => Guid.NewGuid());

and to get (take) 10 random elements:

var result = collection.OrderBy(t => Guid.NewGuid()).Take(10);

From the Scott Mitchell’s post, you can see some empirical tests that proves a nice random distribution. Of course this is not to use in production code where you absolutely need an unpredictable results, but for load testing, and other take-it-easy purpose, that’s cool.

This is a LINQPad example:

void Main()
{
	var collection = Enumerable.Range(1, 100).ToList();
	
	for (int i = 0; i < 3; i++)
	{
		collection.OrderBy(t => Guid.NewGuid()).Take(10).Dump();
	}
}

linq-take-random

Convert XML to C# Classes

Today I found a cool Visual Studio 2012/2013 functionality: you can paste an XML source as Classes, in fact creating all the object model to serialize and deserialize object with the xml format, all this without using xsd.exe tool.

Here’s the very simple steps:

1 – The most difficult step….. copy the xml source in the clipboard, something like CTRL+A and CTRL+C :)

Image

Is ridiculous to add a screenshot, but I’ve got it, so why not!

2 – Create a new empy class file… no more screenshot please! ok here we go ;)

3 – Go to Edit -> Paste Special -> Paste XML As Classes, to paste the generated classes based on the source xml

Image

Image

Here’s the code I’ve used to test the deserialization:

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using ConsoleDump;
using ConvertXmlToCSharpClasses.Properties;

namespace ConvertXmlToCSharpClasses
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            TestSample1();

            TestSample2();

            Console.WriteLine("Press enter to exit the application...");
            Console.ReadLine();
        }

        private static void TestSample1()
        {
            var serializer = new XmlSerializer(typeof(library));
            var buffer = Encoding.UTF8.GetBytes(Resources.Sample1);
            using (var stream = new MemoryStream(buffer))
            {
                var library = (library)serializer.Deserialize(stream);
                library.book.Dump("Book");
                library.book.title.Dump("Book Title");
                library.book.author.Dump("Book Title");
            }
        }

        private static void TestSample2()
        {
            var serializer = new XmlSerializer(typeof(catalog));
            var buffer = Encoding.UTF8.GetBytes(Resources.Sample2);
            using (var stream = new MemoryStream(buffer))
            {
                var catalog = (catalog)serializer.Deserialize(stream);
                catalog.product.Dump("Product").catalog_item.Dump("Product Items")[0].size.Dump("Item Size")[0].color_swatch.Dump("Color Swatch");
            }
        }
    }
}

You can also download the test project.

4 – Enjoy your saved time

Image