Link to home
Start Free TrialLog in
Avatar of Splinter_2K10
Splinter_2K10

asked on

WCF - Calling the WCF service from the Presenter

Greetings,

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:

1.  I will put a product id in the txtProductID field.
2.  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.

Thank you in advance for you help.

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;                     
        }        
    }
}

Open in new window

Avatar of Darren
Darren
Flag of Ireland image

Hi,

So, you simply want code to call the WCF service and get back the data?

You ca add a reference to your service by right clicking on References in the Project Explorer and selecting Add Service Reference. Then point it to where your service is located and then on the client you create an instance of the WCF client and use that to talk to the WCF service.

See the section Using WCF Client.
http://msdn.microsoft.com/en-us/library/ms734691.aspx#Y912


ProductServiceClient productClient = new ProductServiceClient ();

Product myprod = new Product()

myprod  = productClient.GetProductData("123");

You should have no issues and then you simply set the

txtProductDescription.Text = myprod.ProductDescription

Hope this helps,

Darren
Avatar of Splinter_2K10
Splinter_2K10

ASKER

Hi Darren,

Thanks for the assistance again.  So in the Model View Presenter design pattern (Passive View), I belive my Presenter is supposed to be talking to the Model (WCF in this case) and returning the data back to the client.

I have a VS solution with multiple projects.   I have a project for the WCF Service Library and separate projects for the View, Business Logic and the Data Access Layer.

So, should the service reference be added to my presenter project?     Also, in the time since I posted this I found an example which I will post below, where it believe I am creating a proxy manually.   Is this correct?   If so, should I create the service reference through VS instead?    The code below seems to work, although I have a small connection string issue that I put in another post.

Here is my presenter layer:


ProductService.cs
---------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyWCFServices.ProductService;
using MyWCFServices.ProductDAL;

namespace ProductPresenters
{
   
    public class ProductListPresenter
    {

        private IProductListView productView;

        public ProductListPresenter(IProdcutListView productView)
        {
            this.productView = productView;          
        }
        

        public void GetProductList()
        {

            ProductService productService;

            productService = new ProductService();

            Product product = null;

            string productID;

            productID = productView.ProductId;
            
            product = productService.GetProductData(productID);
            
            productView.ProductDescription = product.ProductDescription;           
                       
        }

    }
}

IProductListView.cs
-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductPresenters
{
    public interface IProductListView
    {
        string ProductId { get; }
        string ProductDescription { set; get; }
       
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Darren
Darren
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Darren,

The reference to the DAL was leftover code from my previous fumbling!   You are correct that it has no reason to be in the presenter layer.  I also noticed that I had my file names wrong in my code window above.   The first file is actually named ProductList.cs.

Thanks for the pointers in the right direction.

Keith