简体   繁体   English

WCF服务是否公开属性?

[英]Do WCF Services Expose Properties?

In the interface required to implement a WCF service, I declare the main class with the [ServiceContract()] attribute and any exposed method with [OperationContract()] . 在实现WCF服务所需的接口中,我使用[ServiceContract()]属性声明主类,并使用[OperationContract()]声明任何公开的方法。

How can i expose public properties? 我如何揭露公共财产? Thanks 谢谢

Since the get portion of a property is a method, this will technically work but, as mentioned in previous answers/comments, this may not be advisable; 由于属性的获取部分是一种方法,这在技术上可行,但如前面的答案/评论中所述,这可能是不可取的; just posting it here for general knowledge. 只是为了一般知识在这里发布。

Service Contract: 服务合同:

[ServiceContract]
public interface IService1
{
    string Name
    {
        [OperationContract]
        get;
    }
}

Service: 服务:

public class Service1 : IService1
{
    public string Name
    {
        get { return "Steve"; }
    }
}

To access from your client code: 要从您的客户端代码访问:

var client = new Service1Client();
var name = client.get_Name();

You can't. 你不能。 That's not how it works. 这不是它的工作原理。 Methods only. 仅限方法。

您可以公开属性,但是您必须使用[DataContract]属性并且可以将属性声明为属性的[DataMember]属性。

Properties are an object oriented aspect of component interfaces. 属性是组件接口的面向对象方面。 WCF is about services , where you have to think about and design the sequence of interactions between your components. WCF是关于服务的 ,您必须考虑并设计组件之间的交互顺序。

Object orientation does not scale well to distributed scenarios (where your code executes on multiple servers or even in multiple processes) due to the cost of roundtrips, potentially expensive state management, and versioning challenges. 由于往返的成本,可能昂贵的状态管理和版本控制挑战,面向对象无法很好地扩展到分布式方案(您的代码在多个服务器上执行,甚至在多个进程中执行)。 However, OO is still a good way of designing the internals of services, especially if they are complex. 但是,OO仍然是设计服务内部的好方法,特别是如果它们很复杂的话。

PLEASE PLEASE don't expose Properties as a web method.. This will not work in HTTPS. 请不要将属性公开为Web方法..这在HTTPS中不起作用。 I had BIG time identifying and fixing this issue. 我有很大的时间来识别和解决这个问题。 The best way is to write a concrete method to return in WCF. 最好的方法是编写一个在WCF中返回的具体方法。

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

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