1

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

提前致谢

4

1 回答 1

2

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

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

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

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

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

于 2012-05-04T09:14:20.707 回答