【问题标题】:WCF - Calling the WCF service from the Presenter LayerWCF - 从 Presenter 层调用 WCF 服务
【发布时间】:2011-05-02 16:29:24
【问题描述】:

我是 WCF 的新手,我也在学习 MVP 设计模式。我有一个带有工作 WCF 服务的测试项目。我可以使用 WCF 测试客户端进行测试,并且工作正常。

我需要有关如何从我的 Presenter 层调用 WCF 服务然后让 Presenter 将数据传递回视图 (winforms) 的帮助。我有一个带有两个名为 txtProductID 和 txtDescription 的文本框的 Windows 窗体。我还有一个名为 btnGetProductData 的按钮。我希望发生以下情况:

  1. 我将在 txtProductID 字段中添加一个产品 ID。
  2. 我将单击 btnGetProductData 按钮,演示者应从 WCF 服务调用 GetProductData 方法并将产品描述返回到我表单上的 txtProductDescription 字段。

这是来自 WCF 服务库的相关代码:

IProductService.cs
------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace MyWCFServices.ProductService
{
   [ServiceContract]
    public interface IProductService
    {
        [OperationContract]
        Product GetProductData(string ProductId);     
    }

    [DataContract]
    public class Product
    {
        [DataMember]
        public string ProductID { get; set; }
        [DataMember]
        public string ProductDescription { get; set; }

    }
}

ProductService.cs
--------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MyWCFServices.ProductEntities;
using MyWCFServices.ProductBusinessLogic;

namespace MyWCFServices.ProductService
{

    public class ProductService : IProductService
    {
        ProductLogic productLogic = new ProductLogic();

        public Product GetProductData(string ProductId)
        {

            ProductEntity productEntity = productLogic.
                GetProductData(ProductId);
            Product product = new Product();

            TranslateProductEntityToProductContractData(productEntity, 
                product);
            return product;

        }

        private Product TranslateProductEntityToProductContractData(
            ProductEntity productEntity, Product product)
        {

            product.ProductID = productEntity.ProductID;
            product.ProductDescription = productEntity.ProductDescription;

            return product;                     
        }        
    }
}

【问题讨论】:

  • 您的服务看起来不错,但您的客户端有什么问题,具体来说? EG:您在创建代理时遇到问题,它不会返回您期望的结果吗?

标签: .net wcf mvp


【解决方案1】:

我不确定您在哪里遇到问题,所以我将从头开始。

  1. 您需要将“服务引用”添加到您的表示层项目(这会生成一个代理类,您可以使用它来调用您的服务)
  2. 您需要创建生成的代理类的实例
  3. 您需要调用代理类的方法并存储其值

在 Visual Studio 中,右键单击您的项目并选择“添加服务引用”,然后导航到您的服务的端点。

示例代码:

// Presentation Tier (button event handler)
var proxy = new ServiceReference1.ProductServiceClient();
var prod = proxy.GetProductData("yourProductID");
txtDescription.Text = prod.Description;
txtProductID.Text = prod.ProductID; // same as passed parameter

【讨论】:

  • 您仍然需要通过 Visual Studio 添加服务引用,因为它与您使用的编程模型没有任何关系。 WCF 初学者在 WinForms 客户端中使用它的最简单方法之一是在表单的代码隐藏中创建和使用代理实例。
  • 感谢您的回复和示例。关于你的回复,我有一个问题。由于我使用的是 Model View Presenter 设计模式并且我的 Prensenter 类需要调用 WCF 服务,我是否仍要通过 Visual Studio 添加服务引用? -或者- 我应该按照我在下面找到的示例代码中所示的方式进行操作吗?注意:ProductService 是 WCF 服务类的名称。产品服务产品服务;产品服务
  • 是的,您仍然需要添加服务引用才能使用我在上面定义的代理类,并且您正在尝试在刚刚发布的代码中使用它。
  • 由于我在合并和编辑方面的笨拙尝试,上面的前两个 cmet 出现故障。似乎没有办法让它们以正确的顺序出现。
猜你喜欢
  • 1970-01-01
  • 2011-01-31
  • 2010-12-11
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2012-10-20
  • 2010-09-12
  • 1970-01-01
相关资源
最近更新 更多