Hi Everyone, After publishing my blog on .NET framework, .NET Keywords, C# keywords and multithreading, started a new blog on WCF knowledge - Part1, this blog will cover almost all the fundamental concept on WCF but still need to enhance with advance question along with tips and tricks, will post the next blog in coming weekend.
Why WCF?
1. WCF is interoperable with other services when compared to .Net Remoting, where the client and service have to be .Net.
2. WCF services provide better reliability and security in compared to ASMX web services.
3. In WCF, there is no need to make much change in code for implementing the security model and changing the binding. Small changes in the configuration will make your requirements.
4. WCF has integrated logging mechanism, changing the configuration file settings will provide this functionality. In other technology developer has to write the code.
5. It can be hosted in IIS, WAS, Self hosting and Managed windows service.
What is difference between WCF and Web services?
1. Web Services is hosted in IIS, accessed only over HTTP and works in stateless environment while WCF is flexible and can be hosted in
i. IIS (Internet information services)
ii. WAS (Windows activation services)
iii. Self-hosting
iv. Managed Windows Service
2. Web Service supports One-way and Request- Response communications while WCF supports One-Way, Request-Response and Duplex communication.
3. Web Service used System.Xml.serialization namespace while WCF used System.Runtime.Serialization for serialization
4. Web service supports security while WCF supports security, reliable session and transportation
What is a SOA Service?
SOA is Service Oriented Architecture. SOA service is the encapsulation of a high level business concept. A SOA service is composed of three parts.
1. A service class implementing the service to be provided.
2. An environment to host the service.
3. One or more endpoints to which clients will connect.
Which protocol is used for platform-independent communication?
SOAP (Simple Object Access Protocol), which is directly supported from WCF (Windows Communication Foundation).
What is the proxy for WCF Service?
A proxy is a CLR class by which a service client can interact with the service.
By the use of proxy in the client application we are able to call the different methods exposed by the service
What is the advantage of Proxy Class?
The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.
How can we create Proxy for the WCF Service?
Two ways are their 2 create a proxy in WCF service
1. Using Command prompt tool: svcutil.exe
We can use the following command at command line.
svcutil.exe *.wsdl *.xsd /language:C# /out:SampleProxy.cs /config:app.config
2. Using Visual Studio: By right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy.
What is .svc file in WCF?
.svc file is similar as .asmx file in web services and it contains the details required for WCF service to run it successfully.
This file contains following details:
1. Language (C# / VB) 2. Name of the service 3. Where the service code resides
Example of .svc file
<%@ ServiceHost Language="C#/VB" Debug="true/false" CodeBehind="Service code files path" Service="ServiceName" >
Note: We can also write our service code inside but this is not the best practice.
What is WCF Design Principle?
1. Boundaries are explicit: No attempt to hide communication
2. Services are autonomous: Deployed, Managed and version independently
3. Services share contract and schemas, not types: Contracts defines behavior, schemas define data
4. Compatibility is policy-based: Policy supports separation of behaviour from access constraints
What is WCF (Windows Communication Foundation)?WCF is a programming platform and runtime system for building, configuring and deploying network-distributed services. WCF provides a runtime environment for services, enabling you to expose CLR types as services, and to consume other services as CLR types. WCF is a combined feature of Web Service, Removing, MSMQ and COM+.
What is service and client in perspective of data communication?A service is a unit of functionality exposed to the world while client of a service is merely the party consuming the service.
What do you understand by endpoint?The endpoint is an Interface which defines how a client will communicate with the service
End Point = Address + Binding + Contract
Endpoint features
1. Portal for the service
2. Communicate with outside world
What are the essential pieces and files are required to host the any WCF Service?
Essential pieces in WCF
1. Contracts for services, data, and messages: A contract is simply an interface declaration
2. Service, Data, and Message definitions: Class implementations
3. Configurations defined programmatically or declaratively: Config files
4. A host process (can be self hosted) : IIS, Windows Executable, Windows Service, or WAS
Service files are included in WCF
1. IService.cs: Interface(s) that define a service, data, or message contract
2. Service.cs: Implement the service’s functionality
3. Service.svc: Markup file (with one line) used for services hosted in IIS
4. Configuration files that declare service attributes, endpoints, and policy: App.config (self hosted) contains service model markup and Web.config (hosted in IIS) has web server policy markup plus service model markup, as in App.config
What is ABC?
Address: URL where the WCF has hosted. Using this URL client will communicate to the service. i.e. http://localhost:8090/MyService/SimpleCalculator.svc
Schema: It’s top level of the address; normally it is http with colon
Machine: Machine name can be public url like www.google.com or can be a localhost
Port: Port number is optional and comes after colon
Path: It is used to locate the service file
Binding: How the client will communicate with services.
Contract: Collection of operation that specifies what the endpoint will communicate with outside world.
What are the bindings are supported by WCF?
BasicHttpBinding:
1. Basic web service communication.
2. Exposes WCF services as legacy ASMX web services. It is fairly weak on interoperability and No security by default.
3. This binding uses HTTP as the transport and text/XML as the default message encoding.
4. This binding does not support WS-* functionalities like WS- Addressing, WS-Security, WS-Reliable Messaging
WSHttpBinding:
1. Defines a secure, reliable, interoperable binding suitable for non-duplex service contracts.
2. It offers lot more functionality in the area of interoperability. It supports WS-* functionality and distributed transactions with reliable and secure sessions using SOAP security.
3. It uses HTTP and HTTPS transport for communication. Reliable sessions are disabled by default.
WSDualHttpBinding:
1. This binding is same as that of WSHttpBinding, except it supports duplex service.
2. Duplex service is a service which uses duplex message pattern, which allows service to communicate with client via callback.
3. In WSDualHttpBinding reliable sessions are enabled by default. It also supports communication via SOAP intermediaries.
WSFederationHttpBinding:
1. This binding support federated security.
2. It helps implementing federation which is the ability to flow and share identities across multiple enterprises or trust domains for authentication and authorization.
3. It supports WS-Federation protocol.
NetTcpBinding:
1. This binding provides secure and reliable binding environment for .Net to .Net cross machine communication.
2. By default it creates communication stack using WS-ReliableMessaging protocol for reliability, TCP for message delivery and windows security for message and authentication at run time.
3. It uses TCP protocol and provides support for security, transaction and reliability.
NetNamedPipeBinding:
1. Communication between WCF applications on same computer. Supports duplex contracts and transactions.
2. This binding provides secure and reliable binding environment for on-machine cross process communication.
3. It uses NamedPipe protocol and provides full support for SOAP security, transaction and reliability.
4. By default it creates communication stack with WS-Reliable Messaging for reliability, transport security for transfer security, named pipes for message delivery and binary encoding.
NetMsmqBinding:
1. This binding provides secure and reliable queued communication for cross-machine environment.
2. Queuing is provided by using MSMQ as transport. It enables for disconnected operations, failure isolation and load levelling
NetPeerTcpBinding:
1. This binding provides secure binding for peer-to-peer environment and network applications.
2. It uses TCP protocol for communication. It provides full support for SOAP security, transaction and reliability.
MsmqIntegration:
1. Communication directly with MSMQ applications. Supports transactions
Webhttpbinding:
1. It is introduced in .net framework 3.5, it’s basically used to provide explicit support for RESTful communication
2. The below code shows how to expose a RESTful service
[ServiceContract]
Interface IStock
{
[OperationContract]
[WebGet]
Int GetStock(string StockId);
}
By adding the WebGetAttribute, we can define a service as REST based service that can be accessible using HTTP GET operation.
What is the address formats of the WCF transport schemas?
Address format of WCF transport schema always follow
[transport]://[machine or domain][:optional port] format.
For example:
HTTP Address Format: http://localhost:8888/
1. The way to read the above url is "Using HTTP, go to the machine called localhost, where on port 8888 someone is waiting"
2. When the port number is not specified, the default port is 80.
TCP Address Format: net.tcp://localhost:8888/MyService // port no. 8888
When a port number is not specified, the default port is 808
i.e. net.tcp://localhost/MyService
IPC Address Format: net.pipe://localhost/MyPipe
We can only open a named pipe once per machine, and therefore it is not possible for two named pipe addresses to share a pipe name on the same machine.
MSMQ Address Format net.msmq://localhost/private/MyService
net.msmq://localhost/MyService
What are the different type of contract exist in WCF?
Contracts: In WCF, all services are exposed as contracts. Contract is a platform-neutral and standard way of describing what the service does. Mainly there are four types of contracts available in WCF
Service Contract:
1. Describe the operation that service can provide. For Eg, a Service provide to know the temperature of the city based on the zip code, this service is called as Service contract.
2. It will be created using Service and Operational Contract attribute.
Data Contract:
1. Describes the custom data type which is exposed to the client.
2. This defines the data types,that are passed to and from service.
3. Data types like int, string are identified by the client because it is already mention in XML schema definition language document, but custom created class or data types cannot be identified by the client e.g. Employee data type.
4. By using DataContract we can make client to be aware of Employee data type that are returning or passing parameter to the method.
Message Contract: Default SOAP message format is provided by the WCF runtime for communication between Client and service. If it is not meeting your requirements then we can create our own message format. This can be achieved by using Message Contract attribute.
1. Control the SOAP messages while sending and receiving
2. Used to add and to get the custom headers in SOAP message
3. Customize the parameters sent using SOAP message
Fault Contract: Suppose the service I consumed is not working in the client application. I want to know the real cause of the problem. How I can know the error? For this we are having Fault Contract.
1. Fault Contract provides documented view for error occurred in the service to client.
2. This helps us to easy identity, what error has occurred.
Additional Explanation about fault contract
Soap faults are not specific to any particular technology and they are based on industry standards. To support SOAP Faults WCF provides FaultException class. This class has two forms:
a. FaultException: to send untyped fault back to consumer
b. FaultException<T>: to send typed fault data to the client
WCF service also provides FaultContract attribute so that developer can specify which fault can be sent by the operation (method). This attribute can be applied to operations only.
What are the three main component of WCF?
1. Host 2. Service Class 3. Endpoint
What are the Hosting environments and it’s feature supported in WCF?
Internet information Service – It can work with Http or Https protocol. It does not require Host code to activate the service, it automatically activates service code.
Self-Hosting:
WCF service can be self hosted as console app, Win Forms or WPF application with graphical UI.
Windows Activation Service:
WAS is the new process activation mechanism that ships with IIS 7.0. In addition to HTTP based communication, WCF can also use WAS to provide message-based activation over other protocols, such as TCP and named pipes.
Windows Service:
WCF can also be hosted as a Windows Service, so that it’s under control of the Service Control Manager (SCM).
Hosting feature \
1. Executable Process/App Domain
2. Configuration (App.config/Web.config)
3. Activation (Manual at start up - self hosting / Message Based)
4. Idle Time Management
5. Health Monitoring
6. Process recycling
Note: 4, 5 ,6, and 7 has not included in self hosting
What is service host factory in WCF?
1. Service host factory is the mechanism by which we can create the instances of service host dynamically as the request comes in.
2. This is useful when we need to implement the event handlers for opening and closing the service.
3. WCF provides ServiceFactory class for this purpose.
What are the advantages of Hosting WCF in IIS
1. Provides process activation and recycling ability thereby increasing reliability
2. It is a simplified way of deployment and development of hosted services.
3. Hosting WCF services in IIS can take advantage of scalability and density features of ASP.NET
What are the advantages of hosting WCF service in WAS?
WAS (Windows Activation Service) is a component of IIS 7.0. Following are few advantages :
1. We are not only limited to HTTP protocol. We can also use supported protocols like TCP, named pipes and MSMQ
2. No need to completely install IIS. We can only install WAS component and keep away the WebServer.
What is the difference between XMLSerializer and the DataContractSerializer?
1. DataContractSerializer is the default serializer fot the WCF
2. DataContractSerializer is very fast.
3. DataContractSerializer is basically for very small, simple subset of the XML infoset.
4. XMLSerializer is used for complex schemas.
Tricky questions
1. Is it possible that both (HTTP and TCP) addresses from the same host can share a port, even on the same machine? Yes
2. What are the protocol supports reliability
NetTcpBinding, WSHttpBinding, WSFederationHttpBinding, WSDualHttpBinding
3. Following bindings supports the streaming in WCF:
BasicHttpBinding, NetTcpBinding and NetNamedPipeBinding
4. What does a WCF service application use to present exception information to clients by default?
WCF service cannot return a .NET exception to the client. The communication happened between WCF service and the client by passing SOAP messages. If an exception occurs, the WCF runtime serializes the exception into XML and passes that to the client.
5. What is the use of Is Required Property in Data Contracts?Data Contracts, is used to define Required or NonRequired data members. It can be done with a property named IsRequired on DataMember attribute.
[DataContract]
public class test
{
[DataMember(IsRequired=true)]
public string NameIsMust;
[DataMember(IsRequired=false)]
public string Phone;
}
6. When two data contract are said to equivalent?
Only if they can satisfy the following conditions
- Both the datacontracts must have same namespace
- Both the datacontracts must have same name
- The data member on data contract must have an equivalent data member on the other.
- The members in the datacontract must appear in the same order in both datacontracts.
7. Can we have a message contract as input parameter and data contract as returned parameter in a single operation contract?No, Input and return parameter should be same as Message contract if we want to use message contract in a operation contract.
8. What is Asynchronous Messaging?Asynchronous messaging describes a way of communications that takes place between two applications or systems, where the system places a message in a message queue and does not need to wait for a reply to continue processing.
9. What does the namespace "System.Runtime.Serialization" exactly do in WCF?
WCF uses the class to convert objects into a stream of data suitable for transmitting over the network (this process is known as Serialization). It also uses them to convert a stream of data received from the network back into objects (De-Serialization).
10. Why in WCF, the "httpGetEnabled" attribute is essential ?The attribute "httpGetEnabled" is essential because we want other applications to be able to locate the metadata of this service that we are hosting.
<Service metadata httpGetEnable = true>
Without the metadata, client applications can't generate the proxy and thus won't be able to use the service.
11. What is "Automatic activation" in WCF?
Automatic Activation means that the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in case of self hosting the service should always be running.
References
http://www.wcftutorial.net/
www.dotnetspider.com/
http://www.dotnetfunda.com/
No comments:
Post a Comment