суббота, 26 октября 2013 г.

How to revert to the previous version of a nuget package on subset of projects

I was trying to revert nuget package to a previous version. Apparently, this is not an easy task. You can easily update some package to a new version, but if you want to downgrade, you would have to uninstall the old version and then reinstall a specific version. Moreover, if you don't want to install it to all the projects, you would have to remember the project names.

Here is a script that automates this process.
Replace the package name and version and run it in the Package Manager Console.

$packageName = Newtonsoft.Json;
$version = 4.0.5
$projects = Get-Project -all
$projectsWithPackage = new-object System.Collections.ArrayList;
ForEach ($proj in $projects) { Write-Host $project.ProjectName; $projWithPackage = Get-Package -Filter $packageName -ProjectName $proj.ProjectName; if($projWithPackage.Length -gt 0) { $projectsWithPackage.Add($proj.ProjectName) } }
ForEach ($proj in $projectsWithPackage) { Uninstall-Package $packageName -ProjectName $proj -Force}
ForEach ($proj in $projectsWithPackage) { Install-Package $packageName -Version $version -ProjectName $proj }

I haven't figured out how to break long commands to multiple lines, so please feel free to give me your valuable advice. :)

пятница, 12 ноября 2010 г.

XamlWriter.Save throws exception when trying to serialize empty ObservableCollection

I've encountered this exception, when tried to serialize empty ObservableCollection. Non-empty collection was serialized smoothly.

To fix it, create a wrapper class.

public class StringObservableCollection : ObservableCollection<string>
{
}

// ... 
var collection = new StringObservableCollection();
System.Windows.Markup.XamlWriter.Save(collection);

среда, 27 октября 2010 г.

TO DON'T: Do not ever think of writing WYSIWYG visual editor on WPF RichTextBox and FlowDocument base

It's gonna be a pain in the ass.
FlowDocument has very limited capabilities for visual editing. It's gonna be a pain to:
— Insert an image
— Make an image resizable
— Serialize, copy-paste images
— Modify table's width directly, horizontal or vertical alignment
— Modify table column's width and table cell height
— Implement a robust algorithm of table cells merging
... I'm sure this list can be continued.

Just remember to think twice before considering this idea as a good one

суббота, 2 октября 2010 г.

How to run Console in Windows Forms application

To be honest, it's quite easy to find out this with google, but I'd like to share a couple of useful tricks.

To open console window in Windows Forms application, write the following in Program.cs Main function:

[STAThread]
static void Main()
{
    AllocConsole();
    // .... the rest of the code, opening MainForm
}

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool AllocConsole();


Console windows are usually need to be opened on debugging and tracing purposes. Note that if you didn't open Console window, everything you wrote using Console.WriteLine method would appear in Output tab in Visual Studio during debugging. So go to View > Output to open this tab and you will see your messages.

However, it's better to use more convenient Debug class from System.Diagnostics namespace and it's Debug.WriteLine method or another useful method Debug.Assert(), which checks weather some statement is true. This methods calls are not included into the final release build, because they are decorated with [ConditionalAttribute("DEBUG")] attribute, so you don't have to bother clearing up your code after debugging is finished. Console.WriteLine are not cleared out during release build, that's why it's not cool to use them.

You can also decorate your debugging methods with [ConditionalAttribute("DEBUG")] attribute or wrap debugging code in
#if DEBUG
#endif
language preprocessor directives.

You can find out even more great VisualStudio 2010 debugging tricks in a ScotGu's post.

пятница, 27 августа 2010 г.

How to get UDP sender IP address and port number in .NET

Surprisingly, it was not easy to find out IP address and port number of UDP package sender in .NET. After a couple of googling hours, I gave up. The answer was found ... guess where? — At MSDN :)

So far, if you are listening some local port and accept any incoming UDP message, use EndReceiveFrom method. Below is an example of simple (and pretty useless) application, that sends a UDP message to itself (127.0.0.1).


using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace UDPTestProject
{
    class Program
    {
        private static byte[] buffer;
        private static Socket sender;
        private static EndPoint remoteEndPoint;
        static void Main(string[] args)
        {
            try
            {
                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                EndPoint localEndPoint = (EndPoint)new IPEndPoint(IPAddress.Any, 30777);
                socket.Bind(localEndPoint);

                buffer = new byte[512];

                EndPoint remoteEndPoint = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
                IAsyncResult ar = socket.BeginReceiveFrom(buffer,
                                        0,
                                        buffer.Length,
                                        SocketFlags.None,
                                        ref remoteEndPoint,
                                        Callback,
                                        socket);

                sender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                EndPoint localhostEndPoint = (EndPoint)new IPEndPoint(IPAddress.Parse("127.0.0.1"), 30777);
                byte[] sendBuffer = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                sender.BeginSendTo(sendBuffer, 0, sendBuffer.Length, SocketFlags.None, localhostEndPoint, OnSend, null);

                ar.AsyncWaitHandle.WaitOne();
                Console.Read();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static void OnSend(IAsyncResult ar)
        {
            sender.EndSend(ar);
        }

        public static void Callback(IAsyncResult ar)
        {
            Socket socket = (Socket)ar.AsyncState;
            remoteEndPoint = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
            socket.EndReceiveFrom(ar, ref remoteEndPoint);
            Console.WriteLine("Received from " + remoteEndPoint.ToString());
        }
    }
}

воскресенье, 24 января 2010 г.

We're moving

My russian blog is  here: http://aspnetmvcfan.blogspot.com/