What is WCF?
WCF provides a runtime environment for services, enabling to expose Common Language Runtime (CLR) types as services and to consume other services as CLR types
Why WCF?
Easy to develop services - WCF provides developers with the essential off-the-shelf plumbing required by almost all applications and, as such, it greatly increases productivity.
WCF provides facilities:
1. Hosting
2. Service Instance Management
3. Asynchronous calls
4. Reliability
5. Transaction management
6. Disconnected queued calls
7. Security
8. Discovery
9. Routers
10. Extends supports to Windows Azure AppFabric Service Bus
WCF Supports extensibility Model.
Most of the WCF functionality is included in a single assembly called System.SericeModel.dll, located in the System.ServiceModel namespace.
Service:n
A service is a unit of functionality exposed to the world.
Service Orientation (SO) is an abstract set of principles and best practices for building service-oriented applications.
A service-oriented application aggregates services into a single logical application.
Similar to component-oriented application aggregates component
Object-oriented application aggregates objects.
WCF provides a runtime environment for services, enabling to expose Common Language Runtime (CLR) types as services and to consume other services as CLR types
Why WCF?
Easy to develop services - WCF provides developers with the essential off-the-shelf plumbing required by almost all applications and, as such, it greatly increases productivity.
WCF provides facilities:
1. Hosting
2. Service Instance Management
3. Asynchronous calls
4. Reliability
5. Transaction management
6. Disconnected queued calls
7. Security
8. Discovery
9. Routers
10. Extends supports to Windows Azure AppFabric Service Bus
WCF Supports extensibility Model.
Most of the WCF functionality is included in a single assembly called System.SericeModel.dll, located in the System.ServiceModel namespace.
Service:n
A service is a unit of functionality exposed to the world.
Service Orientation (SO) is an abstract set of principles and best practices for building service-oriented applications.
A service-oriented application aggregates services into a single logical application.
Similar to component-oriented application aggregates component
Object-oriented application aggregates objects.
- The services can be local or remote,
- Can be developed by multiple parties using any technology
- Can even execute on different timelines
- Inside a service we can find concepts such as languages, technologies, platforms, versions, and frameworks, yet between services, only prescribed communication patterns are allowed.
The client of a service is merely the party consuming its functionality. The client can be anything - for instance, a Windows Forms, WPF, Or Silverlight class, an ASP.NEt page, or another service.
WCF messages are independent of protocols, unlike webservices, WCF services may communicate with various protocols.
WCF service exposes metadata describing the available functionality and possible ways bf communicating with the service. Metadata is published in predefined , technology neutral way, such as WSDL over HTTP-GET or an industry standard for metadata exchange over any protocol.
A non-WCF client can import the metadata to its native environment as native types. Similarly, a WCF client can import the metadata of a non-WCF service and consume it as native CLR classes and interfaces.
Service Execution Boundaries:
With WCF, the client never interacts with a service directly, even when dealing with a local, in-memory service. Instead, the client always uses a proxy to forward calls to the service. The proxy exposes the same operations as the service, plus some proxy-management methods.
WCF allows the client to communicate with a service across all execution boundaries. On the same machine, the client can consume services in the same app domain, across app domains in the same process, or across processes. Across the machine boundaries, the client can interact with services in its intranet or across the Intranet.
Beacuse all interactions are done via a proxy, requiring the same configuration and hosting, WCF maintains the same programming model for the local and remote cases; thus, it not only enables you to switch locations without affecting the client, but also significantly simplifies the application programming model. Another important benefit of always using a proxy is that it enables WCF to intercept the call and add its value.
Addresses:
WCF supports the following transport schemes:
HTTP(80)/ HTTS(443)-
Two Host can share same port:
http://localhost:8001
http://localhost:8001/MyService
TCP(Default:808):
Two Host can share same port:
net.tcp//localhost:8002/MyService
IPC: Inter-Process Communication :
Accepts call from the same machine.
We can open a named pipe only once per machine.
.net.pipe://localhost/MyPipe
Peer network:
MSMQ:
When we are dealing with private queues, we must specify queue type, but we can omit that for the public queues.
net.msmq://localhost/private/MyQueue
net.msmq://localhost/MyQueue
Service Bus:
sb://MyNamespace.servicebus.windows.net/
The way to read an address:
"Using HTTP, go to the machine called localhost, where on port 8001 someone called MyService is waiting for my calls".
Contracts:
The contract is a platform-neutral and standard way of describing what the service does.
Service Contracts: Describe which operations the client can perform on the service.
Data Contracts: Defines which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.
Fault Contracts: Define which errors are raised by the service and how the service handles and propagates errors to its clients.
Message Contracts:
Allow the service to interact directly with messages. Message contracts can be typed or untyped and are useful in interopratability cases when another party has already dictated some explicit (typically proprietary) message format.
Service Contract:
Service contract attribute must be explicitly provided on class or interface, if not provided then the interface is not visible to WCF clients, in line with the service-oriented tenet that service boundaries should be explicit.
OperationContractAttribute should be explicitly added to methods to expose as WCF contracts. This enforces explicit service boundaris and maintains an explicit option model for operation themselves.
In addition, a contract operation cannot use object reference as parameters: only primitive types or data contracts are allowed.
Applying the ServiceContract attribute:
A single class can support multiple contracts by deriving and implementing multiple interfaces decorated with the ServiceContract attribute.
Constraints of the service implementation class: We should avoid parameterized constructors, because WCF will only use the default constructor. Also, although the class can use internal properties, indexers, and static members, no WCF client will ever be able to access them.
Avoid using the ServiceContract attribute directly on the service class. Always define a separate contract so that you can both consume it independently of the class and have other classes implement it.
Name and namespaces: to scope a type of contract and reduce the overall chance of collision.
By default, the exposed name of the contract will be the name of interface. However, we can use alias for a contract to expose a different name to the clients in the metadata, by using the Name property of the ServiceContract attribute.
Similarly, the name of the publicly exposed operation defaults to the method name, but you can use the Name property of the OperationContract attribute to alias it to a different publicly exposed name.
Hosting:
A single host process can host multiple services, and the same service type can be hosted in multiple host process.
A separate host process for client promotes fault and security isolation.
Host Process:
WCF messages are independent of protocols, unlike webservices, WCF services may communicate with various protocols.
WCF service exposes metadata describing the available functionality and possible ways bf communicating with the service. Metadata is published in predefined , technology neutral way, such as WSDL over HTTP-GET or an industry standard for metadata exchange over any protocol.
A non-WCF client can import the metadata to its native environment as native types. Similarly, a WCF client can import the metadata of a non-WCF service and consume it as native CLR classes and interfaces.
Service Execution Boundaries:
With WCF, the client never interacts with a service directly, even when dealing with a local, in-memory service. Instead, the client always uses a proxy to forward calls to the service. The proxy exposes the same operations as the service, plus some proxy-management methods.
WCF allows the client to communicate with a service across all execution boundaries. On the same machine, the client can consume services in the same app domain, across app domains in the same process, or across processes. Across the machine boundaries, the client can interact with services in its intranet or across the Intranet.
Beacuse all interactions are done via a proxy, requiring the same configuration and hosting, WCF maintains the same programming model for the local and remote cases; thus, it not only enables you to switch locations without affecting the client, but also significantly simplifies the application programming model. Another important benefit of always using a proxy is that it enables WCF to intercept the call and add its value.
Addresses:
WCF supports the following transport schemes:
HTTP(80)/ HTTS(443)-
Two Host can share same port:
http://localhost:8001
http://localhost:8001/MyService
TCP(Default:808):
Two Host can share same port:
net.tcp//localhost:8002/MyService
IPC: Inter-Process Communication :
Accepts call from the same machine.
We can open a named pipe only once per machine.
.net.pipe://localhost/MyPipe
Peer network:
MSMQ:
When we are dealing with private queues, we must specify queue type, but we can omit that for the public queues.
net.msmq://localhost/private/MyQueue
net.msmq://localhost/MyQueue
Service Bus:
sb://MyNamespace.servicebus.windows.net/
The way to read an address:
"Using HTTP, go to the machine called localhost, where on port 8001 someone called MyService is waiting for my calls".
Contracts:
The contract is a platform-neutral and standard way of describing what the service does.
Service Contracts: Describe which operations the client can perform on the service.
Data Contracts: Defines which data types are passed to and from the service. WCF defines implicit contracts for built-in types such as int and string, but we can easily define explicit opt-in data contracts for custom types.
Fault Contracts: Define which errors are raised by the service and how the service handles and propagates errors to its clients.
Message Contracts:
Allow the service to interact directly with messages. Message contracts can be typed or untyped and are useful in interopratability cases when another party has already dictated some explicit (typically proprietary) message format.
Service Contract:
Service contract attribute must be explicitly provided on class or interface, if not provided then the interface is not visible to WCF clients, in line with the service-oriented tenet that service boundaries should be explicit.
OperationContractAttribute should be explicitly added to methods to expose as WCF contracts. This enforces explicit service boundaris and maintains an explicit option model for operation themselves.
In addition, a contract operation cannot use object reference as parameters: only primitive types or data contracts are allowed.
Applying the ServiceContract attribute:
A single class can support multiple contracts by deriving and implementing multiple interfaces decorated with the ServiceContract attribute.
Constraints of the service implementation class: We should avoid parameterized constructors, because WCF will only use the default constructor. Also, although the class can use internal properties, indexers, and static members, no WCF client will ever be able to access them.
Avoid using the ServiceContract attribute directly on the service class. Always define a separate contract so that you can both consume it independently of the class and have other classes implement it.
Name and namespaces: to scope a type of contract and reduce the overall chance of collision.
By default, the exposed name of the contract will be the name of interface. However, we can use alias for a contract to expose a different name to the clients in the metadata, by using the Name property of the ServiceContract attribute.
Similarly, the name of the publicly exposed operation defaults to the method name, but you can use the Name property of the OperationContract attribute to alias it to a different publicly exposed name.
Hosting:
A single host process can host multiple services, and the same service type can be hosted in multiple host process.
A separate host process for client promotes fault and security isolation.
Host Process:
- ISS ( Internet Information Services)
- WAS (Windows Activation Services)
- Windows Server AppFabric
In-process (or in-proc) hosting, where the service resides in the same process as the client, is a special case. By defination, the developer provides the host for the in-proc case.
IIS 5/6 Hosting:
Advantage: Host process is launched automatically upon the first client request.
Disadvantage with IIS 5/6: Only HTTP
.svc similar to .asmx: is used to identify code behind the file and class
The Web.Config file:
Lists the types we want to expose as services. We need to use fully qualified type names, including the assembly names if the service type comes from an unreferenced assembly
IIS 5/6 Hosting:
Advantage: Host process is launched automatically upon the first client request.
Disadvantage with IIS 5/6: Only HTTP
.svc similar to .asmx: is used to identify code behind the file and class
The Web.Config file:
Lists the types we want to expose as services. We need to use fully qualified type names, including the assembly names if the service type comes from an unreferenced assembly