0

I have a windows service where I create a list of objects and I want to pass this list to my windows service.

So first of all I have two classes Person and Persons. Person just describes the Person and Persons makes a list of Person objects. Then I want to pass this list to my wcf service but I get an error because I don't have a DataContract for this list. The two classes are in my windows service.

How can I give those classes DataContract and then pass the list?

I couldn't find anything about implementing external classes into an wcf service.

WindowsService

Person

public class Person()
{
 public string Age{get; set;}
 public string Path{get; set;}

 public Person(string age, string path)
 {
  this.Age = age;
  this.Path = path;
 }
}

-Persons-

public class Persons()
{
 private List<Person> personlist;

 public Persons()
 {
  personlist = new List<Person>();
 }
}

WCFService

WindowsServiceMethod wsm = new WindowsServiceMethod(); //Just imagine a method in my windows service

public List<Person> GetPerson(string path)
{
 List<Person> personlist = wsm.GetProjects(path);
}
Mr. Paul
  • 103
  • 3
  • 16

3 Answers3

0

You'll need to do some form of IPC or RPC, personally I'd simply create an endpoint in your Windows service that exposes a Named-Pipes endpoint, this would then allow you to communicate with it from your WCF SOA endpoint.

WCF named pipe minimal example

And yes, I said embed this service inside your windows service side.

Your POCO classes can either be datacontracts or serializable. I don't see that in your code.

Here is what you should do:

You should first mark the Person(s) classes as a DataContract and mark the properties as DataMembers BUT you must ALSO have a class with the SAME DataContract name in the WCF side. The DataContract classes must exist in both app domains, they don't have to be the same actual classes but they must have the same DataContract name.

However if you are referencing the assembly that contains the DataContract classes from both projects then you should be fine.

Suggestion:

Create a single assembly that contains your DataContracts (if possible) and reference this single assembly from both projects. If that is too much work then simply create a DataContract in both (class names can be different), again what matters is that there is a class that has the name of the DataContract on the receiving side as well as the sending side.

Community
  • 1
  • 1
T McKeown
  • 12,971
  • 1
  • 25
  • 32
  • That is exactly what I am trying. I have an NetNamedPipesBinding endpoint in my WCF I just don't now how to go on. The part with the DataContract causes the biggest trouble. – Mr. Paul Jan 13 '16 at 14:43
0

You have to add the [DataContract] Attribute to person class and [DataMember] to child properties from person class you want to expose:

[DataContract]
public class Person()
{
  [DataMember]
  public string Age{get; set;}
  [DataMember]
  public string Path{get; set;}

 public Person(string age, string path)
 {
  this.Age = age;
  this.Path = path;
 }
}
Souhaieb Besbes
  • 1,485
  • 17
  • 30
0

Sorry for my english :) I'm not sure if I understood but maybe is referring the object the list contains, in that case the Person class... Try to decorate your Person class with the attribute [DataContract] and his properties with the attribute [DataMember]: Ex.

using System.Runtime.Serialization;

[DataContract]
public class Person()
{

 [DataMember]
 public string Age{get; set;}

 [DataMember]
 public string Path{get; set;}

 public Person(string age, string path)
 {
  this.Age = age;
  this.Path = path;
 }
}

Using Data Contracts