?
Solved

WCF - Calling the WCF service from the Presenter

Posted on 2011-05-01
4
Medium Priority
?
669 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:Splinter_2K10
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:DarrenD
ID: 35507080
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
0
 

Author Comment

by:Splinter_2K10
ID: 35507527
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

0
 
LVL 18

Accepted Solution

by:
DarrenD earned 2000 total points
ID: 35708940
Hi,

The service should be added to the Presenter project.

Why do you also have the ProductDAL in the presenter project. This should only be needed for the ProductService to communicate to the database and pass back the Product.

I would create the service proxy through VS. But it depends on the situation.
http://www.eggheadcafe.com/tutorials/aspnet/a1647f10-9aa4-4b0c-bbd9-dfa51a9fab8e/adding-wcf-service-refere.aspx

http://geekswithblogs.net/hinshelm/archive/2007/05/30/Creating-WCF-Service-Host-Programmatically.aspx

Hope this helps,

Darren


0
 

Author Comment

by:Splinter_2K10
ID: 35709083
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
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Loops Section Overview
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question