2013-03-31

Connecting a .NET client (Console application) to a website with SignalR

On one of my previous posts, I’ve shown you how to build a web based chat application with the use of SignalR. SignalR isn’t only restricted to web clients. It also works with many other clients like iOS, Android (There are no official support for them yet though. Those projects are community driven at this stage.) and Silverlight, .NET clients. In this blog post, I’m going to show you how to integrate a .NET client to my already built web application.

Following is the link to the web application I’ve created using SignalR

http://ruchirac.blogspot.com/2013/02/creating-chat-application-in-aspnet.html

Now I’m going to connect my .NET client (Console application) to that hosted website. For that, first of all I have to install SignalR .NET Client for my Console application. I can either do it by manually adding the DLL files and adding the references to the project or install it directly using NuGet Package manager. Since Installing it via NuGet Package manager is a lot easier, I’m going to use that.

.net client

After installing the Microsoft ASP.NET SignalR .NET Client, we are ready to develop our application. Following is the code for the Console app

using Microsoft.AspNet.SignalR.Client.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleClient
{
    class Program
    {
        static void Main(string[] args)
        {
            var hubConnection = new HubConnection("
http://localhost:53748");
            var chat = hubConnection.CreateHubProxy("ChatHub");
            chat.On<string, string>("broadcastMessage", (name, message) => { Console.Write(name + ": "); Console.WriteLine(message); });
            hubConnection.Start().Wait();
            chat.Invoke("Notify", "Console app", hubConnection.ConnectionId);
            string msg = null;

            while ((msg = Console.ReadLine()) != null)
            {
                chat.Invoke("Send", "Console app", msg).Wait();
            }
        }
    }
}

At the first line, I’m connecting to the hosted website. In this example, the chat server. I’m using localhost because I’m using Visual Studio to run the application. If you’ve already hosted the application in somewhere else, then you have to give that web address to the HubConnection Constructor.

var hubConnection = new HubConnection("http://localhost:53748");

The above line will create a Connection to the Hub hosted in http://localhost:53748. We can use that to create a Hub Proxy. In my example, the hub name is ChatHub (The class name in server). So I’ve passed that to CreateHubProxy method. Then I’ve registered the method “braodcastMessage”, to display the messages on Console, app which I get from the other clients.

chat.On<string, string>("broadcastMessage", (name, message) => { Console.Write(name + ": "); Console.WriteLine(message); });

I’m getting two parameters from the server call, name and message. I’m writing them on the console as I get them. My server method looks like below

public void Send(string name, string message)
{
    // Call the broadcastMessage method to update clients.
    Clients.All.broadcastMessage(name, message);
}

As you can see, there I’m calling the method “broadcastMessage” on all clients. In the next line I’m starting the connection with the server.

hubConnection.Start().Wait();

I’m calling the Wait() method in order to wait till the connection made between server and my .NET client.

Next, I’m calling the Notify method in the server. It’s responsible for adding the connection ID to the client list and notifying other clients about the arrival of Console client.

chat.Invoke("Notify", "Console app", hubConnection.ConnectionId);

Here I’ve hardcoded the name “Console app”.

After that, I’m running a loop to get the messages from Console app. Then I invoke the server method as soon as I get a message from the .NET Client user. Then the server method, in this case “Send”, is responsible for broadcasting the message to all other connected clients.

string msg = null;

while ((msg = Console.ReadLine()) != null)
{
    chat.Invoke("Send", "Console app", msg).Wait();
}

So that’s all what it takes to build up a simple .NET client application which connects to a SignalR hub to do the real time communication. Following is a screenshot of how it looks like when it communicates with a web client.

SignalR console

2013-03-22

Crop an image when uploading to the server in ASP.NET

To crop an image when saving it to the server through asp:FileUpload control, we can use the below code. Here I’m using the Clone method of the Bitmap class and I’ve specified my area from the Rectangle. It actually creates a copy of the image within the specified area by the Rectangle.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

namespace WebApplication1
{
    public partial class About : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int x, y, width, height;
            x = y = 0; //X, Y values for crop the image.
            width = 300;
            height = 300;

            Rectangle cropArea = new Rectangle(x, y, width, height);

            Bitmap bmpImage = new Bitmap(FileUpload1.PostedFile.InputStream);
            Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
            bmpCrop.Save(@"D:\test.jpg");
        }
    }
}

Since we are using Rectangle and Bitmap classes, we have to import the System.Drawing namespace. Here I’m defining the width and height as 300px and X, Y value to 0. So it will crop 300x300 area from the image from the top left corner. Following is the markup I’ve used

<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button"
    onclick="Button1_Click" />

Following is an example image file I’ve uploaded:

1

After upload it and being cropped, following is the result file uploaded to the server.

test

Application_Error method Vs. Custom Error pages

I’m going to answer two FAQ about Application_Error method and Custom error pages.

1. If we have defined a custom error pages for our application, will the Application_Error method get fired?

Of course it will. Application_Error method in the Global.asax file will get fired whenever an unhandled exception occurs. Application_Error method will invoke every time an unhandled exception occurs regardless of whether you've declared a custom error page or not. After that method being executed, it will display the custom error page.

2. If we are doing a Response.Redirect or Server.Transfer inside the Application_Error method, then what will happen? Will the custom error page get displayed after that?

No it will not. Then the application will be redirected to the page which we've defined in the Application_Error method. It will not go to the custom error page since we are overriding the redirect page in global.asax.

Restrict user access to a website based on IP address

Though the method I’m going to describe below would not work 100%, it is the closest thing you can do within few minutes.

First, you may need to keep track of the suspected IP list in a database table or in some other storage. At the Application_BeginRequest method of the Global.asax file, iterate through each IP and match it with the current user’s IP. If it matches, then redirect the user to a permission denied page.

protected void Application_BeginRequest(object sender, EventArgs e)
{
    string ip = HttpContext.Current.Request.UserHostAddress;

    if(ip == suspectedIP){
        Response.Redirect("~/PermissionDenied.aspx");
    }
}

2013-03-12

Logging with ASP.NET - Part 2 (Using an XML file as the configuration file and logging levels)

Here I've shown how to use an XML file as the log4net configuration file and how to use logging levels in log4net to manage our log info efficiently into log files. Also, I've shown how the RollingFileAppender works and how to give a dynamic name to the log file (ex:-Date) I'm using Visual Studio 2012 and C#.NET to do the demonstration.

If the text isn’t clear, please use the HD format and full screen mode.