Listing 1

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

// Mandatory namespace for WCF (you need .NET 3.0 for this)
using System.ServiceModel;

namespace WCFHello
{
    /*
     * Define an interface for exposing to WCF.
     * Use the [ServiceContract] attribute to expose
     * the interface as a WCF service.
     * */

    [ServiceContract]
    interface ISayHello
    {
        /*
         * Use the [OperationContract] attribute for 
         * every method that is exposed to WCF.
         * */

        [OperationContract] string GetGreeting(string name);
        [OperationContract] int    GetRandomInteger();
    }

    /*
     * We implement the above interface as a class that is
     * private to our assembly
     * */

    class SayHello : ISayHello
    {
        // This is a console application.
        // For every call to our WCF service,
        // we'll write a short message for our user

        public string GetGreeting(string name)
        {
            Console.WriteLine("ISayHello.GetGreeting called");
            return "Hello world, and hi there, " + name;
        }

        public int GetRandomInteger()
        {
            Console.WriteLine("ISayHello.GetRandomInteger called");

            Random r = new Random();

            return r.Next();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // This is a self-hosted WCF service; use
            // a ServiceHost instance for this
            ServiceHost sh = new ServiceHost(typeof(SayHello));

            sh.Open();

            // Wait for calls
            Console.WriteLine("The WCF service is up and running, press <ENTER> to stop it");
            Console.ReadLine();

            sh.Close();
        }
    }
}

Listing 2

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WCFSayHelloMetadata">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="WCFSayHelloMeta­data" name="WCFHello.SayHello">
                <endpoint binding="basicHttpBinding" bindingConfiguration=""
                    name="WCFSayHelloEndpoint" contract="WCFHe­llo.ISayHello" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:4500/WCFSayHello" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Listing 3

using System;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace NETSockets
{
    class Program
    {
        static void Main(string[] args)
        {
            // Construct the endpoint (localhost:1225)
            IPHostEntry ipHostInfo = Dns.GetHostEntry("localhost");
            IPAddress   ipAddress  = ipHostInfo.AddressList[0];
            IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, 1225);

            Console.WriteLine("Connecting to the ColdFusion Socket Gateway...\r\n");

            // Create the client socket and connect to our endpoint
            Socket client = new Socket(
                AddressFamily.InterNetwork,
                SocketType.Stream,
                ProtocolType.Tcp);
            client.Connect(remoteEP);

            int numBytes  = 0;
            byte[] buffer = null;



            // Write the first response from our socket.
            // This is the welcome message coming from ColdFusion.
            if (client.Poll(-1, SelectMode.SelectRead))
            {
                // Read & show the message
                numBytes = client.Available;
                buffer = new byte[numBytes];
                client.Receive(buffer, 0, numBytes, SocketFlags.None);
                Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, ?numBytes));
            }

            while (true)
            {
                Console.Write("> ");

                // Read a string from the console
                string input = Console.ReadLine();

                if (input.Equals("exit"))
                {
                    // Writing <exit> tells us to stop
                    break;
                }

                // Send our string to the socket
                client.Send(Encoding.ASCII.GetBytes(input + "\r\n"));

                // Retrieve the answer from the socket
                if (client.Poll(-1, SelectMode.SelectRead))
                {
                    numBytes = client.Available;
                    buffer = new byte[numBytes];
                    client.Receive(buffer, 0, numBytes, SocketFlags.?None);
                    Console.WriteLine(Encoding.ASCII.GetString(buffer, ?0, numBytes));
                }
            }

            // Don't forget to close our connection
            client.Shutdown(SocketShutdown.Both);
            client.Close();
            client = null;

            Console.WriteLine("Disconnected from server. Press <ENTER> to close the app");
            Console.ReadLine();
        }
    }
}


Listing 4

<cfcomponent>

	<cffunction name="onIncomingMessage" output="no">
		<cfargument name="CFEvent" type="struct" required="yes">

		<!--- Echo the received message --->
		<cfset retValue = StructNew()>
		<cfset retValue.DestinationID = CFEvent.OriginatorID>
		<cfset retValue.Message       = "Hello, " & CFEvent.data.MESSAGE>

		<cfreturn retValue>
	</cffunction>

</cfcomponent>


Listing 5

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace COMExposed
{
    /*
     * Define a class and expose it to COM.
     * Assuming that your compiled assembly is called
     * COMExposed.dll, then the ProgId of this
     * class is "COMExposed.MiniCalc"
     * 
     * You can define your own ProgID by decorating
     * the class like this: [ProgId("YourProgram.YourComponent")]
     * Remember that a ProgID is limited to 39 characters
     * 
     * We expose the class to COM as ClassInterfaceType.AutoDual,
     * thus making our class available to both early- and
     * late-binding COM clients.
     * 
     * The other options for ClassInterfaceType would be
     * None (IUnknown-derivative class) and
     * AutoDispatch (to indicate the class support only
     * late binding clients; AutoDispatch is the default value).
     * Note that ColdFusion uses only COM objects that
     * implement the IDIspatch interface.
     * */

    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class MiniCalc
    {
        // Default constructor to make CCW happy
        public MiniCalc() { }

        // The DispId  attribute is optional; it allows you to
        // choose your own DISPID for a method or property.
        // If omitted, you'll end up with automatically generated
        // DISPIDs.

        [DispId(1)] public int Add     (int a, int b) { return a + b; }
        [DispId(2)] public int Subtract(int a, int b) { return a - b; }
        [DispId(3)] public int Multiply(int a, int b) { return a * b; }
        [DispId(4)] public int Divide  (int a, int b) { return a / b; }
    }
}


Listing 6

#include "stdafx.h"

using namespace System;
using namespace System::Text;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;

void ProcessTagRequest(CCFXRequest* pRequest)
{
	try
	{
		// Get the currently executing assembly
		System::Reflection::Assembly^ curAsm =
			System::Reflection::Assembly::GetExecutingAssembly();

		// Construct the output string value as a concatenation
		// between this assembly's name, a random number,
		// and the current date
		StringBuilder^ sb = gcnew StringBuilder();
		sb->AppendFormat("The assembly full name is \"{0}\"<br>", curAsm->FullName);
		sb->AppendFormat("Here's a random number: \"{0}\"<br>", Random().Next());
		sb->AppendFormat("Here's the current date: \"{0}\"<br>", DateTime().Now);

		// Store it in a managed string
		String^ sOutput = sb->ToString();

		// Convert the managed string to a normal string so
		// that we can return it to ColdFusion
		IntPtr ptr             = Marshal::StringToHGlobalAnsi(sOutput);
		const char* lpszOutput = (const char*)ptr.ToPointer();

		// Write this string in the browser; we could as well return
		// this string using pRequest->SetVariable or to add it
		// to a ColdFusion query.
		pRequest->Write(lpszOutput);

		// Don't forget to free the output string
		Marshal::FreeHGlobal(IntPtr((void*)lpszOutput));
	}
	catch(CCFXException* e)
	{
		pRequest->ReThrowException(e);
	}
	catch(...)
	{
		pRequest->ThrowException("General Error", "");
	}
}


Listing 7

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Globalization;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void GetDotNetLanguages()
    {
        // Retrieve a list of all supported cultures in .NET
        CultureInfo[] cultures = 
            CultureInfo.GetCultures(CultureTypes.AllCultures);

        // Create a SqlDataRecord  object that will hold
        // all columns and column types that we'll return from
        // our stored procedure
        SqlDataRecord record = new SqlDataRecord(
            new SqlMetaData("Culture", SqlDbType.NVarChar, 100),
            new SqlMetaData("English Name", SqlDbType.NVarChar, 100),
            new SqlMetaData("Native Name", SqlDbType.NVarChar, 100),
            new SqlMetaData("Display Name", SqlDbType.NVarChar, 100));

        SqlContext.Pipe.SendResultsStart(record);

        foreach (CultureInfo ci in cultures)
        {
            record.SetString(0, ci.Name);
            record.SetString(1, ci.EnglishName);
            record.SetString(2, ci.NativeName);
            record.SetString(3, ci.DisplayName);

            SqlContext.Pipe.SendResultsRow(record);
        }

        SqlContext.Pipe.SendResultsEnd();
    }
};