Wednesday, April 3, 2013

File IO in Conjunction with WinJS

A type that you used in a .NET Framework doesn't exist within the .NET APIs for Metro style apps. Instead, you can use a type from the Windows Runtime.

For example: the System.IO. Class isn't included in the .NET APIs for Metro style apps but Windows.Storage.ApplicationDataContainer class provides similar behavior for storing app settings.

Before we begin, here are some basic prerequisites details to share about Metro style apps:

Every Metro application has three folders. A Local folder, a Roaming folder and a Temp folder.
  1. Local is meant to store assets in a local, application-specific folder.
  2. Roaming is meant to store assets that should be synchronized with any other desktop where the current user has the same application installed.
  3. Temp is a throw-away location that will be cleaned, potentially, every time the application is launched.

Creating a File on deployed Metro app location:

From the below code snippet, you can see that before creating a file we have to navigate to the storage folder. In this case its local folder where the app is deployed else we can change this location
to the installed location as well.“CreateFileAsync” is the WinJS API That is used to create a file asynchronously.

 var dataFile = "MyFile.txt";

 var appFolder = Windows.Storage.ApplicationData.current.localFolder;
 var option = Windows.Storage.CreationCollisionOption.openIfExists;

 var _file = appFolder.createFileAsync(dataFile, option);

Note: The "CreateFileAsync" API is taking another optional parameter that is “CreationCollisionOption”. Based on this option only, it will create or open the file accordingly.


Writing/Appending text on the File:

So, after creating a file successfully, writing/appending text on file will be again based on
Promise object, as soon as the file will be created or opened if existing, “AppendTextAsync”
API will be appending the data from the last cursor position.

 appFolder.createFileAsync(dataFile, option).then(function (file) {
            Windows.Storage.FileIO.appendTextAsync(file, “Testdata”).done()
        });


Reading text from the File:

So, for reading data from a file firstly, you need to get that file from the directory where is stored and then read the data using “ReadTextAsync” WinJS API.


appFolder.getFileAsync(dataFile).then(
function complete(file) {                                Windows.Storage.FileIO.readTextAsync(file).done(function (text) {
                    var json = text;
                })
            },

            function error(file) {
                //do something
            },

            function progress(file) {
                WinJS.Promise.timeout(5000);
                Windows.Storage.FileIO.readTextAsync(file).done(function (text) {
                    var json = text;
                })
            });


Tuesday, March 12, 2013

Integration of SSIS with WCF Service

To use the SSIS configuration mechanism to configure a WCF client, the easiest way is to configure your WCF client in code. We can plug in package variables for things like the service Address, user credentials settings etc. 
Here’s a quick walk-through of calling a Service from a WCF client in an SSIS package, configuring the WCF client in code and using a package variable to insert the return value in database.

First create a WCF Service for testing (I’ve used the default WCF service code for implementation)


Here is the code snippet used to create a service:


Then hit F5 to run the service in the development server, and note the address of the .svc file:


















Create a new SSIS package and add a Script Component configured to 
Source and a Execute SQL Task.  Add Package variable named “ReturnValue” to the script source to match the data flowing out of the web service.  Here it’s just a single String.

                                   









Edit the script component and pass the package variable to the script to get the output from the service.
Note: This variable will be available using suffix “User::” as it is a user defined variable not the system defined.

                                 



















Now click on Edit Script button to edit the script component. 
It will open the script editor project. Edit the script for the script source 
and change the target .NET Framework from 2.0 to 3.5 so you can
use WCF:

                  








In the Script project add a Service reference:We can follow the conventional 
procedure of consuming WCF service by creating a Proxy class also (using channel factory).














Now edit the script to configure the WCF client in code and Set the 
package variable with the output of the service.













If you’re not sure what binding to use look at the app.config that the 
Add Service Reference wizard put in your script project.

                               








And here you go after running the SSIS package :)