Wednesday, October 19, 2011

Consume WCF REST service in F#

Today I successfully implemented the code for consuming WCF REST service in F#. Funny part here is that, this is my first program in F#.. LOL

Ok.. Here is the code for consuming WCF REST in F#.

module FSModule

#light

open System
open System.Text
open System.Net
open System.IO
open System.Web

// F# uses indenting to define scope. So ensure that you indent properly to get it working
let GetDataFromRest =
     let buffer = Encoding.ASCII.GetBytes("<HelloService_SayHello><input>Hello from F#</input></HelloService_SayHello>") //This is needed if its a POST call
     let req = WebRequest.Create(new Uri("http://localhost/HelloService.svc/SayHello")) :?> HttpWebRequest
     req.Method <- "POST"
     req.ContentType <- "application/xml" //Use accordingly XML or JSON
     req.ContentLength <- int64 buffer.Length
     let reqSt = req.GetRequestStream()
     reqSt.Write(buffer,0,buffer.Length)
     reqSt.Flush()
     req.Close()

     let res = req.GetResponse () :?> HttpWebResponse
     let resSt = res.GetResponseStream()
     let sr = new StreamReader(resSt)
     let x = sr.ReadToEnd()
     sr.Close()
     x.ToString()

Done... To invoke this, you can either use F# or C# or even VB.NET :)

F#

let result = GetDataFromRest()
printfn result

C#

string result = FSModule.GetDataFromRest()

Friday, October 14, 2011

Smartphone App for Mainframe systems - A thought

Recently I submitted this idea for one of the competition. The competition was for design approaches for smartphone app for mainframe systems.
Mainframe application provides functionality for uploading the JCL jobs via FTP and executing them. And iPhone SDK supports FTP functionality. Hence we can leverage these functionalities for running the Mainframe jobs from the iPhone or iPad Apps (even we can extend further to Android as well).



Here is the simple demonstration how this can be achieved



1) User keys in the instructions to be run in the mainframe application

2) Clicks Submit button

3) Using the iPhone SDK create the Commands.jcl file

4) Open the FTP connection to the IBM mainframe (FTP details will be pre-configured using the Settings screen)

5) Run the following commands

        a. quote site filetype=jes

        b. quote site jeslrecl=80

        c. put commands.jcl

This will kicks of the job and execute the instructions in the commands.jcl file.

6) Once the instructions are executed, the response will be available in the spooled output file “JOB#####.n” where ‘n’ is the dataset number. (The challenge here will be when to read this response file)

I am yet to hear the result from the competition. Hope this idea wins me an iPad2.. LOL

Tuesday, September 6, 2011

Silverlight and SEO

Recently I came across a very good article on Silverlight and SEO techniques for public facing Silverlight based websites. Check the link below for more;

Silverlight and SEO

Thursday, September 1, 2011

WCF RIA and custom methods

There may be times where-in you will have to add some custom coded methods along with the auto generated EF wrapper methods. You may be wondering how to do. Don't worry.. RIA provides support for that and is very simple. Follow these steps to get this work;

Server-side

In the Domain service class, decorate the custom methods with the [Invoke] attribute. Job done!!

[Invoke]
public string SayHello ()
{
        return "Hello";
}

Client-side

This is how you can access the RIA custom methods;

HelloDomainServiceContext ds = new HelloDomainServiceContext();
ds.SayHello( (p) => {var result = p.Value;}, null);

Wednesday, August 24, 2011

Dreaded NotFound and Object reference not set to an instance of object exceptions in WCF RIA Services

Recently while developing an WCF RIA Services and SL4 client, I was facing these dreaded "NotFound" and "Object reference not set to an instance of object". After a bit of googling figured out the problem and its root cause.

NotFound exception

To view the actual error, you will have to use Fiddler. In the Fiddler, you can find out the actual error as "HTTP 404, Page Not Found error". This is because, WCF RIA service sits in the same virtual directory of you SL4 application. But the necessary config entries were not getting copied to the SL4 application config file.

For instance, When you create an RIA service and SL4 client; you will get the following projects in your solution;

xxxRIASerices.Host
xxxRIAServices.Host.Web

xxxSLRIAClient
xxxSLRIAClient.Web

The necessary config entries for enabling RIA service will be available in the App.Config file in xxxRIAServices.Host.Web (Basically the connectionStrings, System.Web, System.webServer sections). Copy those sections to xxxSLRIAClient.Web Web.Config file.

Bingo, we have get rid of the NotFound exception.

Object reference not set to an instance of object exception

I am sure, you will be getting this error. :) To solve this error, copy paste the System.ServiceModel section from xxxRIAServices.Host.Web App.Config to xxxSLRIAClient.Web Web.Config file.

Yes, now you should have get rid of these 2 exceptions and your WCF RIA service and SL client App will be working like charm.