I am new to WCF and I'm also learning the MVP design pattern. I have a test project with a working WCF service. I am able to test with the WCF test client and it works fine.
我是WCF的新手,我也在学习MVP设计模式。我有一个带有工作WCF服务的测试项目。我能够使用WCF测试客户端进行测试,并且工作正常。
I need help with how to call the WCF service from my Presenter layer and then have the Presenter pass the data back to the View (winforms). I have a Windows Form with two textboxes named txtProductID and txtDescription. I also have a button named btnGetProductData. I would like the following to happen:
我需要有关如何从Presenter层调用WCF服务,然后让Presenter将数据传递回View(winforms)的帮助。我有一个Windows窗体,其中包含两个名为txtProductID和txtDescription的文本框。我还有一个名为btnGetProductData的按钮。我希望发生以下情况:
- I will put a product id in the txtProductID field.
- I will click the btnGetProductData button and the presenter should call the GetProductData method from the WCF service and return the product description to the txtProductDescription field on my form.
我将产品ID放在txtProductID字段中。
我将单击btnGetProductData按钮,演示者应从WCF服务调用GetProductData方法,并将产品描述返回到表单上的txtProductDescription字段。
Here is the pertinent code from the WCF service library:
以下是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;
}
}
}
1 个解决方案
#1
1
I'm not sure where you're having issues, so I'll start at the beginning.
我不确定你在哪里遇到问题,所以我会从头开始。
- You need to add a "Service Reference" to your Presentation Tier Project (this generates a proxy class you can use to call your service)
- You need to create an instance of the generated proxy class
- You need to call a method on the proxy class and store its value
您需要在Presentation Tier Project中添加“Service Reference”(这会生成一个可用于调用服务的代理类)
您需要创建生成的代理类的实例
您需要在代理类上调用方法并存储其值
From Visual Studio, right click your project and choose "Add Service Reference" and then navigate to the endpoint for your service.
在Visual Studio中,右键单击您的项目并选择“添加服务引用”,然后导航到服务的端点。
Example Code:
// 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
1
I'm not sure where you're having issues, so I'll start at the beginning.
我不确定你在哪里遇到问题,所以我会从头开始。
- You need to add a "Service Reference" to your Presentation Tier Project (this generates a proxy class you can use to call your service)
- You need to create an instance of the generated proxy class
- You need to call a method on the proxy class and store its value
您需要在Presentation Tier Project中添加“Service Reference”(这会生成一个可用于调用服务的代理类)
您需要创建生成的代理类的实例
您需要在代理类上调用方法并存储其值
From Visual Studio, right click your project and choose "Add Service Reference" and then navigate to the endpoint for your service.
在Visual Studio中,右键单击您的项目并选择“添加服务引用”,然后导航到服务的端点。
Example Code:
// 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