.Net Interview Questions
Ir al canal en Telegram
1 794
Suscriptores
+124 horas
+207 días
+4930 días
Archivo de publicaciones
The preferred way for serializing complex types in WCF is to use data contracts. Using Data Contracts provides us with the following advantages.
1. Using DataMember attribute, you can control which members of the class to serialize.
2. You can also control the order in which members are serialized using Order parameter of the DataMember attribute..
3. You can also provide explicit Name to the serialized members using Name parameter of the DataMember attribute.
4. You can also specify if a member is required or optional using IsRequired parameter of the DataMember attribute.
Consider the example below which uses Name, IsRequired and Order parameters of the DataMember attribute to serialize CustomerId property. By the way DataMember attribute can be used with either fields or properties. If you donot specify the order in which members are serialized, then by default alphabetical ordering is done by the DataContractSerializer.
🚩2️⃣6️⃣6️⃣ What is the preferred way for serializing complex types in WCF?
When we decorate a class with Serializable attribute, all the fields of the class are serialized regardless of the accessibility. We donot have control on what to serialize and what not to serialize. We also will not have any control over naming conventions or data types.
🚩2️⃣6️⃣5️⃣ What is the disadvantage of using Serializable attribute to serialize a complex type that is sent and received between clients and services in WCF?
The following are the different options available to serialize complex types that are exchanged between clients and services in WCF. These options have their own advantages and disadvanatages. Data contracts is thepreferred way to serialize complex types in WCF.
1. Serializable types - Us the Serializable attribute on the type that you want to serialize
2. Data contracts - Use DataContract attribute on the type and DataMember attribute on every member of the type, that you want to serialize. You can apply DataMember attribute either on a filed or a property.
3. Known types - Use Known types to enable polymorphic behavior in service contracts.
4. IXmlSerializable - IXmlSerializable types provide XSD schema to Web Services Description Language (WSDL) and metadata exchange (MEX).
🚩2️⃣6️⃣4️⃣ What are the different options available to serialize complex types that are sent and received between clients and services in WCF?
Normally, by default, when some exception occurs at a WCF service level, it will not be exposed as it is to the client. The reason is that the WCF exception is a CLR exception and it doesn't make sense to expose it outside of the CLR because it contains internal details of service code like stack trace. So, WCF handles and returns error details to the client using a Fault Contract.
So, a fault contract is a contract that contains the details of possible exception(s) that might occur in service code
MessageParameter attribute is used to control the parameter and returned object names from a service operation. Consider the example below. On the service side, the method parameter name in SaveCustomer([MessageParameter(Name = "Customer")] Customer cust) is cust. If we donot use MessageParameter attribute, then "cust" is what is exposed as parameter name to the client, which is not very proffesional. So we are using MessageParameter attribute to expose the method parameter name as Cutomer.
Yes, a ServiceContract attribute can be applied either to a class or an interface, but defining service contracts using interfaces rather classes has the following benifits.
1. Defining service contracts using interfaces, removes coupling to service implementation. Later the implementation can be changed at will without affecting the clients.
2. Defining service contracts using interfaces, also allows a service to implement more than 1 contract.
🚩2️⃣6️⃣1️⃣ Can you apply service contract attribute to a class rather than an interface in WCF?
2. Operation Contract - All methods in a service contract should
have OperationContract attribute. You can also provide explicit Name, Action
and ReplyAction as shown in the example below.
1. Service Contract - An interface that exposes the service operations is usually decorated with the service contract attribute. Always provide meaningful Namespace and Name to a service contract as shown in theexample below.
Service Utility (svcutil.exe) can be used by the clients to generate the proxy and configuration file. For the client to be able to generate proxies, the service should enable metadata exchange.
🚩2️⃣5️⃣9️⃣ What is the tool that a client application can use to generate the proxy for a WCF service?
WSDL stands for Web Service Description Language. The WCF service exposes the WSDL document for the clients, to generate proxies and the configuration file. The WSDL file provides the following information for the consumers of the WCF service. Provides the following points.
1. The service contract and operations available.
2. All the end points exposed by the WCF service.
3. Messages and types that can be exchanged between the client and the WCF service.
4. WSDL also provides any information about the policies used.
