【发布时间】:2012-04-22 17:01:40
【问题描述】:
假设我有一个简单的合同,我从 MS 示例中获取并进行了一些修改:
[ServiceContract(SessionMode = SessionMode.Allowed)]
public interface IService
{
[WebInvoke(Method = "POST", UriTemplate = "", ResponseFormat = WebMessageFormat.Xml,
RequestFormat = WebMessageFormat.Xml),
Description(
"Adds a customer to customers collection. The response Location header contains a URL to the added item.")]
[OperationContract]
Customer AddCustomer(Customer customer);
[WebInvoke(Method = "DELETE", UriTemplate = "{id}"),
Description(
"Deletes the specified customer from customers collection. Returns NotFound if there is no such customer.")
]
[OperationContract]
void DeleteCustomer(string id);
[WebGet(UriTemplate = "{id}"),
Description(
"Returns the specified customer from customers collection. Returns NotFo`enter code here`und if there is no such customer.")
]
[OperationContract]
Customer GetCustomer(string id);
[WebGet(UriTemplate = ""), Description("Returns all the customers in the customers collection.")]
[OperationContract]
List<Customer> GetCustomers();
[WebInvoke(Method = "PUT", UriTemplate = "{id}"),
Description("Updates the specified customer. Returns NotFound if there is no such customer.")]
[OperationContract]
Customer UpdateCustomer(string id, Customer newCustomer);
}
我需要此合约通过 webhttp REST 和 nettcp 绑定(带有会话)公开。
我的案例(合同)要困难得多,所以我需要了解是否为这两种目的都有一个实现,并以某种方式区分 webhttpbinding 调用或 nettcpbinding 调用,或者为每个端点提供不同的实现。
提前致谢
【问题讨论】:
-
我想我会问自己的问题是,拥有像您描述的服务合同对开发的便利性和未来的维护有何帮助。我的直觉是遵循单一职责原则,并在公共代码库上拥有单独的服务合同和端点。
-
嗯,首先感谢您的回复。我问是否有关于它的常见做法,也许其他人有类似的问题(问题)以及他们对此的以下决定。
-
我同意,因为当涉及到它时,您将混合两种不同的架构风格:面向soap/rpc 和面向http/document。这就是我相信微软将新的基于 HTTP 的 API 从 WCF 团队转移到 ASP.NET 团队的原因之一。
-
为什么需要 nettcp 和 webhttp?为什么需要会话?例如,您只能拥有一个无状态的 REST 端点。在大多数情况下,会话用于授权。使用 REST 技术进行授权,授权标头。
-
您可以使用无状态 REST 服务,但可以模拟您的会话。例如,通常在特殊的“身份验证”HTTP 标头中传递用户身份验证结果令牌。此类标头随每个请求一起传递,并实际模拟与授权用户的会话。所以,当会话只是两个端点的一个原因时,这个问题可以只用其中一个来解决。
标签: .net wcf rest architecture