Question

Running into a block trying to follow the logic of an example program. The example is used to demonstrate creating a contract, create a rest web service and then consume the rest service.

What throws me is I have the interface defined in the contract

namespace ProductDetailsContracts
{
    [ServiceContract]
    public interface IProductDetails
    {
        [OperationContract]
        [WebGet(UriTemplate = "products/{productID}")]
        Product GetProduct(string productID);
    }
}

then used in the web service

using ProductDetailsContracts;
public class ProductDetails : IProductDetails
{
    public Product GetProduct(string productID)
    {
        //do something
    }
}

The code is then consumed in the client

using ProductDetailsContracts;
namespace ProductClient
{
    class ProductClientProxy : ClientBase<IProductDetails>, IProductDetails
    {
        public Product GetProduct(string productID)
        {
            return this.Channel.GetProduct(productID);
        }
    }
}

I feel ClientBase<IProductDetails> is the key but I don't see how it is associated with the web service ProductDetails. My real goal to understanding this will be to run a client application that can add and update records in a SQL Server.

Was it helpful?

Solution

The interface you defined is the contract you pass to your client which tells them what services you provide, in your case you provide a GetProduct method.

Then, you create a concrete implementation of that contract, so when client code calls on your interface, they will end up invoking that concrete implementation, which will probably access some external resource (a database or a file) containing the product.

Your ProductClientProxy, which inherits from ClientBase<IProductDetails> is responsible for settings up the channel that will allow the client to make calls to your service. ClientBase is part of the WCF infrastructure, and is the class that actually reads the settings from your app.config and is responsible for settings up communication.

I suggest you read more about ClientBase and even look at the source code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top