Reputation: 5011
Okay this is the scenario !! I know I am definitely making some mistakes but I want to clear out the basics and I want to implement them successfully. I have an Employee class which is a DataContract in WCF and FullTimeEmployee and PartTimeEmployee extend the class. In my EmployeeService we return the objects of FullTimeEmployee and PartTimeEmployee based on user inputs respectively from a console application client(Only in Demo purpose). Now I want to tweak the SOAP messages by MessgaeContract. I want the request object to be called EmployeeRequestObject and the response object the EmployeeResponseObject and I want to include same license Keys in their headers.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace EmployeeServiceLibrary
{
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
EmployeeResponse GetEmployee(EmployeeRequest request);
}
#region EmployeeRequestObject
[MessageContract(IsWrapped=true,WrapperName="EmployeeRequestObject",WrapperNamespace="http://Chiranjib_VAIO.com/")]
public class EmployeeRequest
{
[MessageBodyMember(Namespace = "http://Chiranjib_VAIO.com/")]
public int EmployeeRequestID { get; set; }
[MessageHeader(Namespace = "http://Chiranjib_VAIO.com/")]
public string EmployeeRequestLicenseKey { get; set; }
}
#endregion
#region EmployeeResponseObject
public class EmployeeResponse
{
public EmployeeResponse()
{
}
public EmployeeResponse(EmployeeRequest e)
{
this.EmployeeResponseLicenseKey = e.EmployeeRequestLicenseKey+"_Response";
}
[MessageHeader(Namespace = "http://Chiranjib_VAIO.com/")]
public string EmployeeResponseLicenseKey { get; set; }
}
#endregion
[KnownType(typeof(FullTimeEmployee))]
[KnownType(typeof(PartTimeEmployee))]
[DataContract]
public abstract class Employee
{
[DataMember]
public int EmployeeId { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Gender { get; set; }
[DataMember]
public EmployeeType EmpType { get; set; }
}
public enum EmployeeType
{
FullTimeEmployee=0,
PartTimeEmployee=1
}
[DataContract]
public class FullTimeEmployee : Employee
{
[DataMember]
public decimal AnnualSalary { get; set; }
}
[DataContract]
public class PartTimeEmployee : Employee
{
[DataMember]
public int HoursWorked { get; set; }
[DataMember]
public decimal SalaryPerHour { get; set; }
}
}
When User inputs a 0 I want a FullTimeEmployee Object to be returned and when inputs a 1 I want a PartTimeEmployee Object to be returned. Here is the Service file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace EmployeeServiceLibrary
{
public class EmployeeService : IEmployeeService
{
public EmployeeResponse GetEmployee(EmployeeRequest request)
{
int empId = request.EmployeeRequestID;
switch (empId)
{
case 0:
return new FullTimeEmployee()
{
EmployeeId=1,
Name="Chiranjib Nandy",
Gender="Male",
EmpType=EmployeeType.FullTimeEmployee,
AnnualSalary=1500
};
case 1:
return new PartTimeEmployee()
{
EmployeeId = 1,
Name = "Archana Nandy",
Gender = "Female",
EmpType = EmployeeType.PartTimeEmployee,
HoursWorked=9,
SalaryPerHour=150
};
default: return null;
}
}
}
}
But the error is saying cannot implicitly convert EmployeeServiceLibrary.FullTimeEmployee to EmployeeServiceLibrary.EmployeeResponse. How to achieve the desired behavior ? Helps are badly appreciated !! Thanks
Upvotes: 0
Views: 218
Reputation: 149518
Your code wont compile, as both your return types implement Employee
, not EmployeeResponse
. You can encapsulate a Employee
object inside your EmployeeResponse
public class EmployeeResponse
{
public EmployeeResponse(EmployeeRequest e)
{
this.EmployeeResponseLicenseKey = e.EmployeeRequestLicenseKey+"_Response";
}
[MessageHeader(Namespace = "http://Chiranjib_VAIO.com/")]
public string EmployeeResponseLicenseKey { get; set; }
public Employee Employee { get; set; }
}
And then modify your code to:
public EmployeeResponse GetEmployee(EmployeeRequest request)
{
int empId = request.EmployeeRequestID;
switch (empId)
{
case 0:
return new EmployeeResponse
{
Employee = new FullTimeEmployee()
{
EmployeeId=1,
Name="Chiranjib Nandy",
Gender="Male",
EmpType=EmployeeType.FullTimeEmployee,
AnnualSalary=1500
}
}
case 1:
return new EmployeeResponse
{
Employee = new PartTimeEmployee()
{
EmployeeId = 1,
Name = "Archana Nandy",
Gender = "Female",
EmpType = EmployeeType.PartTimeEmployee,
HoursWorked=9,
SalaryPerHour=150
}
}
default: return null;
}
Upvotes: 1