How to host ASP.NET 5 Application in Azure Web Apps

Standard

This post is about hosting ASP.NET 5 Application in Azure WebApps / Websites. If you are using Visual Studio, you can use the publish wizard. But if you are using k or dnx runtimes this option is not available. ASP.NET wiki in GitHub contains a page about FTP deploy but it is not working :( Later I found another page, which discuss about this. But I had to spent some time to make the deployment work successfully.

Here is the steps you need to follow to deploy ASP.NET app to Azure Web App.

  1. Include IIS in the project.json file – Even if you are using WebListener, you have to include the reference of IIS in your project.json file. This is important without this deployment will not work. So your project.json will look like this.
    {
        "webroot": "wwwroot",
        "dependencies": {
            "Microsoft.AspNet.Diagnostics": "1.0.0-beta3",
            "Microsoft.AspNet.Hosting": "1.0.0-beta3",
            "Microsoft.AspNet.Mvc": "6.0.0-beta3",
            "Microsoft.AspNet.Server.WebListener": "1.0.0-beta3",
            "Microsoft.AspNet.StaticFiles": "1.0.0-beta3",
            "Microsoft.AspNet.Server.IIS": "1.0.0-beta3"
        },
        "commands": {
            "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001"
        },
        "frameworks": {
            "aspnet50": { }
        }
    }
    
  2. Bundle the application using KPM build command – I am using k runtime, I tried with dnx runtime, but I faced few issues, so switched back to k runtime. Here is the command, which will bundle the source and will make it ready for deployment. Make sure, you are bundling with the proper runtime. I am using shared mode in azure, and hence my target platform is x86. So I am bundling with x86 version of the runtime.
    kpm bundle --out C:\WebApp --runtime kre-clr-win-x86.1.0.0-beta3
    

    You can find the installed runtimes and the name from the “C:\Users\[username]\.k\runtimes” folder. I am using beta3 runtime. Once you execute this command, you will find a folder structure like this in the C:\WebApp folder.

    Bundle output - Folder structure

    Bundle output – Folder structure

    Make sure you have a bin folder inside wwwroot folder, and which contains file – AspNet.Loader.dll. And here is the web.config file, inside wwwroot folder.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="kpm-package-path" value="..\approot\packages" />
        <add key="bootstrapper-version" value="1.0.0-beta3" />
        <add key="runtime-path" value="..\approot\packages" />
        <add key="kre-version" value="1.0.0-beta3" />
        <add key="kre-clr" value="clr" />
        <add key="kre-app-base" value="..\approot\src\MVCApp" />
      </appSettings>
    </configuration>
    

    All the runtime, dependencies and packages are created inside approot folder. If you are deploying it in IIS, you need to point the website to the wwwroot folder.

  3. Publish files using FTP – Connect to the website FTP folder using any FTP client. Copy the contents in the C:\WebApp(output folder of kpm build) to site folder of the website. With this copy operation, You are overwriting the original site/wwwroot folder with the one in output folder and adding a new folder site/approot. Once upload finished, Remote site folder structure will look like this.

    Remote site folder structure

    Remote site folder structure

Hosting ASP.NET 5 apps in IIS and IIS Express.

As I mentioned earlier, you can host this app in IIS, by pointing the physical path to the wwwroot folder.

ASP.NET 5 - Hosting in IIS

ASP.NET 5 – Hosting in IIS

And you can do the same with IIS express as well.

iisexpress /path:C:\WebApp\wwwroot /port:5001

Happy Programming :)

How to change lync status using C#

Standard

Another Lync post :) This snippet will help you to change the lync status(available, away, busy etc) using C# and Lync API. As I mentioned in the earlier post, you need to install the Lync SDK.

Here is the snippet

var lyncClient = LyncClient.GetClient();
lyncClient.Self.BeginPublishContactInformation(
    new Dictionary<PublishableContactInformationType, object>() {
    { PublishableContactInformationType.Availability, ContactAvailability.Busy }
}, null, null);

In this code, I am making the availability status as Busy. You can change it to any value available in the ContactAvailability enumeration. It is always better idea to check Lync is running or not. I have not included the code for that.

Happy Programming :)

Building a Lync bot using C#

Standard

Long back I wrote an article on creating a language translation bot using Skype and C#, since Microsoft stopped Skype COM API, and I am using lync more than Skype, I thought of writing the same implementation using lync.

The implementation is pretty same, you have to get the instance of the lync instance first, attach event handlers, translate and return the text back. To make this post simple, I am not including the translation logic, I am reversing the text and sending back to the sender.

You need to install the Lync SDK to start using the lync APIs, which you can download from here. You need to add reference of Microsoft.Lync.Model and Microsoft.Lync.Utilities DLLs.

Lync References

Lync References

And here is the code snippet.

private LyncClient _lyncClient;
private ConversationManager _conversationManager;

_lyncClient = LyncClient.GetClient();
_conversationManager = _lyncClient.ConversationManager;
_conversationManager.ConversationAdded += ConversationAdded;

private void ConversationAdded(object sender, ConversationManagerEventArgs e)
{
    var conversation = e.Conversation;
    conversation.ParticipantAdded += ParticipantAdded;
}

private void ParticipantAdded(object sender, ParticipantCollectionChangedEventArgs e)
{
    var participant = e.Participant;
    if (participant.IsSelf)
    {
        return;
    }

    var instantMessageModality = 
        e.Participant.Modalities[ModalityTypes.InstantMessage] as InstantMessageModality;
    instantMessageModality.InstantMessageReceived += InstantMessageReceived;
}

private void InstantMessageReceived(object sender, MessageSentEventArgs e)
{
    var text = e.Text.Replace(Environment.NewLine, string.Empty);
    (sender as InstantMessageModality).BeginSendMessage(Reverse(text), null, null);
}

Reverse method reverses the text and send back to the user using BeginSendMessage method. You can extend the bot MEF and custom plugins.

Happy Programming :)

Android – How to intercept SMS messages and prevent them from appearing in the Inbox

Standard

Android allows app developers to intercept SMS, here is the code snippet, which will help you to get the sms details and if you require you can stop them appearing in the inbox.

First you need to set the permission for the app to receive and process SMS. You can do this by modifing the AndroidManifest.xml file.

AndroidManifest.xml file

AndroidManifest.xml file

Source :

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

Now you need to write a BroadcastReceiver, which helps to process the SMS message.

[BroadcastReceiver (Enabled = true, Label = "SMS Receiver")]
public class SMSBroadcastReceiver : BroadcastReceiver
{
	public static readonly string IntentAction = "android.provider.Telephony.SMS_RECEIVED";

	public override void OnReceive (Context context, Intent intent)
	{
		try {
			if (intent.Action != IntentAction) {
				return;
			}

			var bundle = intent.Extras;

			if (bundle != null) {
				var pdus = (Java.Lang.Object[])bundle.Get ("pdus");
				var messages = new SmsMessage[pdus.Length];
				var stringBuilder = new StringBuilder ();
				for (int i = 0; i < messages.Length; i++) {
					messages [i] = SmsMessage.CreateFromPdu ((byte[])pdus [i]);
					stringBuilder.AppendFormat ("Message from  {0} here is the Content : {1}", 
						messages [i].OriginatingAddress.ToString (), messages [i].MessageBody.ToString ());
				}
				Toast.MakeText (context, stringBuilder.ToString (), ToastLength.Long).Show ();
				//Uncomment next line if you want to skip the inbox.
				//InvokeAbortBroadcast ();
			}
		} catch (Exception ex) {
			Toast.MakeText (context, ex.Message, ToastLength.Long).Show ();
		}
	}
}

And finally you need to register the BroadcastReceiver, you can do this like

RegisterReceiver (new SMSBroadcastReceiver(), new IntentFilter ("android.provider.Telephony.SMS_RECEIVED"));

You can use this technique to avoid SPAM messages by looking into the number or message content.

Note: You can verify this implementation using android emulator. You can find more details here – SMS Emulation

Happy Programming. :)