且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WCF服务实例的生存期?

更新时间:2022-06-27 00:19:53

每个文档:

只有一个InstanceContext对象可用于所有传入呼叫,并且是通话后未回收.如果服务对象没有存在,一个被创建.

Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.

因此,只有一个实例,并且在调用后不会清除它.这就像您的WCF服务的Singleton.因此,您需要注意共享内存和资源.

So there is only one instance, and it's not cleaned up after a call is made. This is like a Singleton for your WCF service. So you need to be careful about shared memory and resources.

要回答您的问题-是的,这就是它的工作方式.

To answer your question - yes, this is the way it works.

更新添加了示例:我从MSDN修改了一些示例,以显示 InstanceContextMode.Single 的效果.即使我使用两个不同的客户端,您也会看到操作计数将继续增加.如果我将 InstanceContextMode 更改为 PerCall ,则计数将有所不同(为零).

UPDATE Added sample: I modified a few samples from MSDN to show the effects of InstanceContextMode.Single. You'll see the operation count will continue to increment even though I use two different clients. If I change the InstanceContextMode to PerCall, the count will be different (it will be zero).

自托管服务:

[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;
        }
    }
}

public class Program
{

    static void Main(string[] args)
    {
        Uri baseAddress = new Uri("http://localhost:12345/calc");
        using (ServiceHost host = new ServiceHost(typeof(CalculatorService), baseAddress))
        {
            // Enable metadata publishing.
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
            host.Description.Behaviors.Add(smb);

            // Open the ServiceHost to start listening for messages. Since
            // no endpoints are explicitly configured, the runtime will create
            // one endpoint per base address for each service contract implemented
            // by the service.
            host.Open();

            Console.WriteLine("The service is ready at {0}", baseAddress);
            Console.WriteLine("Press <Enter> to stop the service.");
            Console.ReadLine();

            // Close the ServiceHost.
            host.Close();
        }
        Console.WriteLine();
        Console.WriteLine("Press <ENTER> to terminate client.");
        Console.ReadLine();
    }
}

客户端:

class Program
{
    static void Main()
    {
        // Create a client.
        CalculatorInstanceClient client = new CalculatorInstanceClient();
        string instanceMode = client.GetInstanceContextMode();
        Console.WriteLine("InstanceContextMode: {0}", instanceMode);
        Console.WriteLine("client1's turn");
        Console.WriteLine("2 + 2 = {0}", client.Add(2, 2).ToString());
        Console.WriteLine("3 - 1 = {0}", client.Subtract(3, 1).ToString());
        Console.WriteLine("number of operations = {0}", client.GetOperationCount().ToString());

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

        Console.WriteLine("client2's turn");
        Console.WriteLine("2 + 2 = {0}", client2.Add(2, 2).ToString());
        Console.WriteLine("3 - 1 = {0}", client2.Subtract(3, 1).ToString());
        Console.WriteLine("number of operations = {0}", client2.GetOperationCount().ToString());

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