Saturday, 10 August 2013

WCF - Invoke a WCF Service Application from a client

     First step to invoke a WCF service get the proxy class for that using  svcutil command in vscmd.exe
In our example let we take following example to Create WCF Service and Host WCF Service

    Type the following cmd in visual studio command prompt to download the proxy class
svcutile.exe  "WCF Hosted service url" , Results in generate a two files.
Service.cs and config file



Call a WCF service

Step 1. Right click the project.
Step 2. Add a Service Reference and Enter the Url.
Step 3. Select ok.


ServiceReference1.Service1Client cli = new ServiceReference1.Service1Client();
string h=cli.WelcomeMessage("rajesh");
Console.WriteLine(h);


All the class are ends with Client ,create a object and invoke the methods 

output:



From this article you can learn how to call a WCF service from a client.

WCF - Host WCF service application.

In This article we are going to see hosting a WCF application in various ways.
1. Self Hosting
2. IIS

Self Hosting 
    After developing the WCF application, It should be hosted so now we are currently see how we are going to Host the application as Self Hosted.


  1. Open a Visual studio 2008 and above 2010.
  2. Create a console application .
  3. Add Reference "System.ServiceModel"


    class Program
    {
        static void Main(string[] args)
        {
           
            Uri httpUrl = new Uri("http://localhost:8090/Service/Calculator"); // Hosting Url
            using (ServiceHost host = new ServiceHost(typeof(CalculatorService.Service1), 
                                                      httpUrl))
            {
                host.AddServiceEndpoint(typeof(CalculatorService.IService1), 
                       new   WSHttpBinding(), "");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                host.Description.Behaviors.Add(smb);

                host.Open();
                Console.WriteLine("Service is host");
                Console.WriteLine("Host is running... Press any key to stop");
                Console.ReadLine();
            }
         
        }
    }

Press F5 to host the WCF service..

Output



Above image shows that WCF service is self hosted.
Now launch the service by the following URL  http://localhost:8090/Service/Calculator





IIS HOSTED 
     Hosting the WCF service application in IIS , Before  hosting check the web.config file for the binding, end points,and behaviour information for example let we consider a sample of binding in Web.config

Web.Config

<system.serviceModel>
    <services>
      <service name="CalculatorService.Service1" behaviorConfiguration="CalculatorService.Service1Behavior">
          
        <endpoint address="" binding="basicHttpBinding" contract="CalculatorService.IService1">         
        </endpoint>                             
       
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>                         
        <behavior name="CalculatorService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to               false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- 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 -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        
      </serviceBehaviors>
    </behaviors>   
  </system.serviceModel>

Hosting the WCF in IIS do the following steps 

1.   Type Inetmgr in run and press enter
2.   Right click on Sites and select a Add a WebSite 





3.   Give a name "Calculator" 
4.   Select AppPool as Framework 4.0
5.   In Binding select http type and port 70.
6.   Select Ok.





Now Launch the Service by following URL http://localhost:70/Service1.svc





From this article we can see how to host the WCF service in various ways.

WCF - Create a sample WCF service application

To create a sample WCF service application, Do the following steps.

  1. Launch the Visual studio 2008 and above version.
  2. Select the New Project
  3. Select "WCF service application" in WCF template.
  4. Give the name of the Project as "CalcualtorService"
  5. click enter

Step 1 : Now select the Interface IService.cs and add an operation contract WelcomeMessage.


   [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        string WelcomeMessage(string name);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


Step 2 : Now select the service1.svc, Implement the method "WelcomeMessage" which will return the string type.

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public string WelcomeMessage(string name)
        {
            return string.Format("Welcome to the dotnetvisio.blogspot.com "+name);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

Repeat the above two steps for adding additional two methods Add and Sub. Add the following code in interface.

       [OperationContract]
        int Add(int a, int b);

        [OperationContract]
        int Sub(int a, int b);

Now add the following code of implementation in service1.svc

        public int Add(int a, int b)
        {          
            return a + b;
        }

        public int Sub(int a, int b)
        {
            return a - b;
        }

    Now Press F5 WCF test client application will run to test the WCF service.In that application double click the method WelcomeMessage, Add . Sub which is present in left side of the panel in tree view. and give the input in the right panel in Request section.Press the Invoke button to see the output. It will be seen in response section.


Before hosting WCF Service Application configure the Web.Config by adding the multiple EndPoints and Behavior

     <system.serviceModel>
    <services>
      <service name="CalculatorService.Service1" behaviorConfiguration="CalculatorService.Service1Behavior">
          
        <endpoint address="" binding="basicHttpBinding" contract="CalculatorService.IService1">         
        </endpoint>                             
       
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>                         
        <behavior name="CalculatorService.Service1Behavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- 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 -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        
      </serviceBehaviors>
    </behaviors>   
  </system.serviceModel>

Click the below links to see the host and invoke a WCF service through client 

Host a WCF Service


From this article you will find the how to create a WCF service application.




Copy data from command prompt to Clipboard

           Sometimes we have to copy the data from command prompt using the mark all options in command prompt then press enter to copy the data. Instead of that how we can copy the output of a command in to clipboard.

Now let we see a sample demo , using the Command Dir which will list the content of a directory.


  1. Launch the Run.
  2. Type the cmd.
  3. enter
Type the "dir" in the command prompt



Now we can copy the same data to clipboard instead of command prompt. for  "Dir"

type dir | clip in cmd and enter , output of the following command is stored in clipboard.

 


Now paste the data to notepad and see the data .



  From this article you can learn how to copy the output of a command from the command prompt to clip board.


Thursday, 8 August 2013

Jquery - Graph Drawing



The above attractive graph is an example of an below code.

  In HTML we can draw a attractive Graph with combination of css and Jquery.You have to just pass the parameters that have to plot the graph at which positions in an graph.Data must be in array using Jquery.

     To Create a attractive plot with in a short time. Create a placeholder, make sure it has dimensions (so Flot knows at what size to draw the plot), then call the plot function with your data.The axes are automatically scaled.

To Plot the Graph Using following JQuery Plugins and CSS in your application. Please download the Plugins from the following links.

Click Here to Download CSS
Click Here to Download JQuery
Click Here to Download JQuery-Plotting

JQuery :

<script type="text/javascript">

    $(function()
    {
        var d1 = [];
        for (var i = 0; i < 20; i += .4) {
            d1.push([i, (Math.tan(i)+Math.sin(i))]);
        }

        var d2 = [[0, 120], [4, -120], [8, 50], [9, -50]];
        // A null signifies separate line segments

        var d3 = [[0, 100], [7, 50], null, [15, -50], [20, -100]];

        var d4=[[0,50],[1,-120],[3,15],[5,25],[8,30],[20,100]]
       
        $.plot("#placeholder", [ d1, d2, d3,d4 ]);
      
    });

    </script>



HTML CODE:
<div id="content">
<div class="demo-container">
<div class="demo-placeholder" id="placeholder" width="600px">
</div>
</div>
</div>

CSS :
#content {
    width: 600px;
    margin: 0 auto;
    padding: 10px;
}

#footer {
    margin-top: 25px;
    margin-bottom: 10px;
    text-align: center;
    font-size: 12px;
    color: #999;
}

.demo-container {
    box-sizing: border-box;
    width: 600px;
    height: 450px;
    padding: 20px 15px 15px 15px;
    margin: 15px auto 30px auto;
    border: 1px solid #ddd;
    background: #fff;
    background: linear-gradient(#f6f6f6 0, #fff 50px);
    background: -o-linear-gradient(#f6f6f6 0, #fff 50px);
    background: -ms-linear-gradient(#f6f6f6 0, #fff 50px);
    background: -moz-linear-gradient(#f6f6f6 0, #fff 50px);
    background: -webkit-linear-gradient(#f6f6f6 0, #fff 50px);
    box-shadow: 0 3px 10px rgba(0,0,0,0.15);
    -o-box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    -ms-box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    -moz-box-shadow: 0 3px 10px rgba(0,0,0,0.1);
    -webkit-box-shadow: 0 3px 10px rgba(0,0,0,0.1);
}

.demo-placeholder {
    width: 100%;
    height: 100%;
    font-size: 14px;
    line-height: 1.2em;
}



     From the above sample code you can create a attractive graph using Jquery and css. I hope from this article you have some idea on graph using Jquery and css.