如何将自定义类传递给 WCF 服务? [英] How do I pass a custom class to a WCF service?

查看:36
本文介绍了如何将自定义类传递给 WCF 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将几个参数传递给 WCF 服务,因此我创建了一个具有适当字段的类,并认为我会将该类的一个实例传递给该服务.但该服务认为该类与其拥有的类不同.

I have to pass several arguments to a WCF service so I created a class with the appropriate fields and thought I'd pass an instance of that class to the service. But the service sees that class as different than the one it has.

所以我想在服务中创建那个类,但我发现在调用程序中

So I thought of creating that class in the service, but I found that in the calling program

Class1 c = new Class1 ();//of the class in the service
FieldInfo[] f = c.GetType().GetFields();

什么都不返回.

所以我尝试制作一个接口,但服务仍然需要服务的它的类.

So I tried making an interface, but the service still wants its class from the service.

那么如何传递自定义类实例,并且仍然能够像任何其他类一样在调用者中使用该类?

So how can I pass a custom class instance and still be able to use that class in the caller like any other class?

这就是我正在做的事情:我在 VWD (-Visual Studio for web) 中创建了一个网站(只是简单的模板).并添加了一个 WCF 服务和一个带有

This is what I'm doing: I created a website (just the simple template) in VWD (-Visual Studio for web). And added a WCF service and a file with

[DataContract]
[Serializable()]
public class Class1
{
    [DataMember]
    public string s = "";
}

好的.出于某种原因,字段被转换为属性.

OK. For some reason the fields are converted to properties.

谢谢大家!

推荐答案

以下是 Datacontract 的示例:

Here is an example of a Datacontract:

    [DataContract]
public class Person
{
    [DataMember]
    public int id { get; set; }

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

然后,您可以从 WCF 客户端启动它,或将其作为方法中的参数传递或让它从方法(函数)返回.

Then you can initiate it from your WCF client or pass it as an argument in a method or let it return from a method(function).

这篇关于如何将自定义类传递给 WCF 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆