C# WCF 服务是否公开属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/824268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:04:27  来源:igfitidea点击:

Do WCF Services Expose Properties?

c#wcf

提问by pistacchio

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

我如何公开公共财产?谢谢

采纳答案by John Saunders

You can't. That's not how it works. Methods only.

你不能。这不是它的工作原理。方法而已。

回答by Pontus Gagge

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.

由于往返成本、潜在的昂贵状态管理和版本控制挑战,面向对象不能很好地扩展到分布式场景(您的代码在多个服务器上甚至在多个进程中执行)。但是,面向对象仍然是设计服务内部结构的好方法,尤其是在它们很复杂的情况下。

回答by Steve Dignan

Since the getportion 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.

由于属性的get部分是一种方法,因此这在技术上是可行的,但正如之前的答案/评论中所述,这可能是不可取的;只是将其张贴在这里以获取一般知识。

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();

回答by Rishi_raj

You can expose properties but u will have to use [DataContract]attribute and can declare property as [DataMember]attribute for properties.

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

回答by KKR

PLEASE PLEASE don't expose Properties as a web method.. This will not work in HTTPS. I had BIG time identifying and fixing this issue. The best way is to write a concrete method to return in WCF.

请不要将属性公开为 Web 方法。这在 HTTPS 中不起作用。我花了很多时间来识别和解决这个问题。最好的方法是在WCF中写一个具体的方法返回。