2011-05-02 102 views
3

我是WCF的新手,我也在学习MVP设计模式。我有一个运行WCF服务的测试项目。我能够用WCF测试客户端进行测试,并且工作正常。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;      
     }   
    } 
} 
+2

你的服务看起来不错,但您不必在客户端上有什么问题,特别是? EG:您是否在创建代理时遇到问题,是否不会返回您期望的内容? – 2011-05-02 17:13:44

回答

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 
+0

是的,您仍需要添加服务引用才能使用我上面定义的代理类,并且您正尝试在刚刚发布的代码中使用该代理类。 – Nate 2011-05-03 20:45:37

+0

感谢您的回复和示例。我有一个关于你的回复的问题。由于我使用的是模型视图展示器设计模式,而我的Prensenter类需要调用WCF服务,我还会通过Visual Studio添加服务引用吗? - 或者 - 我应该按照我在下面找到的示例代码中所示的方式进行操作吗?注意:ProductService是WCF服务类的名称。 ProductService产品服务; productService – Robert 2011-05-03 20:45:13

+1

您仍然会通过Visual Studio添加服务引用,因为它与您正在使用的编程模型没有任何关系。 WCF初学者在WinForms客户端中使用它的最简单方法之一是在代码隐藏窗体中创建和使用代理实例。 – 2011-05-03 15:47:21