Skip to main content

WCF Basic

WCF
- windows communication foundation (.net 3.0)
- communication framework
- enables us to expose CLR type services and consume exisitng services as CLR types
- Different communication technologies in the world - webservices (asmx), web services enhancements(wse), messaging (msmq), .net enterprise services (ES), .net remoting
- used for building service oriented applications
- eg: websevice for weather - consumes zip code and outputs forecast
- wcf service is based on contract ( implemented  as an interface decorated with the attribute [ServiceContractAttribute] )
[ServiceContract]
public interface IWeatherForecastService
{
    [OperationContract]
    public WeatherForecast GetForeCast(int zipCode);
}
- Address : where the service will be found [ServiceContract]
public interface IWeatherForecastService
{
    [OperationContract]
    public WeatherForecast GetForeCast(int zipCode);
}
ABC od WCF
- Address: where the service will be found scheme://domaon[:port]/path (e.g.: http://localhost:8080/MyWCFService ). 
- Binding :Protocols used by specific endpoint(http, tcp, msmq)
- Contract : Expresses what the service does (can have an app with multiple channels)
- Endpoint : combination of ABC
- Consumer of sevice creates a proxy. This Proxy is what the consumer uses to communicate with the host and basically is an empty type that exposes all operations in a service contract, and hides serialization/sending over wire details. A single proxy, uses a single endpoint.
- WeatherForecast type is a class we wrote and it represents a business object that conveniently holds all data related to the forecast. When sending this object to a consumer it will be serialized.
Conclusion: We have a host and client who agree upon a contract.  Our host exposes one or more EndPoints which are a combination of Address, Binding and Contract. On the other side our client uses a Proxy (this proxy is tied to a specified EndPoint thus also tied to a particular Contract). The actual communication related details are abstracted in Channels and Behaviors.
Ask these to yourself
   -  First, think of the contract (What am I trying to do?!)
   -  Secondly, think of where you will host it, and what technology you will use to host it.  
   -  Finally think of the other parts like authentication.
1. Create a 'WCF Service Application'
2. Add a new WCF Service file (.svc). [name.svc and Iname.cs will be created]
3. Add this in .cs file
namespace HelloWorldService
{
    [ServiceContract]
    public interface IHelloWorld
    {
        [OperationContract]
        string sayHello(string name);
    }
}
4. Add this in .svc file
namespace HelloWorldService
{
   public class HelloWorld : IHelloWorld
    {
        public string sayHello(string name)
        {
            return "Hello " + name;
        }
    }
}
5. We just can run the application and use the default bindings
6. To test the method we can also run WCF Test Client (in C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE) and pass value to the method. We can see the output in 2 formats - XML, formated

The request:
http://schemas.xmlsoap.org/soap/envelope/
">
 
            xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">                 http://tempuri.org/IHelloWorld/sayHello>
 

 
    http://tempuri.org/
">
      Jeroen
   
 
The response:http://schemas.xmlsoap.org/soap/envelope/
">
 
 
    http://tempuri.org/
">
      Hello Jeroen
   
 

WCF CLIENT - will consume this service
client can be created
- by using metadata exposed by the EndPoint to generate proxies on the fly
- by pre-distribute the metadata as a definition of the interface, or a dll that contains the IHelloWorld interface.
1. Create a console app and add a refernce to our WCF service (add service refernce : address : http://localhost:5558/HelloWorld.svc
2. write
static void Main(string[] args)
{
    // Create a new HelloWorldClient object
    HelloWorldReference.HelloWorldClient client = new HelloWorldReference.HelloWorldClient();
    // Write the response from the sayHello method
    Console.WriteLine(client.sayHello("Jeroen"));
    // Close the WCF connection
    client.Close();
    Console.Read();
}



Types of Contracts / Services:
1. Service Contract : all operations that client can perform
2. Data Contract : defines data types that are passed in and out of service. [DataContract] and [DataMember] attributes are used
3. Fault Contract : describes the error raised by the service . [FaultContract(<>)] attribute is used
4. Message Contracts: direct control over SOAP message structure. This is useful in inter-operability cases and when there is an existing message format you have to comply with. [MessageContract], [MessageHeader], [MessageBodyMember] attributes are used



Example of Data Service:
- Microsoft implementation of OData, which is an open-format specification for accessing data
- Microsoft Visual Studio .NET facilitates creating a WCF data service by using an ADO.NET Entity Framework data model.
- WCF Data Services is designed to provide access to your data (xml, database,


A solution has a web application and a 'WCF Data Service'  file (exposed URI you can call from the internet)

Comments

Popular posts from this blog

JavaScript Interview Questions

This is a compilations of all the interview questions related to Javascript that i have encountered.  Q: Difference between window.onload and onDocumentReady? A: The onload event does not fire until every last piece of the page is loaded, this includes css and images, which means there’s a huge delay before any code is executed. That isnt what we want. We just want to wait until the DOM is loaded and is able to be manipulated. onDocumentReady allows the programmer to do that. Q:  What is the difference between == and === ? A: The == checks for value equality, but === checks for both type and value. Few examples: "1" == 1; // value evaluation only, yields true "1" === 1; // value and type evaluation, yields false "1" == true; // "1" as boolean is true, value evaluation only, yields true "1" === false; // value and type evaluation, yields false Q: What does “1″+2+5 evaluate to? What about 5 + 2 +...

Adding a linked Server using the GUI

Adding a linked Server using the GUI There are two ways to add another SQL Server as a linked server.  Using the first method, you need to specify the actual server name as the “linked server name”.  What this means is that everytime you want to reference the linked server in code, you will use the remote server’s name.  This may not be beneficial because if the linked server’s name changes, then you will have to also change all the code that references the linked server.  I like to avoid this method even though it is easier to initially setup.  The rest of the steps will guide you through setting up a linked server with a custom name: To add a linked server using SSMS (SQL Server Management Studio), open the server you want to create a link from in object explorer. In SSMS, Expand Server Objects -> Linked Servers -> (Right click on the Linked Server Folder and select “New Linked Server”) Add New Linked Server The “New Linked Server” Dialog a...

Lookup!!

LOOKUP As the name suggests, Excel gives us the option to lookup for a number or text in a specific area which needs to be stated. If the value is found the corresponding value or text is returned The syntax for LOOKUP is as follows; =LOOKUP( lookup_value , lookup_vector , result_vector )       In the diagram, column D contains varying salaries, against which there is a company car in column E which corresponds to each salary. For example, a £20030 salary gets a Golf, a £35000 salary gets a Scorpio. A LOOKUP formula can be used to return whatever car is appropriate to a salary figure that is entered. In this case, the lookup_value is the cell where the salary is entered (B13), the lookup_vector is the salary column (D3:D11), and the result_vector is the car column (E3:E11). Hence the formula; =LOOKUP(B13,D3:D11,E3:E11) Typing 40000 in cell B13 will set the lookup_value. LOOKUP will search through the lookup_vector to find the matching salary, and return th...