CS-Script 3.27.0

WCF


WCF (Windows Communication Foundation) is a generic framework (first introduced in .NET3.0) for solving a wide range of communication problems. Any C# script utilizing the WCF classes can be executed by CS-Script as any other scripts.

WCF has signifficant overlaps with .NET Remoting, what is no surprise as WCF programming model unifies Web Services, .NET Remoting, Distributed Transactions, and Message Queues into a single Service-oriented programming model.

From this prospective C# scripts using WCF functionality are organised very similar to the .NET Remoting scripts.

The samples below are from the CS-Script samples library (Script Library).

Servive (Samples\WCF\service.cs)
using System;
using System.ServiceModel;

namespace HelloService
{
    class program
    {
        static void Main(string[] args)
        {
            Uri baseUri = new Uri("http://localhost/hello");
            ServiceHost hService = new ServiceHost(typeof(HelloService), baseUri);
            hService.AddServiceEndpoint(typeof(HelloService), new BasicHttpBinding(), baseUri);
            hService.Open();

            Console.WriteLine("Service started...");
            Console.ReadKey();

            hService.Close();
        }
    }

    [ServiceContract]
    class HelloService
    {
        [OperationContract]
        string SayHello()
        {
            return ("Hello!");
        }
    }
}

Service config file
(Samples\WCF\service.cs.config)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="HelloService.HelloService" 

Client (Samples\WCF\client.cs)
//css_prescript wsdl(http://localhost/hello?wsdl, HelloService, /new);
//css_include HelloService;
using System;
using System.Xml;

namespace HelloClient
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloService p = new HelloService();
            Console.WriteLine(p.SayHello());
        }
    }
}

The major difference between WCF and Remoting scripts is the usage of the script.config file. It is an equivalent of app.config file but for the script applications. In order to process script config files the script engine has to be executed with /sconfig parameter (script engine alway uses this paremeter by default).

The code is trivial and of course it does not demonstrates all aspects of WCF programming model. It implements WCF service HelloService fclient application accesses the service and excercises it's method SayHello().

Interestingly client.cs uses MS wsdl.exe utility to is capable of producing C# file (proxy) implementing the type exposed by the WCF service (HelloClient). CS-Script can prepare such file as a script pre-execution step, so you do not need to do mit manually. This is exactly what the code below does:

//css_prescript wsdl(http://localhost/hello?wsdl, HelloService, /new); 

And after the file with proxy class (HelloService.cs) is prepared it is referenced by the client script as any other C# file:

//css_include HelloService;

See Also

Script Library.NET Remoting scripts Pre- and Post-execution scripts