BlueYonderAirLines (cont..) how to consume a service

Topics: General Discussion Forum
May 13, 2008 at 8:46 PM
Edited May 16, 2008 at 9:27 PM
New to WCF/WSSF?  Visit http://code.msdn.microsoft.com/ServiceFactory 


  • Topics Covered
    • Hands-on Lab continued - how to consume a WCF service (with and without Config file)
    • Assumptions that had me googling and burning hours...
    • Configure IIS for WCF

BYA.Mfg.SCM.Svc.WCF :: Tests :: BYA.Mfg.SCM.Svc.WCF.Client :: MainForm.cs 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using BYA.Mfg.SCM.Svc.WCF.Client.MaterialMgmtProxy;

namespace BYA.Mfg.SCM.Svc.WCF.Client
{
    public partial class MainForm : Form
    {
        PartsMgmtServiceContractClient client;

            public MainForm()
        {
            InitializeComponent();
            client = new PartsMgmtServiceContractClient("DefaultEndpoint");
        }

        // Utilizes App.Config
        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            // Configure the request utilizing input - valid input
            // are "one" and "two" - all others will return 0 values
            DemandRequest request = new DemandRequest();
            request.AircraftPart = new AircraftPart();
            request.AircraftPart.Part = SearchText.Text;

            ResultsGrid grid = new ResultsGrid(client.GetRequirementDemand(request));
            grid.Show();
        }

-- OR --

        // Utilizes Channel
        private void ExecuteButton_Click(object sender, EventArgs e)
        {
            // Configure the request utilizing input - valid input
            // are "one" and "two" - all others will return 0 values
            DemandRequest request = new DemandRequest();
            request.AircraftPart = new AircraftPart();
            request.AircraftPart.Part = SearchText.Text;

            BasicHttpBinding binding = new BasicHttpBinding();
            EndpointAddress endPoint = new EndpointAddress("http://localhost:2035/BYA.Mfg.SCM.Svc.WCF.Host/MaterialMgmt.svc");

            ChannelFactory<PartsMgmtServiceContract> channelFactory =
                new ChannelFactory<PartsMgmtServiceContract>(binding, endPoint);

            PartsMgmtServiceContract service = channelFactory.CreateChannel();

            DemandResponse response = service.GetRequirementDemand(new DemandRequest1(request));

            ResultsGrid grid = new ResultsGrid(response.PartLevel);
            grid.Show();

        }
    }
}



BYA.Mfg.SCM.Svc.WCF :: Source :: Service Interface :: BYA.Mfg.SCM.Svc.WCF.ServiceImplementation :: PartsMgmtService.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using BYA.Mfg.SCM.Svc.WCF.DataContracts;
using BYA.Mfg.SCM.Svc.WCF.BusinessEntities;
using BYA.Mfg.SCM.Svc.WCF.MessageContracts;

namespace BYA.Mfg.SCM.Svc.WCF.ServiceImplementation
{
    public partial class PartsMgmtService
    {
        public override DemandResponse GetRequirementDemand(DemandRequest request)
        {
            // The following object represents the
            // business entity returned from the business logic
            ItemLevel itemLevel;
            itemLevel = new ItemLevel();

            switch (request.AircraftPart.Part)
            {
                case "one":
                    itemLevel.Expecting = 10;
                    itemLevel.Usable = 100;
                    break;

                case "two":
                    itemLevel.Expecting = 20;
                    itemLevel.Usable = 200;
                    break;

                default:
                    itemLevel.Expecting = 0;
                    itemLevel.Usable = 0;
                    break;
            }

            PartLevel partLevel;
            partLevel = TranslateBetweenItemLevelAndPartLevel
                .TranslateItemLevelToPartLevel(itemLevel);

            DemandResponse response = new DemandResponse();
            response.PartLevel = partLevel;
            return response;
        }
    }
}


Being new to the WSSF I've made some bad assumptions....  As such I plan to extend this message thread as I stumble  across them (no pun intended) along with their resolutions in hopes of preventing the next newbie (with no knowledge of WCF and WSSF) from making the same assumptions and losing valuable hours.  Comments will be within the context of the BlueYonderAirLines lab.

Assumption:  Exercise 3 - Wiring up the data contracts uses a Message for Request and a XSD Message for Response, not being familiar with either of these I assumed that request and response required these types.   While researching the following assumptions I discovered that a message is used to reference a service.datacontract item where a XSD Message allows you to reference types located in schemas or xsd files (ref: http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=21890)

Assumption: I could create an XSD file using the VS DataSet design tool and use it as an XSD Message.  Querying "XSD Message" in the forum did not yield any understandable results (I don't yet share a common core of experience with the community) however this message thread http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=3787 provided a Microsoft link that suggest a "subset of the XML Schemea (XSD) used by the DataContractSerializer" is used.   (MS: http://msdn.microsoft.com/en-us/library/ms733112.aspx).   My assumption is that the PartLevel.xsd file referenced in step 4 of Exercise 3 was manually created.

Assumption: Visual Studio will Launch Development Server.  I missed one step in Exercise 7 which sent me configuring my firewall and chasing google trails - because my connection was actively refused.  Step 18  notes "Right-click this project again and select View In Browser to expose the service so the metadata will be available to generate the proxy from"

Issue: I started getting an invalid port message after experimenting (making changes and recompiling).   The WCF :: Tests :: BYA.Mfg.SCM.Svc.WCF.Client's App.Config file appends <basicHttpBinding> statements - in my case I learned real quick about <endpoint> as I had multiple entries (with different ports).   I cleaned out all of the entries but the "DefaultEndpoint" and all was happy again.

Issue: Couldn't compile source code after removing FXCop references (over 200 errors).  Used binaries for prototype but usually use source for development; fix was to remove VS2005 SDK (ref: http://www.codeplex.com/servicefactory/Thread/View.aspx?ThreadId=25149).

Assumption:  (On the documention's part)  That we would know how to consume a WCF / WSSF - the following represents the only two classes I had to update to provide a service and consume it.