Friday, April 22, 2011

Rar and UnRar in DOS.cmd

To Rar all directories in the current folder and make a rar file using the folder name:
@echo off
@setlocal
REM
REM Unrar all .rar files on the current directory and use the full path
REM

set path="%ProgramFiles(x86)%\WinRAR\";%path%
for /F %%i in ('dir /b *.rar') do call :do_extract "%%i"

:eof
pause
exit 1

:do_extract
echo %1
REM x : Extract files with full path
REM -y : Assume Yes on all queries
unrar x -y %1



To Unrar all files in the current location using FullPath:

@echo off
@setlocal
REM
REM Unrar all .rar files on the current directory and use the full path
REM

set path="%ProgramFiles(x86)%\WinRAR\";%path%
for /F %%i in ('dir /b *.rar') do call :do_extract "%%i"

:eof
pause
exit 1

:do_extract
echo %1
REM x : Extract files with full path
REM -y : Assume Yes on all queries
unrar x -y %1

Monday, April 11, 2011

WCF Authenticate by APIKey

Following my previous note, I want each service have its own authentication mechanics, maybe some public services and some sharing the same authentication manager.

To do so, I will set up different behaviors in my web.config in the servicebehaviors section, and make sure each service points to the corresponding behavior:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name=
"ProductionServiceBehavior">
<serviceAuthorization serviceAuthorizationManagerType=
"WCFWebHttp.APIKeyAuthorization, WCFWebHttp" />
</behavior>
<behavior name=
"PublicServiceBehavior">
<serviceMetadata httpGetEnabled=
"true"/>
<serviceDebug includeExceptionDetailInFaults=
"false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name=
"WCFWebHttp.ProductionService" behaviorConfiguration="ProductionServiceBehavior">
</service>
</services>


Now, all I need to do is to implement my APIKeyAuthorization class. This class inherits from ServiceAuthorizationManager and overrides CheckAccessCore to validate the request and send an Error response if not validated.
For detailed information about this class see the original article on:
MSDN.

Routing WCF services by code

When you want to route the requests to specific WCF service deppending on the url, you can add routers on the Application_Start method of the Global.asax.cs. In the following example I want my ProductionService handle the service calls on the path starting with /Production/ and my PublicService handle the requests with a path starting with /public/demo/

using System;
using System.Web.Routing;
using System.ServiceModel.Activation;

public class Global : System.Web.HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
#region Register Routes
var factory =
new WebServiceHostFactory();
RouteTable.Routes.Add(
new ServiceRoute("Production", factory, typeof(ProductionService)));
RouteTable.Routes.Add(
new ServiceRoute("Public/demo/", factory, typeof(PublicService)));
#endregion
}


Wednesday, April 6, 2011

Animate Image Left To Right

The basics of any animation is moving something from one location to the other. In this code snippet I use to Image elements to slide away previously selected image and slide in the newly selected one.


The Image elements need to sit in a Canvas in order to let the Left property make sence.
<Label x:Name="txtFile"  HorizontalAlignment="Stretch"  />
<Canvas HorizontalAlignment=
"Stretch" VerticalAlignment="Stretch">

<Image x:Name=
"newImage" Canvas.Left="0" Canvas.Top="0" />
<Image x:Name=
"oldImage" Canvas.Left="0" Canvas.Top="0" />

</Canvas>



assuming that I have already given the previously selected image, my new image file will be given through a function call like
AnimateLeftToRight(selectedFile);

This method will swap the images and create a storyboard to begin animating. I start the newImage from x-position that equals to minus Window.Width and the old one starts from 0 and moves to Window.Width:


/// <summary>
/// Begins a story which animates two images from left to right
/// the old image takes the source from te new image just before assigning the new one
/// and they move together in the distance of Window.Width
/// </summary>
/// <param name="path">the path of the new image</param>
private void AnimateLeftToRight(string path)
{
oldImage.Source = newImage.Source;
newImage.Source =
new BitmapImage(new Uri(path));

var myStoryboard =
new Storyboard();

myStoryboard.Children.Add(MakeAnimation(-
this.Width, 0, "newImage"));
myStoryboard.Children.Add(MakeAnimation(
0, this.Width, "oldImage"));

myStoryboard.Begin(
this);
}



Making animation can be done in different ways using different EasingFunction, But this one looked easy to understand and look nice on my example:


/// <summary>
/// Creates a DoubleAnimation that moves the target from left to right
/// </summary>
/// <param name="from">starting point X</param>
/// <param name="to">ending point X</param>
/// <param name="target">the target element to set the x to </param>
/// <returns>the created animation</returns>
private DoubleAnimation MakeAnimation(double from, double to, string target)
{
const double durationSeconds = 1;

var animation =
new DoubleAnimation
{
Duration =
new Duration(TimeSpan.FromSeconds(durationSeconds)),
AutoReverse =
false,
From = from,
To = to,
EasingFunction =
new PowerEase()
{
EasingMode = EasingMode.EaseInOut,
Power =
5
}
};

Storyboard.SetTargetName(animation, target);
Storyboard.SetTargetProperty(animation,
new PropertyPath(Canvas.LeftProperty));

return animation;
}