WCF Interview questions--Part 4


WCF Interview Questions on Contracts - Part 4
Explain the significane of MessageContract attribute?
OR
Why and When do you use MessageContract attribute?

There are several advantages of using MessageContract attribute in WCF. MessageContract attribute can be used for

1. Adding custom headers to the message.

2. Controling message wrapping.

3. Controling signing and encryption of messages.

MessageContract attribute provides us with greater control over message headers and body elements. MessageContract attribute converts a type to a SOAP message. The example below shows how to use IsWrapped and ProtectionLevel parameters of MessageContract attribute. You may also set an explicit Name and Namespace.

MessageContract attribute is supported by MessageHeader attribute and MessageBodyMember attribute. You can apply MessageHeader attribute to fields or properties of message contract. This is a simple technique for creating custom headers.You can provide Name, Namespace and ProtectionLevel. You may also set SOAP protocol settings like Relay, Actor, MustUnderstand.

MessageBodyMember attribute can also be Applied to fields or properties of message contract.Can have several body elements. This is equivalent to multiple parameters in operation and this is the only way to return multiple complex types. It is suggested as a good practice to always supply Order. You can also set Name, Namespace, ProtectionLevel. The example below shows how to use MessageHeader and MessageBodyMember attributes.

WCF Interview Questions on Contract Versioning

What is WS-Policy?

OR

What is Web Services Policy?

Web Services Policy or WS-Policy is an interoperable standard for describing policies that influence communication with the clients. Usually WS-Policy is included in the WSDL contract exposed by the WCF service, although it is optional.

What is the use of WS-Policy?

WS-Policy is generally used for

1. Describing protocols for accessing operations

2. Security

3. Reliable messaging

4. Transactions

5. Message encoding (Message Transmission Optimization Mechanism [MTOM])

6. Other protocols

You can specify the above settings in WSDL directly without a policy section, but the disadvantage is that, once published, the WSDL contract is final. If the clients has to communicate with a WCF service that has changed the settings in the WSDL, the clients need to rebuild the proxy and configuration or atleast the changes to the WSDL contract must support backward compatibility.

The advantage of using WS-Policy is that it can change over time, and the clients can discover the changed policy to communicate via metadata exchange. But keep in mind that, you can only change the policy safely if clients are positioned to handle dynamic changes.

Are WCF Contracts version tolerant?

Yes, WCF contracts are version tolerant by default. Service contracts, data contracts, and message contracts forgive missing and non required data. They also Ignore any superfluous or additional data sent by the client to the service. The DataContractSerializer provides tolerance. Reasonable changes can be made without impacting existing clients.

The following table summarizes the changes to a service contract and impact on existing clients.

What is IExtensibleDataObject

What is IExtensibleDataObject?

OR

What is the advantage and disadvantage of implementing IExtensibleDataObject?

WCF guidelines recommend enhancing all data contracts with support of IExtensibleDataObject interface, to preserve unexpected data from clients. During deserialization, superfluous data is placed in a dictionary on the service side and during serialization, the same data is written as XML as it was originally provided by the client. This is very useful to preserve data from version 2.0 services at a version 1.0 client. It is also useful in case where downstream calls from version 2.0 services go to other services handling version 1.0.

However, there is also a disadvantage of implementing IExtensibleDataObject. It carries risks of denial of service (DoS) and unnecessary use of server resources.

We can turn on and off, the support for IExtensibleDataObject either in code declaratively using attributes or in the configuration file as shown below.

Interview Questions - Exception Handling in WCF

What are SOAP Faults in WCF?

Common language runtime (CLR) exceptions do not flow across service boundaries. At the maximum, a CLR exceptions may propagate up to the service tier from business components. Unhandled CLR exceptions reach the service channel and are serialized as SOAP faults before reporting to clients. An unhandled CLR exception will fault the service channel, taking any existing sessions with it. That is why it is very importatnt to convert the CLR exceptions into SOAP faults. Where possible, throw fault exceptions

SOAP faults are standards based and interoperable. There are 2 formats used by SOAP faults, SOAP 1.1 and SOAP 1.2. SOAP format depends on the binding used.

What happens if there is an unhandled exception in WCF?

If there is an unhandled exception in WCF, the the service model returns a general SOAP fault, that does not include any exception specific details by default. However, you can include exception details in SOAP faults, using IncludeExceptionDetailsInFaults attribute. If IncludeExceptionDetailsInFaults is enabled, exception details including stack trace are included in the generated SOAP fault. IncludeExceptionDetailsInFaults should be enabled for debugging purposes only. Sending stack trace details is risky.

What are the 2 ways to enable IncludeExceptionDetailsInFaults for a WCF service?

Either programatically or thru configuration.

To enable thru code use ServiceBehaviorAttribute as shown below:

[ServiceBehaviourAttribute(IncludeExceptionDetailsInFaults=true)]

public class PragimService : IPragimService

To enable using configuration use servicedebug element as shown below

No comments:

Post a Comment