WCF - calling a WCF service from a Presenter level

I am new to WCF and I am also studying the MVP design pattern. I have a test project with a working WCF service. I can test the WCF test client and it works fine.

I need help on how to invoke a WCF service from my Presenter level and then provide Presenter data back to View (winforms). I have a Windows form with two text fields named txtProductID and txtDescription. I also have a button called btnGetProductData. I would like the following:

  • I will put the product identifier in the txtProductID field.
  • I press the btnGetProductData button, and the host should call the GetProductData method from the WCF service and return the product description to the txtProductDescription field in my form.

Here is the corresponding 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;                     
        }        
    }
}
+3
1

, , .

  • ( -, ).
  • -
  • proxy

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

All Articles