简体   繁体   English

C#中的Web服务和属性

[英]Web Service and Properties in C#

I'm quite new to Web Services. 我对Web Services很陌生。 I've created a Web Serivce with a class like this: 我用这样的类创建了一个Web服务:

public class ClassA
{
     private ClassB _varA;

     public ClassB VarA
     {
         get {
             if(_varA == null)
                _varA = new ClassB();
             return _varA;
         }
         set { _varA = value; }
     }
}

When I try to access the property from a website, it gives me null. 当我尝试从网站访问该属性时,它给我null。

using WebServiceA;

ClassA obj = new ClassA();
obj.VarA // gives me null ?

Am I missing something here? 我在这里错过了什么吗? Please help. 请帮忙。 Thanks. 谢谢。

When you send an object over a web-service, the actual functions don't come with it, only the property values (so the get in your example doesn't actually happen client side). 当您通过Web服务发送对象时,实际的功能不会附带它,只有属性值(因此示例中的get实际上不会发生在客户端)。 Instead, it creates a 'mock' type version of the same object. 相反,它会创建同一对象的“模拟”类型版本。

I figured I'd clarify in this edit: 我想我会在这个编辑中澄清一下:

When you connect to a web service that returns an object, it actually just returns an XML representation of the object. 当您连接到返回对象的Web服务时,它实际上只返回该对象的XML表示形式。 This XML representation only contains data that gets serialized (method depends on the settings, in plain-jane web services, it is usually just an XML Serializer), thus does not contain any functions or property definitions. 此XML表示仅包含序列化的数据(方法取决于设置,在简单的Web服务中,它通常只是XML序列化程序),因此不包含任何函数或属性定义。

So, the class in this example is: 所以,这个例子中的类是:

public class ClassA
{
     public ClassB VarA
     {
         get;
         set;
     }
}

Also: Fredrik Mörk said it correclty, its called a 'Proxy' object, not a mock object, I couldn't think of the word. 另外:FredrikMörk说它是correclty,它叫做'Proxy'对象,而不是模拟对象,我想不到这个词。

B类是否在Web服务的项目中实现?

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

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