WCF。休息。建筑。需要建议

时间:2012-04-10 11:47:27

标签: .net wcf rest architecture

假设我有这个简单的合同,我从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调用,或者为每个端点提供不同的实现。

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为您不需要混合使用不同的架构样式 - SOAP / WS- *(nettcpbinding)和REST。正确实施后,他们会有所不同。当您将这些样式混合在一个服务中时,服务方法的逻辑肯定会包含多个if-than或switch情况,以确定您正在使用哪种模式,这也可能是使用HTTP或WCF上下文时的问题。

我建议你创建单独的端点(契约) - 一个用于REST,一个用于WS - * / SOAP并遵循它们的架构风格。

要在它们之间共享逻辑,将业务逻辑封装在单独的层(类)中,然后您可以简单地重用它,而无需混合使用REST或WS- *特定逻辑和业务逻辑。

在处理简单的CRUD操作时,请考虑使用WCF Data Serivces,它可以根据数据库为您生成REST / OData端点。

UPD评论: 您可以使用无状态REST服务,但“模拟”您的会话。例如,在每个请求中的特殊“身份验证”HTTP标头中传递用户身份验证结果令牌以识别和区分用户是很常见的。这样的头部与每个请求一起传递,并且实际上模拟与授权用户的会话。因此,当会话只是两个端点的一个原因时,只能用其中一个端点来解决这个问题。

相关问题