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.
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:
- 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.
Here is the pertinent code from the WCF service library:
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;
}
}
}
I’m not sure where you’re having issues, so I’ll start at the beginning.
From Visual Studio, right click your project and choose “Add Service Reference” and then navigate to the endpoint for your service.
Example Code: