Share via


Implementing Adapter Inbound Message Exchange Handler - Part 3 (Consumption in .NET Console Application using WCF Channel Model)

You saw in this post, how to test the inbound functionality of the WCF-based adapter using the WCF Service Model. 

If you are interested in testing the Adapter Inbound Handler using the WCF Channel Model, the following sample shows you how to do so. This bypasses the de-serialization of the WCF messages that happens in the WCF Service Model.

1) Create a new Visual Studio C# Console Application Project

 

2) Modify Program.cs

We will not use the app.config in this sample. In this case, the channel listener is invoked using the raw WCF Channel Model programming model. Note how the binding parameter collection is used in this case to pass the action to the inbound handler StartListener() method. The WCF channel model lets you work with the XML directly without having to go through the Dispatcher and run into serialization/de-serialization issues.

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;

// WCF Namespace

using System.ServiceModel;

using System.ServiceModel.Channels;

// Adapter SDK Namespace

using Microsoft.ServiceModel.Channels;

// HelloWorldAdapter Namespace

using Microsoft.WCF.Samples.Adapters;

namespace TestHelloWorldAdapter_InboundScenario

{

    class Program

    {

        static void Main(string[] args)

        {

            IChannelListener<IInputChannel> listener = null;

            IInputChannel channel = null;

            try

            {

                string baseUri = "hello://";

                // Create the adapter binding

                HelloWorldAdapterBinding binding = new HelloWorldAdapterBinding();

                binding.Inbound_FileFilter = "*.txt";

                binding.Inbound_FileWatcherPath = @"c:\temp\helloworld";

                // Create a binding parameter collection with a list of SOAP actions to listen on

   AdapterInboundActionCollection actions = new AdapterInboundActionCollection(new Uri(baseUri));

                actions.Add("Hello/OnReceiveGreeting");

                BindingParameterCollection bpcol = new BindingParameterCollection();

                bpcol.Add(actions);

                // Use the Binding and Binding Parameter Collection to build the channel listener

                listener = binding.BuildChannelListener<IInputChannel>(new Uri(baseUri), bpcol);

                // Listen for messages

                listener.Open();

                // Wait for and accept incoming connections

                bool havechannel = listener.WaitForChannel(TimeSpan.FromMinutes(1));

                channel = listener.AcceptChannel();

            // Open the accepted channel

                channel.Open();

                // Wait for and receive a message from the channel

                Console.WriteLine("Copy a file with extension " + binding.Inbound_FileFilter + " into " + binding.Inbound_FileWatcherPath);

                Console.WriteLine("Wait for and receive a message from the channel...");

                Console.WriteLine("\nPress CTRL+C to terminate.\n");

                Message message = null;

                while (true)

                {

                    try

                    {

                        bool status = channel.TryReceive(TimeSpan.FromMinutes(2), out message);

                        // Read the message

                        if (message != null)

                        {

                            // Display the request message content

                            XmlReader requestReader = message.GetReaderAtBodyContents();

                            Console.WriteLine(requestReader.ReadOuterXml());

                            Console.WriteLine();

                            Console.WriteLine(requestReader.ReadString());

                            Console.WriteLine();

                            // Do not forget to close the message

                            message.Close();

                        }

                    }

                    catch(System.Xml.XmlException xmlEx)

                    {

                        Console.WriteLine(xmlEx);

                        break;

                    }

                }

            }

            catch (CommunicationException ex)

            {

                Console.WriteLine(ex.Message);

            }

            finally

            {

                // Do not forget to close the channel

                channel.Close();

                // Do not forget to close the listener

                listener.Close();

            }

        }

    }

}

 

1) Build and Run

Once the build is successful, you should be able to see a Console Window popup.

2) Test

Copy a file with extension *.txt into c:\temp\helloworld

Wait for and receive a message from the channel...

Press CTRL+C to terminate.

<OnReceiveGreeting xmlns="hello://Microsoft.WCF.Samples.Adapters"><name>c:\temp\helloworld\lang.txt</name><data>Salut, Hallo, Hey, Moin Moin, Hola, Bonjour, Bon Soir, Bonne Nuit, Ciao, Hej, Hoi, Namaste!</data></OnReceiveGreeting>

<OnReceiveGreeting xmlns="hello://Microsoft.WCF.Samples.Adapters"><name>c:\temp\helloworld\hello.txt</name><data>Hello Hello ..</data></OnReceiveGreeting>

 

Copy a *.txt file into the file watcher folder – make this folder an adapter binding property, so it can be set via app.config.

The service is listening for the messages coming from the adapter and the messages are created when the event is triggered by the File System Watcher. I copied two text files into the watcher folder and the above output was created, based on the service implementation.

Next time: Testing adapter inbound handler implementation in BizTalk Server 2006 R2 using BizTalk WCF Adapter and Consume Adapter Service BizTalk Project Add-In

Comments

  • Anonymous
    May 20, 2007
    Using LINQ to SQL (Part 1) [Via: ScottGu ] Easier Winform UI Thread Safe Methods with DynamicProxy2...