Collection in WCF Service

As we know Collection is very much specific to .Net,  WCF does not expose it to the metadata of the service. WCF provides marshaling rules for collection.

Actually, Collections are exposed as array in Service metadata.

Let us understand it with the help of an example.

Iservice1.cs

[ServiceContract]
public interface IStudentService
{
[OperationContract]
IEnumerable<StudentInformation> GetStudentInfo(int studentId);

[OperationContract]
List<StudentInformation> GetStudentData();
}

Service1.svc.cs
public class StudentService : IStudentService
{
List<StudentInformation> Students = new List<StudentInformation>();

public IEnumerable<StudentInformation> GetStudentInfo(int studentId)
{
IEnumerable<StudentInformation> Student = from student in Students
where student.StudentId == studentId
select student;

return Student;
}

public List<StudentInformation> GetStudentData()
{
List<StudentInformation> Student = (from student in Students
select student).ToList();

return Student;
}
}

When we add this service reference at the client side, we can see the collection is exposed as array.

On Client-side, if we try to access returned value of the service method

StudentServiceClient objClient =  new StudentServiceClient();
List<StudentInformation> studentList = objClient.GetStudentData();

we will face following error:

Cannot Implicitly convert type ‘StudentService.StudentInformation[]’ to System.Collections.Generic.List<StudentService.StudentInformation>.

Since, we are returning collection from the service but at the client side we are getting array. There is a disparity at the service end (Collection) and client end (Array). To overcome from this issue, we need to change the setting while adding service reference. While adding service reference, click on “Advance” button. It will open “Service Reference Settings” window. On this window, choose appropriate option for Collection Type i.e. System.Collections.Generic.List option.

WCF_Collection_Settings

Now following code will run smoothly without any issue at the client side,

IEnumerable<StudentInformation> objStudent = objClient.GetStudentInfo(Convert.ToInt16(txtStudentId.Text));

List<StudentInformation> lstStudent = objClient.GetStudentData();

I hope this article will be useful to you. Thanks for reading. Happy Coding .

Posted in Uncategorized | Leave a comment

Serializing and Deserializing Objects…to and from…XML

How do we serialize objects to XML and deserialize the XML back into objects. Let us understand it with the help of an example.

public class MyClass
{

// school property
private int _Age;

public int Age
{
get { return _Age; }
set { _Age = value; }
}

// new school property
public bool Citizen { get; set; }

// there’s nothing wrong with using fields
public string Name;

}

/// <summary>
/// Serializes an object to an XML string
/// </summary>
/// <param name=”AnyObject”>Object to serialize</param>
/// <returns>XML string</returns>

public static string SerializeAnObject(object AnyObject)
{
XmlSerializer Xml_Serializer = new XmlSerializer(AnyObject.GetType());
StringWriter Writer = new StringWriter();

Xml_Serializer.Serialize(Writer, AnyObject);
return Writer.ToString();
}

/// <summary>
/// DeSerialize an object
/// </summary>
/// <param name=”XmlOfAnObject”>The XML string</param>
/// <param name=”ObjectType”>The type of object</param>
/// <returns>A deserialized object…must be cast to correct type</returns>

public static Object DeSerializeAnObject(string XmlOfAnObject, Type ObjectType)
{
StringReader StrReader = new StringReader(XmlOfAnObject);
XmlSerializer Xml_Serializer = new XmlSerializer(ObjectType);
XmlTextReader XmlReader = new XmlTextReader(StrReader);

try
{
Object AnObject = Xml_Serializer.Deserialize(XmlReader);
return AnObject;
}
finally
{
XmlReader.Close();
StrReader.Close();
}
}

Below is the sample code which demonstrate how to use thess functions.

protected void Button1_Click(object sender, EventArgs e)
{
// create and initialize an object
MyClass Test = new MyClass();

Test.Age = 18;
Test.Name = “Rashid”;
Test.Citizen = true;

//  Serialize it

String XML;
XML = MiscUtilities.SerializeAnObject(Test);

// Deserialize it
MyClass Test2;

Test2 = MiscUtilities.DeSerializeAnObject(XML, typeof(MyClass)) as MyClass;

// TODO:  Get a cup of Tea and bask in the glory of rock solid code.

}

Happy Learning !

Posted in Uncategorized | Leave a comment

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

Posted in Uncategorized | 1 Comment

Birthday Party.

Hi 2 All,
First of all sorry for late in posting new entry in my blog. Yesterday i celebarated my b’lated birthday party with my team mates and other colleagues. Really, it was a great experience for me and a memorable moment in my life. I thanks to all my team mates Specially Archana Mam, Sachin, Anil, Ravi, Sanjay and Janmaya. Also thanks to all my colleagues who came in the party and make it memorable.
 
Right now there is a song coming in my mind from the movie "Yaadein".
 
Yaadein yaad aati hai
Yaadein yaad aati hai
Yeh yaadein kisi dil-o-jaanam ke
Chal jaane ke baad aati hain
Yaadein, yaadein, yaadein !

 

Once again thanks a lot in coming and attending my party .

Posted in Entertainment | Leave a comment

A Very Happy New Year.

 Hi 2 All ,

 May this New Year be filled

 with Sweeteness and Happiness

 in all that you do and may this

 Joy continues the whole year through.

 
 
Posted in messages | Leave a comment

Marriage Proposal

Marriage Proposal!

 

Dear Ms. ABCDEFGHI,

    Baby, I ‘v seen you yesterday while surfing on local train platform and realized that you are the only site I was browsing for. For long    time, I have been lonely; trying to find a bug in my life and you can be a real debugger for me now. My life is just an uncompiled program without you, which never produces an executable code and hence is useless. You are not only beautiful by face but all your ActiveX controls are attractive as well.Your smile is so delightful, which encourages me and gives power to me equal to thousands of mainframes processing power. When you looked at me last evening, I felt like all my program modules were running smoothly and giving expected results. /* which I never experienced before*/.

 

          With this letter, I just want to convey to you that, if we linked together, I’ll provide you all objects & libraries necessary for a human being to live an error free life. Also don’t bother about the firewall which may be created by   our parents as I’ve strong hacking capabilities by which I’ll ultimately break their security passwords and make them agree for our marriage. I anticipate that nobody is already logged in to your database so that my connect script will fail. And it’s all certain that if this happened to me, I will crash my system beyond recovery.

 

Kindly interpret this letter properly and grant me all privileges of your inbox.

 

Only yours,

 XYZ Software Professional

Posted in Entertainment | 1 Comment

Aa bhi jao ….

 Aa bhi jaao …

 Aa bhii jaao kii zindagii kam hai

 Tum nahiin ho to har Khushii kam hai

 Vaadaa kar ke ye kaun aayaa nahiin

 Shahar mein aaj raushanii kam hai

 Jaane kyaa ho gayaa hai mausam ko

 Dhuup ziyaadaa hai chaandnii kam hai

 Aaiinaa dekh kar Khayaal aayaa

 Aaj kal un kii dostii kam hai

 Tere dam se hii main mukammal huun

 Bin tere terii ‘Dosti’ kam hai . 

Posted in Entertainment | Leave a comment