You are developing a Windows Communication Foundation (WCF) service to replace an existing ASMX Web service.
The WCF service contains the following code segment. (Line numbers are included for reference only.)
01 [ServiceContract()]
02
03 public interface IEmployeeService
04 {
05 [OperationContract()]
06 EmployeeInfo GetEmployeeInfo(int employeeID);
07
08 }
09
10 public class EmployeeService : IEmployeeService
11 {
12
13 public EmployeeInfo GetEmployeeInfo(int employeeID)
14 {
15 …
16 }
17 }
18
19
20 public class EmployeeInfo
21 {
22 …
23 public int EmployeeID { get; set; }
24 public string FirstName { get; set; }
25 public string LastName { get; set; }
26
27 }
The existing Web service returns the EmployeelD as an attribute of the Employeelnfo element in the response XML.
You need to ensure that applications can consume the service without code changes in the client. What should you do?
A.
Insert the following code at line 02.
[DataContractFormat()]
Insert the following code at line 22.
[DataMember()]
B.
Insert the following code at line 02.
[XmlSerializerFormat()]
Insert the following code at line 22.
[XmlAtttibute()]
C.
Insert the following code at line 09.
[XmlSerializerFormat()]
Insert the following code at line 22.
[XmlAttribute()]
D.
Insert the following code at line 19.
[DataContractFormat()]
Insert the following code at line 22.
[DataMember()]
Explanation:
DataContractFormatAttribute Class
(http://msdn.microsoft.com/en-us/library/system.servicemodel.datacontractformatattribute.aspx)Why not A???
D and A are both wrong
It’s B (C would work as well, but less general)
“replace an existing asmx webservice” and “no changes to client code” would imply the use of XmlSerializer.
Also, the DataContractSerializer doesn’t support attributes, and there would be a need for a [DataContract] attribute as well.
DataContractSerializer class only serializes members marked with the DataMemberAttribute attribute when serializing data contract types while XmlSerializer class serializes any public member. In case of C FirstName and LastName will also be serialized.
In this case I beleive the correct answer is D.
Sure, but DCS will not serialize as an attribute, so if you choose A or D, you will need code changes in the client.
Also, since you want to return an EmployeeInfo to the caller, why wouldn’t you want it to serialize First- and LastName?! If all you return is the EmployeeID, you may as well not call the operation at all since employeeID is the input parameter.
The answer is absolutely B, since you want data serialized as XML attribute (which DataContract answers won’t do). The only choice is between B and D, but if you look at the following link, you can see that the [XmlSerializerFormat()] goes right before Interface declaration:
http://msdn.microsoft.com/en-us/library/ms733901%28v=vs.110%29.aspx
B