Sunday 9 April 2017

WCF a beginners tutorial

Contents                                                            




To get an overall idea about   the fundamentals of WCF, and help those readers who want to learn the basics of WCF from scratch
1.2.SCOPE
WCF is one of the latest technologies of Microsoft that is used to build service-oriented applications. Based on the concept of message-based communication, in which an HTTP request is represented uniformly, WCF makes it possible to have a unified API irrespective of diverse transport mechanisms.
WCF-Windows Communication Foundation

SOAP - Simple Object Access Protocol


<This section should list all the applicable and reference documents>
  

What is WCF ???
Ø  WCF is one of the latest service oriented technology developed by Microsoft
Ø  Released for the first time in 2006 as a part of the .NET framework, and then got updated several times. WCF 4.5 is the most recent version that is now widely used
Ø  WCF is a programming platform and runtime system for building, configuring and deploying network-distributed services
Ø  The elementary feature of WCF is interoperability
Ø  WCF is a set of .NET technologies (Web services ,.Net Remoting and enterprises services) for building and running connected systems
Why WCF ???
Ø  It is interoperable with respect to other services
Ø  Provides a varying array of communication across several platforms: HTTP, HTTPS, TCP, MSMQ, and Etc
Ø  It provides additional Security
Ø  It provides more code reusability
Ø  Different forms of hosting
Ø  WCF has integrated AJAX and support for JSON (JavaScript object notation)
Ø  It offers scalability and support for up-coming web service standards

When WCF???
Ø  When your business logic has to interact with a variety of client applications
Ø  When client apps, which are going to use your service, may be written in Java or .Net.
Ø  You are targeting a distributed computing architecture
Difference between WCF & Web Services
WCF
Ø  [ServiceContract] attribute has to be added to the class
Ø  [OperationContract] attribute represents the method exposed to client
Ø  One-Way, Request-Response, Duplex are different type of operations supported in WCF
Ø  System.Runtime.Serialization namespace is used for serialization
Ø  XML 1.0, MTOM, Binary, Custom
Ø  Can be accessed through HTTP, TCP, Named pipes, MSMQ,P2P, Custom
Web Services
Ø  [WebService] attribute has to be added to the class
Ø  [WebMethod] attribute represents the method exposed to client
Ø  One-way, Request- Response are the different operations supported in web service
Ø  System.Xml.serialization name space is used for serialization
Ø  XML 1.0, MTOM(Message Transmission Optimization Mechanism), DIME, Custom
Ø  Can be accessed through HTTP, TCP, Custom
WCF – key terms to remember
  1. WCF Service 
  2. Addresses
  3. Contracts
  4.  Host Environment
  5.  End point 
  6.  WCF Client
  7. Channel
  8. SOAP
WCF Service
A WCF service is based on an interface that defines a contract between the service and the client. It is marked with a  ServiceContractAttribute attribute
Example:
[ServiceContract]
public interface ICalculatorService
We define functions or methods that are exposed by a WCF service by marking them with an OperationContractAttribute
Example :
[OperationContract]
string GetData(string value);
Address
Description:
In WCF, every service is associated with a unique address. The address provides two important elements: the location of the service and the transport protocol or transport schema used to communicate with the service. The location portion of the address indicates the name of the target machine, site, or network; a communication port, pipe, or queue; and an optional specific path or URI.
a) TCP Addresses
Ex. net.tcp://localhost:8002/MyService.
b) HTTP Addresses
c) IPC Addresses
Ex. net.pipe://localhost/MyPipe
d) MSMQ Addresses
Ex. net.msmq://localhost/MyService
e) Peer Network Addresses
Peer network addresses use net.p2p for transport, to indicate the use of the Windows peer network transport. You must specify the peer network name as well as a unique path and port.
Contracts
Description:
In WCF, all services expose contracts. The contract is a platform-neutral and standard way of describing what the service does. WCF defines four types of contracts.
a) Service Contracts
Describe which operations the client can perform on the service.
b) Data Contracts
Define which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but you can easily define explicit opt-in data contracts for custom types
c) Fault Contracts
Define which errors are raised by the service, and how the service handles and propagates errors to its clients
d) Message Contracts
Allow the service to interact directly with messages. Message contracts can be typed or untyped, and are useful in interoperability cases and when there is an existing message format you have to comply with.
Hosting
Hosting from the viewpoint of WCF refers to the WCF service hosting which can be done through many available options like
self-hosting
IIS hosting
and WAS hosting.
End point
All communications with the WCF service will happen via the endpoints.
It defines the address where a message is to be sent or received. It also specifies the communication mechanism to describe how the messages will be sent along with defining the set of messages. A structure of an endpoint comprises of the following parts:
Address - Address specifies the exact location to receive the messages and is specified as a Uniform Resource Identifier (URI). It is expressed as scheme://domain[:port]/[path]. Take a look at the address mentioned below:
net.tcp://localhost:9000/ServiceA
Here, 'net.tcp' is the scheme for the TCP protocol. The domain is 'localhost' which can be the name of a machine or a web domain, and the path is 'ServiceA'.
Binding - It defines the way an endpoint communicates. It comprises of some binding elements that make the infrastructure for communication. For example, a binding states the protocols used for transport like TCP, HTTP, etc., the format of message encoding, and the protocols related to security as well as reliability.
Contracts - It is a collection of operations that specifies what functionality the endpoint exposes to the client. It generally consists of an interface name.
WCF Client
A WCF client consists of a proxy that enables an application to communicate
with a WCF service, and an endpoint that matches an endpoint defined for the service. The proxy is generated on the client side in the app.config file and includes information about the types and methods that are exposed by the service. For services that expose multiple endpoints, the client can select the one that best fits its needs, for example, to communicate over HTTP and use Windows Authentication.

Channel
Channels are the responsible component of WCF for
Ø  Creation of consistent message
Ø  Transport of consistent message
Ø  Conversion of Message in wire format
The main goal of Channel is to transform message to format understandable by the communication wire and compatible to both server and client and then transport the message over the wire in between client and server.

Soap(Simple Object Access Protocol)
SOAP is an open-standard, XML-based messaging protocol for exchanging information among computers.
Although SOAP can be used in a variety of messaging systems and can be delivered via a variety of transport protocols, the initial focus of SOAP is remote procedure calls transported via HTTP.
Our First WCF!!!
First open Visual Studio and click file --> New ->Project  
-->WCF ->WCF Service Library  
Given the name of WCF Service Library and click ok



Now we are successfully created a WCF Service Library. Now In our solution Explorer we can see default class files including Service.cs and IService.cs.
Ø   IService.cs is an interface it does contain Service contracts and Data Contracts
Ø   Service.cs is a normal class inherited by IService where you can all the methods and other stuff.
Ø  Change the file names like below screenshot.



Now open ICalculator.cs and replace with  following code.
using System;
using System.Runtime.Serialization;
using System.ServiceModel;

namespace Calculator
{
    [ServiceContract]
    public interface ICalculatorService
    {
        [OperationContract]
        ResultSet PerformCalculatorOperations(double firstInput,double secondInput,string operationType);

    }

    [DataContract]
    public class ResultSet
    {
        [DataMember]
        public double Result { get; set; }
        [DataMember]
        public string Staus { get; set; }
    }
}

Then open Calculator.cs file and replace the content with below code.
namespace Calculator
{
    public class CalculatorService : ICalculatorService
    {
        ResultSet result = new  ResultSet();
        public ResultSet PerformCalculatorOperations(double firstInput, double secondInput, string operationType)
        {

            switch (operationType)
            {
                case "Addition":
                    Addition(firstInput, secondInput);
                    break;
                case "Subtraction":
                    Subtraction(firstInput, secondInput);
                    break;
                case "Multiplication":
                    Multiplication(firstInput, secondInput);
                    break;
                case "Division":
                    Division(firstInput, secondInput);
                    break;
                 default:
                    result.Staus = "Error";
                    break;
            }
            return result;

        }
        public void Addition(double firstInput, double secondInput)
        {
            result.Result = firstInput + secondInput;
            result.Staus = "Sucess";
        }
        public void Subtraction(double firstInput, double secondInput)
        {
            result.Result = firstInput - secondInput;
            result.Staus = "Sucess";
        }
        public void Multiplication(double firstInput, double secondInput)
        {
            result.Result = firstInput * secondInput;
            result.Staus = "Sucess";
        }
        public void Division(double firstInput, double secondInput)
        {
            result.Result = firstInput / secondInput;
            result.Staus = "Sucess";
        }

    }
}
Build the application. Now our WFC Service Library is ready to use. Run the project this will open a WCF test client.
                 


In test client click on service name (PerformCalcultorOperations). And fill the inputs to the service


Now you can see the output of our application in WCF Test client




How to analyze .net applications using SonarQube

            

Contents                                                                                               

1.      INTRODUCTION

1.1.       OBJECTIVE

1.2.       SCOPE

1.3.       DEFINITIONS, ACRONYMS AND ABBREVIATIONS

1.4.       REFERENCES

2.      CONTENTS

 1 . INTRODUCTION


Help those readers who want to learn how to analyze coding standards, code unit tests, and code coverage, complex code, potential bugs, comments etc. using SonarQube

It helps for various tasks and provide reports on duplicated code, coding standards, unit tests, code coverage, complex code, potential bugs, comments and design and architecture.

https://www.sonarqube.org/

  2. CONTENTS

What is sonarQube
Sonar is a web based code quality analysis tool for Maven based Java projects. It covers a wide area of code quality check points which include: Architecture & Design, Complexity, Duplications, Coding Rules, Potential Bugs, Unit Test etc.

Steps to setup Sonarqube:


Prerequisite
Ø  JDK
Ø  Visual Studio

1)      Download SonarQube 6.3here
2)      You will get a sonarqube-6.3.zip file(You can prefer if latest version is available)
3)      unzip it to a location (let's say in " C:\sonarqube")
4)      Download SonarQube Scanner for MSBuild from here
5)      You will get a sonar-scanner-msbuild-2.2.0.24.zip file (versions might be differing in future)
6)      unzip it to a location (let's say in " C:\sonarqube")
7)      Set the environment variables for SonarQube, Sonarqube-scanner-msbuild
8)      Follow the below screen shots to set environment variables




                  9)      Execute the following to start SonarQube server:
On Windows,
è StartSonar
Note : if you are not setting environment variables for sonarqube then you have to give full path like below
è cd C: \sonarqube\sonarqube-6.3\bin\windows-x86-64
è StartSonar.bat
                 10)  The sonar is up and running as shown below:

Step 11: Give path of your .net project. In my case I have given the following path
Ø  cd C:\InternalProject\Sprint3\Hosts\OSITDC.Host.ServiceApi
Ø  then enter the following command in command prompt
Ø  SonarQube.Scanner.MSBuild.exe begin /k:"OSITDC.Host.ServiceApi" /n:"OSITDC.Host.ServiceApi" /v:"1.0"
Ø  MSBuild.exe /t:Rebuild
Ø  SonarQube.Scanner.MSBuild.exe end
Here OSITDC.Host.ServiceApi is my .net project name , once the analysis finished you can try to access the sonarqube default site

Ø  localhost:9000  open this page from your browser and you can see your project analysis there