简体   繁体   English

将对象列表从Windows服务传递到WCF服务

[英]Pass a list of objects from a windows service to an wcf service

I have a windows service where I create a list of objects and I want to pass this list to my windows service. 我有一个Windows服务,在其中创建对象列表,并且希望将此列表传递给Windows服务。

So first of all I have two classes Person and Persons. 因此,首先我有两个类Person和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. 然后,我想将此列表传递给我的wcf服务,但是由于我没有针对该列表的DataContract,因此收到错误消息。 The two classes are in my windows service. 这两个类在我的Windows服务中。

How can I give those classes DataContract and then pass the list? 我如何给那些类DataContract然后传递列表?

I couldn't find anything about implementing external classes into an wcf service. 我找不到有关在wcf服务中实现外部类的任何信息。

WindowsService 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- -Persons-

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

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

WCFService 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);
}

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. 您将需要执行某种形式的IPC或RPC,就我个人而言,我只是在Windows服务中创建一个暴露了Named-Pipes终结点的终结点,然后就可以从WCF SOA终结点与其进行通信。

WCF named pipe minimal example WCF命名管道的最小示例

And yes, I said embed this service inside your windows service side. 是的,我说过将此服务嵌入Windows服务端。

Your POCO classes can either be datacontracts or serializable. 您的POCO类可以是数据合同,也可以是可序列化的。 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. 首先,您应该将Person类标记为DataContract ,并将属性标记为DataMembers但是您还必须在WCF端拥有一个名称为SAME DataContract的类。 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. DataContract类必须在两个应用程序域中都存在,它们不必是相同的实际类,但是它们必须具有相同的DataContract名称。

However if you are referencing the assembly that contains the DataContract classes from both projects then you should be fine. 但是,如果要从两个项目中引用包含DataContract类的程序集,则应该没问题。

Suggestion: 建议:

Create a single assembly that contains your DataContracts (if possible) and reference this single assembly from both projects. 创建一个包含您的DataContracts的单个程序集(如果可能),并从两个项目中引用该单个程序集。 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. 如果工作量太大,则只需在两者中都创建一个DataContract(类名可以不同),同样重要的是,有一个类在接收方和发送方都具有DataContract的名称。

You have to add the [DataContract] Attribute to person class and [DataMember] to child properties from person class you want to expose: 您必须将[DataContract]属性添加到个人类,并将[DataMember]添加到要公开的个人类的子级属性:

[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;
 }
}

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. 对不起,我的英语:)我不确定我是否理解,但也许是在引用列表中包含的对象,在这种情况下,Person类...尝试用[DataContract]属性装饰他的Person类,并用属性[DataMember]:例如

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 使用数据合约

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM