实例化

实例化示例演示实例行为设置,该设置控制如何创建服务类的实例以响应客户端请求。 此示例基于名为Getting Started的指南,并实现ICalculator服务协定。 此示例定义一个新的协定, ICalculatorInstance该协定继承自 ICalculator. 协议规定的ICalculatorInstance提供三个附加操作以检查服务实例状态。 通过更改实例设置,可以通过运行客户端来观察行为更改。

在此示例中,客户端是一个控制台应用程序 (.exe),服务是由 Internet 信息服务 (IIS) 承载的。

注释

本示例的设置过程和生成说明位于本主题末尾。

可以使用下列实例化模式:

  • PerCall:为每个客户端请求创建新的服务实例。

  • PerSession:为每个新的客户端会话创建一个新实例,并在整个会话期间进行维护(需要能够支持会话的绑定)。

  • Single:服务类的单个实例在应用程序的生存期内处理所有客户端请求。

服务类通过 [ServiceBehavior(InstanceContextMode=<setting>)] 属性指定实例行为,如下面的代码示例所示。 通过更改注释掉的行,可以观察每个实例模式的行为。 更改实例化模式之后要记着重新生成服务。 没有要在客户端上指定的实例化相关设置。

// Enable one of the following instance modes to compare instancing behaviors.
 [ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]

// PerCall creates a new instance for each operation.
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]

// Singleton creates a single instance for application lifetime.
//[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class CalculatorService : ICalculatorInstance
{
    static Object syncObject = new object();
    static int instanceCount;
    int instanceId;
    int operationCount;

    public CalculatorService()
    {
        lock (syncObject)
        {
            instanceCount++;
            instanceId = instanceCount;
        }
    }

    public double Add(double n1, double n2)
    {
        operationCount++;
        return n1 + n2;
    }

    public double Subtract(double n1, double n2)
    {
        Interlocked.Increment(ref operationCount);
        return n1 - n2;
    }

    public double Multiply(double n1, double n2)
    {
        Interlocked.Increment(ref operationCount);
        return n1 * n2;
    }

    public double Divide(double n1, double n2)
    {
        Interlocked.Increment(ref operationCount);
        return n1 / n2;
    }

    public string GetInstanceContextMode()
    {   // Return the InstanceContextMode of the service
        ServiceHost host = (ServiceHost)OperationContext.Current.Host;
        ServiceBehaviorAttribute behavior = host.Description.Behaviors.Find<ServiceBehaviorAttribute>();
        return behavior.InstanceContextMode.ToString();
    }

    public int GetInstanceId()
    {   // Return the id for this instance
        return instanceId;
    }

    public int GetOperationCount()
    {   // Return the number of ICalculator operations performed
        // on this instance
        lock (syncObject)
        {
            return operationCount;
        }
    }
}

static void Main()
{
    // Create a client.
    CalculatorInstanceClient client = new CalculatorInstanceClient();
    string instanceMode = client.GetInstanceContextMode();
    Console.WriteLine("InstanceContextMode: {0}", instanceMode);
    DoCalculations(client);

    // Create a second client.
    CalculatorInstanceClient client2 = new CalculatorInstanceClient();

    DoCalculations(client2);

    Console.WriteLine();
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();
}

运行示例时,操作请求和响应将显示在客户端控制台窗口中。 将显示服务正在运行的实例模式。 每次操作后,都会显示实例 ID 和操作计数,以反映实例化模式的行为。 在客户端窗口中按 Enter 关闭客户端。

设置、生成和运行示例

  1. 确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。

  2. 若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。

  3. 若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。