Windows Communication Foundation (WCF)

Windows Communication Foundation is a feature of the .NET Framework that allows you to build applications that communicate with each other across the network. Essentially, WCF will allow us to call methods on an object that is exposed on another computer as though they were both on the local machine. With this project, there will be one application running on one machine (the client; in this case, the ASP.NET application) that will execute a method on another machine, the host, which will be running Outlook and the application we are writing.

In the WHSMailHost project, add a reference to the System.ServiceModel assembly. Then, add a class to the project named WHSMailService. Open the class and add the code shown in Example 7-5 or Example 7-6 beneath the generated WHSMailService class implementation.

Example 7-5.  C# code for MyServiceHost class

internal class MyServiceHost
{
    internal static ServiceHost myServiceHost = null;

    internal static void StartService()
    {
        // Instantiate new ServiceHost
        myServiceHost = new ServiceHost(typeof(WHSMailService));
        myServiceHost.Open();
    }

    internal static void StopService()
    {
        // Call StopService from your shutdown logic (i.e. dispose method)
        if (myServiceHost.State != CommunicationState.Closed)
            myServiceHost.Close();
    }
}

Example 7-6.  Visual Basic code for MyServiceHost class

Friend Class MyServiceHost Friend Shared ...

Get Coding4Fun now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.