WCF - 从Presenter层调用WCF服务

3

我刚接触WCF并正在学习MVP设计模式。我有一个测试项目,其中包含一个正常运行的WCF服务。我可以使用WCF测试客户端进行测试,并且一切正常。

我需要帮助,了解如何从我的Presenter层调用WCF服务,然后让Presenter将数据传回View(winforms)。我有一个Windows窗体,其中包含两个名为txtProductID和txtDescription的文本框。我还有一个名为btnGetProductData的按钮。我希望实现以下功能:

  1. 我将在txtProductID字段中输入产品ID。
  2. 我将单击btnGetProductData按钮,Presenter应该从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;                     
        }        
    }
}

2
你的服务看起来很好,但是客户端出了什么问题,具体是什么?例如:你在创建代理时遇到了问题吗?它是否没有返回你期望的结果? - Chris Wenham
1个回答

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

1
你仍然需要通过Visual Studio添加服务引用,因为它与你正在使用的编程模型没有任何关系。在WCF中,初学者在WinForms客户端中使用最简单的方法之一是在代码后台中创建和使用代理实例。 - Chris Wenham
谢谢您的回复和示例。我有一个关于您回复的问题。由于我正在使用模型视图控制器设计模式,我的Prensenter类需要调用WCF服务,那么我是否仍然需要通过Visual Studio添加服务引用?还是应该像下面找到的示例代码一样执行?注意:ProductService是WCF服务类的名称。ProductService productService; - Robert
是的,你仍然需要添加服务引用才能使用我上面定义的代理类,并且你正在尝试在刚刚发布的代码中使用它。 - Nate
以上的前两条评论由于我的拙劣合并和编辑尝试而顺序颠倒。似乎没有办法让它们以正确的顺序出现。 - Bill the Lizard

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接