We have our project (it may be Windows or a web application or something else) and we want to host one WCF service within this solution locally. This type of hosting is called Self-Hosting. To implement self-hosting we need to include System.Service.Model.ServiceHost namespace.
The following are the advantages of self-hosting:
Is easy to use: With only a few lines of code you have your service running.
Is flexible: You can easily control the lifetime of your services through the Open() and Close() methods of ServiceHost<T>.
Is easy to debug: Debugging WCF services that are hosted in a self-hosted environment provides a familiar way of debugging, without having to attach to separate applications that activate your service.
Is easy to deploy: In general, deploying simple Windows applications is as easy as xcopy. You don't need any complex deployment scenarios on server farms, and the like, to deploy a simple Windows application that serves as a WCF ServiceHost.
Supports all bindings and transports: Self-hosting doesn't limit you to out-of-the-box bindings and transports whatsoever. On Windows XP and Windows Server 2003, IIS limits you to HTTP only.
The following are the disadvantages of self-hosting:
Limited availability: The service is reachable only when the application is running.
Limited features: Self-hosted applications have limited support for high availability, easy manageability, robustness, recoverability, versioning, and deployment scenarios. At least, out-of-the-box WCF doesn't provide these, so in a self-hosted scenario you have to implement these features yourself; IIS, for example, comes with several of these features by default.
A Service is a program or application that is used by other applications. In other words, a service that is directly consumed by the end user to do their work and is something they ask for and recognize Or It's just self-contained business functionality.
Windows Communication Foundation (WCF) was introduced in .NET 3.0. This is a great network distributed system developed by Microsoft for communication between applications. WCF is meant for designing and deploying distributed applications under a Service-Oriented Architecture (SOA) implementation. From MSDN: Windows Communication Foundation is a part of the .NET Framework that provides a unified programming model for rapidly building service-oriented applications that communicate across the web and the enterprise. WCF is a combined feature of Web Service, Remoting, MSMQ and COM+.
There are the following three main components of a WCF application.
1. WCF Service class: A WCF Service class implements some service as a set of methods.
2. WCF Service host: WCF supports four types of hosting, IIS, Windows Process Activation Services (WAS), self-hosting and Windows Services.
3. WCF Service endpoints: All communication with a Windows Communication Foundation (WCF) service occurs through the endpoints of the service. Usually the name of the Interface will be specified in the contract, so the client application will be aware of the operations that are exposed to the client.
Each endpoint contains:
* Address: An address that indicates where to find the service.
* Binding: A binding that specifies how a client can communicate with the service.
* Contract: A Contract that specifies what can the service do for us.
There are two types of Data Contracts
* Data Contract: Attribute used to define the class
* Data Member: Attribute used to define the properties
WCF follows the model "Software as a Service". In this model all units of functionality are defined as services and for communication, each point is a portal or connection either with the client or other services. It is a program that exposes a collection of endpoints.
The types of the transaction manager that WCF supports are
* Light Weight
* WS- Atomic Transaction
* OLE Transaction
The hosting requirements for a web service include – Availability, Reliability, Manageability, Versioning, Deployment, State, etc.
The main difference between WCF and ASMX web service is that ASMX is designed to send and receive messages using SOAP over HTTP only while WCF facilitates you to send and receive messages using any format over any transport protocol.
Following is a list of different isolation levels in WCF:
<> Read Uncommitted: Also known as dirty isolation level. It makes sure that corrupt Data cannot be read. This is the lowest isolation level.
<> Read Committed: It ensures not to read the data that has been changed by any other application and is not yet committed. It is the default level.
<> Repeatable Read: It stops the usage of dirt read and non-repeatable read. It states that data fetched through a query will be locked and will not be updated by any other transaction.
<> Serializable: It does not allow any modification and addition of new data till the transaction is completed. This is considered to be a very restrictive level.
<> Snapshot: It raises an error on modifying a data that has already been changed by any transaction.
A list of address formats and their respective bindings:
TCF Address Format - net.tcp://local host:portnumber
HTTP Address Format - http://local host:portnumber
MSMQ Address Format - net.msmq://local host:portnumber
REST stands for Representational State Transfer, for designing network application REST is used. It relies on a stateless, client server, cacheable communications protocol. The reason behind creating WCF is to support SOA and not REST. It requires a lot of configuration in order to create HTTP REST service using WCF. To overcome this tedious task, ASP.NET
When an operation has no return value, and the client does not care about the success or failure of the invocation. WCF offers one-way operations to support this sort of fire-and-forget invocation,: once the client issues the call, WCF generates a request message, but no correlated reply message will ever return to the client. The one-way message exchange pattern is useful when a client needs to send information to a service but doesn't receive a response.
Use the one-way design pattern:
When the client must call operations and is not affected by the result of the operation at the operation level.
When using the NetMsmqBinding or the MsmqIntegrationBinding class. (For more information about this scenario, see Queues in Windows Communication Foundation.)
There might be requirements where Service's Operation is not going to return any value. And at the same time client also does not bother about success or failure of service. Only for the client does calling the service matter. Client will call the operation and forget and continue with the operations.
One Way Operations
This is the way in WCF to achieve Fire and forget invocation of the service. Once the Client issues the call, WCF generates the request message and there is no corresponding response message for that. No Service side Exception will make their way to client.
Transaction propagation is the ability to propagate a transaction across the boundaries of a single service. Or in other words, we can say that a service can participate in a transaction that is initiated by a client.
In a SOA environment, transaction propagation becomes a key requirement. As we know, WCF supports SOA, so it provides support for transaction propagation as well.
WCF manages the session by creating the instance of the service class. This created instance(s) handle the incoming service request. In WCF, the session is the way of managing the services instance(s) so that the server can use these instances in an optimized way. At the server side, the InstanceContext class is used to manage service class instance. There are following instance management ways :
* Per Call
* Per Session
* Single
WCF RIA is the framework for developing the n-tier application for RIA (Rich Internet App). It is used in Rich Internet Apps like Silverlight, AJAX etc. WCF RIA is used to solve the major problems like Tight Coupling.
You can generate proxy by using the following steps:
Using Visual Studio
Using SvcUtil
There are three modes of communication in WCF:
One-Way
In the One-Way operation mode, the client will send a request to the server and does not care whether it is success or failure of service execution. There is no return from the server side, it is one-way communication.
Request-Reply
When as client makes a request to the WCF service and client will wait to get a response from service (till receiveTimeout). After getting the response it will start executing the rest of the statement.
Callback
It is the service to call the client. In which, service will act as client and client will act as service.
When a client made a request to a service, it will wait until it gets the response back.
If the response is not received until the given time, it will show an error.
If the client gets the response then next instructions will be executed.
WCF supports nine types of bindings:
1. Basic binding
2. TCP binding
3. Peer network binding
4. IPC binding
5. Web Services binding
6. Federated Web Service binding
7. Duplex WS binding
8. MSMQ binding
9 MSMQ integration binding
Concurrency management is closely related to the Instance management in WCF but both are two different things. Instance management specifies how the service instances are created while Concurrency management specifies how many concurrent requests are handled by the service instances. By using concurrency, you can make your service instance thread-safe. By default, a per-call service instance is thread-safe since each request is served by a new service instance. A per-session service instance is not thread-safe since multiple requests of a client are served by a single service instance. Hence, it’s required concurrency management. A single service instance is not thread-safe since multiple requests of all clients are served by a single service instance. Hence, it’s required concurrency management.
There is a tool called - "wcftestclient.exe" used for testing the WCF service without creating a client application. This tool can be opened from visual studio command prompt.
Transport reliability: It offers point-to-point guaranteed delivery at the network packet level, as well as guarantees the order of the packets.
Message reliability: It deals with reliability with message level independent of how many packets are required to deliver the message.
"Transport Reliability" specifies the guarantee of delivering packets over a network as protocol TCP does. It also maintains the order of the packets as well as delivering of packets.
Impersonation is a way to authorize a caller (client) identity to access the service domain’s resources. The service resources may be local files or database tables and should be on the same machine on which service is hosted. By default, impersonation is disabled and resources are accessed by using the WCF service's process identity.
After the initial deployment of the WCF service, you may need to change the service for a variety of reasons like as changing business needs or fixing others issues. Each change in your existing service introduces a new version of the service. Service versioning is helpful in backward compatibility with your existing clients.
WCF Data Services uses OData (Open Data Protocol) protocol for querying or manipulating the data. WCF Data Services is built on top of WCF REST Services. It is a RESTful service to support CRUD operations on the database using the HTTP protocol. It supports all database operations using URI. DATA protocol can expose data from the relational database, File systems, Web sites, services etc. It supports XML or JSON format for exposing the data.
REST
Representational State Transfer (REST) is a protocol for exchanging data over a distributed environment. The main idea behind REST is that we should treat our distributed services as a resource and we should be able to use simple HTTP protocols to perform various operations on that resource.
JSON
The JavaScript Object Notation (JSON) data format, or JSON for short, is derived from the literals of the JavaScript programming language. JSON helps us to present and exchange data in a self-descriptive, independent and light way. This data can then be easily consumed and transformed into JavaScript objects.
In most browser-based applications, WCF can be consumed using JavaScript, jQuery, AngularJS and so on. When a client makes a call to WCF, JSON or XML is used as the format of the communication. WCF has the option to send the response in JSON object. This can be configured with the WebGet orWebInvoke attribute and the WebHttpBinding. This allows you to expose a ServiceContract as a REST service that can be invoked using either JSON or plain XML.
GET: Retrieves the information.
PUT: Replaces the entire collection with another collection.
POST: Creates a new entry in the collection.
DELETE: Deletes the entire collection.
Exception handling is critical to all applications for troubleshooting the unexpected problems in applications. The Windows Communication Framework (WCF) provides several options for handling exceptions in WCF services. This article discusses these approaches and describes the advantages and disadvantages of each. The following options are provided to handle exceptions in WCF:
1. Using returnUnknownExceptionsAsFaults: Debugging Mode
2. Using FaultException: Best Option.
3. Using IErrorHandler: Only when the exception can't be handled by Fault
The "recieveTimeout" property is used to get/set the time interval to makes a connection active.
WCF service supports 2 styles of models:
RPC style: In RPC style we can use the serialize types and it provides the feature that is available for local calls.
Message style: In message style WCF allows the message header to be customized and it also allows us to define the security for body and header messages.
In WCF one-way mode, a client sends a request to the server but does not wait till the response comes. It does not care if the request is failed or succeeded. The Client will not be blocked in this case until it receives the response.
The WCF callback mode is a special type of mode where WCF on the call back calls the method of client and in this scenario WCF service acts as a client and client acts as a service.
This mode is not supported by "HTTPBinding" so "WSDualHttpBinding" is used in this mode.
These are three types of transaction managers supported by WCF:
1. Light Weight
2. WS-Atomic Transaction
3. OLE Transaction
In WCF, "Throttling" is used to limit the sessions or instances to be created at an application level. It is used to boost the performance.
The "maxConcurrentCalls" attribute in throttling is used to limit the total number of calls which are going to the service instances. Its default value is 16.
The KnowType is an attribute which is used to the parent class because if you define the complex class as the property of class then it would be very difficult for the compiler during the de-serialization process.
In WCF any receiving message is delivered only once entire message has been received. What I mean here is that first message is buffered at the receiving side and once it is fully received it gets delivered to the receiving end. Main problem with this approach is that receiver end is unresponsive while message is getting buffered. So default way of message handling in WCF is ok for small size messages but for the large size messages this approach is not good. So to overcome this problem Streaming in WCF come into action.
The three types of transaction managers that WCF has are as follows…Light Weight, WS-Atomic Transaction and OLE Transaction.
The namespace that is used to access WCF is known as System.ServiceModel.
The different types of binding which are there in WCF are as follows… BasicHttpBinding, NetMsmqBinding, NetTcpBinding and WSHttpBinding.
The full form of REST is Representational State Transfer.
The various isolation levels in WCF are as follows… Read Uncommitted, Read Committed, Repeatable Read and Serializable.
The contract that is used to correct mistakes is known is Fault Contract.
WCF is a program for generating the services application. To make the interaction convenient from MS messaging, remoting, Queuing, services, .NET and j2EE. It is very easy to use on protocols such as HTTP, TCP, msmq. Conveying the data from one to other customers.
Representational State Transfer, It is a way to give standards to protocols on the online for PC conversation. It is used to define resources, changing the information, scalability, data items.
Instant management is the set of techniques that tie up the customer’s appeal for immediate services, It is a way for deciding, when and which services instance will serve a client.
We can define in advance the type of consideration at the time of deserialization. The endpoints of client and service share the data contract for transfer at the time of returning values and parameters.
A Fault Contract, is used to manage the client to try and catch blocks. SQL Server does not respond when the customer calls the service class. FaultExpection class is supported by SOAP Faults.
Ria services, It is for standard versions and action to create a difficult business application in Silverlight, AJAX by the validation from the client-side. It is also the perfect solution to ASP.NET and Silverlight by connecting them.