Specializations

Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Tuesday, November 5, 2013

WCF History of Distributed Application

WCF 
History of Distributed Application
1.)        Monolithic applications give way to application with components using COM.
2.)        Desktop COM gave way to distributed COM (DCOM).
3.)        DCOM evolved into COM+ and integrated with Microsoft Transaction server.
4.)        .NET components can use enterprise services leverage COM+
5.)        .NET Remoting consists of a client and Remote object hosted on a remote server.
6.)        Enterprise service and .NET Remoting require client ,components and data to be on the same network
7.)        Web services enable us to pass data as XML over internet
a.)SOAP (Simple Object Access Protocol) defines structure of XML Messages Client and Services pass. 
Service Oriented Architecture
1.)        A service is a program that performs a task that you can interact with through well defined messages.
2.)        A service-oriented application consists of loosely couple service that communicate through messages and contracts
a.)        Client does not instantiate the service
3.)        Shift from remotely invoking components to passing messages between services
a.)      Evolution of development model for building distributed applications.
4.)        Service are autonomous
a.)        Build, manage and version service independently(u can build  service independently and client invoke service    independently)
b.)        Change a service without affecting clients as long as clients can communicate send and receiving the same messages.
5.)        Principles of service orientation
a.)        Contracts describe the messages services can send and receive
b.)        Schemes define how the client and service construct the messages they exchange
6.)        Compatibility is policy based
a.)      Service can define the circumstances under which clients can communicates with them.
What Wrong With What We Have?
1.)        Too many ways to crate distributed applications
a.)        Web services(SOA)
b.)        Enterprise Servicesn.Net remoting(not SOA)
2.)        Which should we used and when?
a.) Interoperability
b.) Internet or TCP?
c.)  Internally or Externally.
3.)        How easy to change from one to another?
WCF
1.)        WCF provides unified programming model for building service oriented application.
2.)        One model whether communication is internal or external
3.)        Same service can be exposed over multiple protocols without effort or switching technologies.
WCF Vs Web Services
1.)        Web services generally based on HTTP protocol and XML where as WCF based on not only HTTP but also TCP and other protocols.
2.)        Send messages using format as SOAP in web services and within WCF send messages using formats other than SOAP we can use
a.)        REST(Representational State Transfer)
b.)        POX(Plain Old XML)
3.)        Web service host only within web server where as WCF host on other than web server like java server.




Different way create WCF Sever and WCF Client
WCF Service-Server
1.)        Develop using VS2010 and deploy on IIS
2.)        Deploying on WAS(Windows Activation Service)
3.)        Self host
a.)        Console Application
b.)        Windows Application/WPF Application
c.)        Web Service.
WCF Client
1.)        Using VS2010 “Add Service Reference”
2.)        Using SVCutil.exe
3.)        Custom Program(our own ways)
How TO CREATE WCF SERVICE IN CONSOLE APPLICATION
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Myownwcfserver
{
    [ServiceContract]
    public interface IMyownWCFservice
    {
        [OperationContract]
        string SayHello(string name);
        [OperationContract]
        int square(int value);
        [OperationContract]
        int Add(int a, int b);
    }
    public class MyownWCFservice : IMyownWCFservice
    {
        public string SayHello(string name)
        {
            return "Hello " + name;
        }
        public int square(int value)
        {
            return value * value;
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //create my Host
            ServiceHost MyWCFhost = new ServiceHost(typeof(MyownWCFservice), new Uri[] { new Uri("http://localhost:1059") });
            //cretate end point
            MyWCFhost.AddServiceEndpoint(typeof(IMyownWCFservice),new BasicHttpBinding(),"myownwcfathhtp");
            //add the metadata behaviour for clint understand particular service
            ServiceMetadataBehavior smb=new ServiceMetadataBehavior ();
            smb.HttpGetEnabled=true;
            //add the behaviour to my host
            MyWCFhost.Description.Behaviors.Add(smb);
            //open the host
            MyWCFhost.Open();
            //write some service activation messages
            Console.WriteLine("my own service is running ");
            Console.WriteLine("");
            Console.WriteLine("press any keys to shotdown the service");
            Console.ReadKey();
            //close the host
            MyWCFhost.Close();


        }
    }
}


 

       


Friday, March 29, 2013

jQuery Mobile basics


Getting Started with jQuery Mobile

jQuery Mobile provides a set of touch-friendly UI widgets and an AJAX-powered navigation system to support animated page transitions. Building your first jQuery Mobile page is easy. Here's how:

Create a basic page template

Pop open your favorite text editor, paste in the page template below, save and open in a browser. You are now a mobile developer!
Here's what's in the template. In the head, a meta viewport tag sets the screen width to the pixel width of the device and references to jQuery, jQuery Mobile and the mobile theme stylesheet from the CDN add all the styles and scripts. jQuery Mobile 1.2 (1.2.0) works with versions of jQuery core from 1.7.0 to 1.8.2.
In the body, a div with a data-role of page is the wrapper used to delineate a page, and the header bar (data-role="header") and content region (data-role="content") are added inside to create a basic page (these are both optional). These data- attributes are HTML5 attributes used throughout jQuery Mobile to transform basic markup into an enhanced and styled widget.

<!DOCTYPE html> 
<html> 
<head> 
 <title>My Page</title> 
 <meta name="viewport" content="width=device-width, initial-scale=1"> 
 <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
 <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
 <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
</head> 
<body> 

<div data-role="page">

 <div data-role="header">
  <h1>My Title</h1>
 </div><!-- /header -->

 <div data-role="content"> 
  <p>Hello world</p>  
 </div><!-- /content -->

</div><!-- /page -->

</body>
</html>

Add your content

Inside your content container, you can add any standard HTML elements - headings, lists, paragraphs, etc. You can write your own custom styles to create custom layouts by adding an additional stylesheet to the head after the jQuery Mobile stylesheet.

Make a listview

jQuery Mobile includes a diverse set of common listviews that are coded as lists with a data-role="listview"added. Here is a simple linked list that has a role of listview. We're going to make this look like an inset module by adding a data-inset="true" and add a dynamic search filter with the data-filter="true" attributes.

<ul data-role="listview" data-inset="true" data-filter="true">
 <li><a href="#">Acura</a></li>
 <li><a href="#">Audi</a></li>
 <li><a href="#">BMW</a></li>
 <li><a href="#">Cadillac</a></li>
 <li><a href="#">Ferrari</a></li>
</ul>

Add a slider

The framework contains a full set of form elements that automatically are enhanced into touch-friendly styled widgets. Here's a slider made with the new HTML5 input type of range, no data-role needed. Be sure to wrap these in a formelement and always properly associate a label to every form element.

<form>
   <label for="slider-0">Input slider:</label>
   <input type="range" name="slider" id="slider-0" value="25" min="0" max="100"  />
</form>

Make a button

There are a few ways to make buttons, but lets turn a link into a button so it's easy to click. Just start with a link and add adata-role="button" attribute to it. You can add an icon with the data-icon attribute and optionally set its position with the data-iconpos attribute.

<a href="#" data-role="button" data-icon="star">Star button</a>
Star button 

Play with theme swatches

jQuery Mobile has a robust theme framework that supports up to 26 sets of toolbar, content and button colors, called a "swatch". Just add a data-theme="e" attribute to any of the widgets on this page: page, header, list, input for the slider, or button to turn it yellow. Try different swatch letters in default theme from a-e to mix and match swatches.
Cool party trick: add the theme swatch to the page and see how all the widgets inside the content will automatically inherit the theme (headers and footers don't inherit, they default to swatch "a").
<a href="#" data-role="button" data-icon="star" data-theme="a">Button</a>data-theme="a" data-theme="b" data-theme="c" data-theme="d" data-theme="e" 
When you're ready to build a custom theme, use ThemeRoller to drag and drop, then download a custom theme.


button models


<a href="#" data-role="button" data-icon="star">Star button</a>

<a href="#" data-role="button" data-icon="star">Star button</a>
<a href="#" data-role="button" data-icon="home">home button</a>
<a href="#" data-role="button" data-icon="delete">delete button</a>
<a href="#" data-role="button" data-icon="plus">plus button</a>
<a href="#" data-role="button" data-icon="arrow-u">up arrow button</a>
<a href="#" data-role="button" data-icon="arrow-d">down arrow button</a>
<a href="#" data-role="button" data-icon="arrow-r">right arrow button</a>
<a href="#" data-role="button" data-icon="arrow-l">left arrow button</a>
<a href="#" data-role="button" data-icon="check">check button</a>
<a href="#" data-role="button" data-icon="gear">gear button</a>
<a href="#" data-role="button" data-icon="grid">grid button</a>
<a href="#" data-role="button" data-icon="custom">custom button</a>
<a href="#" data-role="button" data-icon="minus">minus button</a>
<a href="#" data-role="button" data-icon="plus">plus button</a>
<a href="#" data-role="button" data-icon="refresh">refresh button</a>
<a href="#" data-role="button" data-icon="forward">forward button</a>
<a href="#" data-role="button" data-icon="back">back button</a>
<a href="#" data-role="button" data-icon="alert">alert button</a>
<a href="#" data-role="button" data-icon="info">info button</a>
<a href="#" data-role="button" data-icon="search">search button</a>

Windows Communication Foundation (WCF) Support for Duplex Service


What is a Duplex Service?

A service is an application which will reside at a single place say a server and can be consumed by multiple client applications. In a normal scenario what happens is the client will send the request, wait for the response and once the response is fetched the client will proceed with its further execution. In other words the client will be waiting until the server sends back the response; this is because the service is not capable of posting a response on its own. To solve such issues the duplex service comes into play. Duplex services are ones which are able to perform a client call back, so that the client doesn't have to wait for the response, it can simply post a request and continue with its further execution. In simple words it constitutes a dual communication between the client and the server, which is client to server and server to client.

Windows Communication Foundation (WCF) Support for Duplex Service

WCF provides the support for creating a duplex service. It allows the clients to perform asynchronous calls to the WCF services and in turn the WCF service can make a callback to the client and provide the result.
All the extra stuff that needs to be done for a duplex WCF service is shown below:
  1. Create a Callback contract along with the regular WCF service contract.
  2. Have a handler class implementing the callback contract on the client.
  3. The instance of the callback handler class should be passed through the constructor of the proxy class.
  4. Once the operations are complete make sure the callback method is invoked by the service and the result is ready to be given to the client.
The above points may be a little perplexing, but in the later part of this article we will create a duplexWCF service which should be a clarifier.

Things to Watch Out For

While developing a WCF duplex service I could think of a couple of important things to pay special attention to:
  1. It only can work with the InstanceContextMode.PerSession mode.
  2. The binding used should support both PerSession mode and also dual messaging.
  3. The client configuration should define the ClientBaseAddress attribute in the binding element.

Demo Application

Now let us create a demo application which will constitute a WCF based duplex service and a Windows application client consuming it. The basic idea would be to pass an institution name from the client to the service, the result string would be formatted based on the given input data from the client request and the formatted result string will be passed to the client through a callback which would then be displayed in a message box.
  1. Create a blank solution and name it as WcfDuplexServiceDemoSolution.
  2. Add a new WCF service application to the solution.
  3. Add a new Windows application to the solution.

Developing a WCF Duplex Service

Let us now go and develop the WCF duplex service. Firstly create a callback contract namedIDuplexCallback. Below is the code:
  1. namespace DuplexWcfService
  2. {
  3.    public interface IDuplexCallback
  4.    {
  5.        [OperationContract(IsOneWay = true)]
  6.        void DuplexCallbackFunction(string requestString);
  7.    }
  8. }
  9. Note that the operation contracts in the WCF service are marked as OneWay.
  10. Now create a regular WCF contract named as IDuplexService as shown below
  11. namespace DuplexWcfService
  12. {
  13.    [ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IDuplexCallback))]
  14.    public interface IDuplexService
  15.    {
  16.  
  17.        [OperationContract(IsOneWay = true)]
  18.        void FormatString(string institution);
  19.    }
  20. }
In the above code note that the SessionMode.Required and the callback contract type are provided in the ServiceContract attribute. Add a .svc file named DuplexService.svc. Go to the code behind the DuplexService.svc which is DuplexService.svc.cs. Implement the WCF contract as shown below:
  1. namespace DuplexWcfService
  2. {
  3.    [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
  4.    public class DuplexService : IDuplexService
  5.    {
  6.        IDuplexCallback _callback;
  7.        string _requestString = String.Empty;
  8.  
  9.        public DuplexService()
  10.        {
  11.            _callback = OperationContext.Current.GetCallbackChannel<IDuplexCallback>();    
  12.        }
  13.  
  14.        public void FormatString(string institution)
  15.        {
  16.            //Make the thread to sleep for 10 seconds
  17.            Thread.Sleep(10000);
  18.            //Format the input data
  19.            _requestString = string.Format("Welcome to {0}", institution);
  20.            //Pass the string to the client through the call back function
  21.            _callback.DuplexCallbackFunction(_requestString);
  22.        }
  23.    }
  24. }
Here is the configuration entry for the servicemodel element in web.config.
  1. <system.serviceModel>
  2. <services>
  3. <service behaviorConfiguration="DuplexWcfService.Service1Behavior" name="DuplexWcfService.DuplexService">
  4. <endpoint address="http://localhost:52775/DuplexService.svc" binding="wsDualHttpBinding" contract="DuplexWcfService.IDuplexService">
  5. </endpoint>
  6. </service>
  7. </services>
  8. <behaviors>
  9. <serviceBehaviors>
  10. <behavior name="DuplexWcfService.Service1Behavior">
  11. <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
  12. <serviceMetadata httpGetEnabled="true" />
  13. <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
  14. <serviceDebug includeExceptionDetailInFaults="true" />
  15. </behavior>
  16. </serviceBehaviors>
  17. </behaviors>
  18. </system.serviceModel>
The binding used is wsDualHttpBinding which supports both per session and dual messaging.