WCF (Windows Communication Foundation) has been introduced with the .NET Framework 3.0 and is the successor of all communication APIs supported by the .NET Framework 2.0. Now, all communication APIs like SOAP-based Web Services have been unified under WCF.

Text Control

Using TX Text Control within a WCF Service Library is quite straightforward. In Visual Studio 2008, you need to create a new WCF Service Library:

Then add a reference to TX Text Control .NET for Windows Forms 15.0, add a new text file and name it licenses.licx. Add the following string to this license file:

TXTextControl.ServerTextControl, TXTextControl, Version=15.0.700.500, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638

To show the functionality, this sample shows a very easy process. The service should simply make the text of an incoming RTF stream bold. Open the IService1.cs file and add the following code to the interface IService1:

[OperationContract]
string MakeItBold(string RTF);

Open the Service1.cs file and add the following method:

public string MakeItBold(string RTF)
{
    string data = String.Empty;

    // create a new ServerTextControl instance

    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();
        // load the RTF document
        tx.Load(RTF, TXTextControl.StringStreamType.RichTextFormat);

        // make it bold
        tx.SelectAll();
        tx.Selection.Bold = true;

        // save the content
        tx.Save(out data, TXTextControl.StringStreamType.RichTextFormat);
    }

    return data;
}

Now, add a new Windows Forms project to your solution. Select the new project in the Solution Explorer and choose Add Service Reference... from the Project menu.

Text Control

In the opened dialog box, click on Discover to browse for services in the same solution. Now, select IService1 in the Services tree view, change the Namespace to tx and confirm with OK.

Create two Windows Forms TextControl instances on the form by dragging them from the toolbox to the form. Add an additional button to the form and copy this code to the button's Click event:

tx.Service1Client txClient = new WindowsFormsApplication1.tx.Service1Client();

string originalData = String.Empty;
string changedData = String.Empty;
textControl1.Save(out originalData, TXTextControl.StringStreamType.RichTextFormat);
changedData = txClient.MakeItBold(originalData);
textControl2.Load(changedData, TXTextControl.StringStreamType.RichTextFormat);

Now, set the Windows Forms project as the StartUp project and press F5 to compile and start the application.

On clicking the button, the content of the first TextControl will be passed to the service as an RTF string. ServerTextControl loads the RTF in the service in order to change the formatting to bold. After that, the new RTF string is passed back as the return value.

This is just a basic sample, but it gives a good overview of the possibilities using WCF. You could build a service to convert documents, to create Adobe PDF files or to create documents on-the-fly.

Download the Visual Studio 2008 sample here:

tx_sample_wcf.zip

Feel free to post your comments.