简体   繁体   English

InstanceContextMode.PerCall在wcf中充当InstanceContextMode.Single

[英]InstanceContextMode.PerCall acts as a InstanceContextMode.Single in wcf

I am trying to create a wcf service host with the help of code only(no config is involved). 我试图仅借助代码来创建wcf服务主机(不涉及配置)。 I have a static int and InstanceContextMode.PerCall set for the wcftestservice. 我为wcftestservice设置了一个静态int和InstanceContextMode.PerCall。 As per the tutorial provided on the internet, I should be having same value for every call to the wcf? 根据互联网上提供的教程 ,每次调用wcf都应该具有相同的价值?

Note: I have tested this behavior in console application as well as windows service and I am testing the behavior with the help of wcf test client 注意:我已经在控制台应用程序和Windows服务中测试了此行为,并且正在wcf测试客户端的帮助下测试了该行为。

Here is the code: 这是代码:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class WcfServiceTest : IContract
{
    static int _counter;
    public string GetData(string p1)
    {
        return Convert.ToString(_counter++);
    }
}

Contract: 合同:

[ServiceContract]
public interface IContract
{
    [OperationContract]
    string GetData(string p1);
}

The service host code: 服务主机代码:

    static ServiceHost _servicehost;

    static void Main(string[] args)
    {
        string tcpPort = "8081";
        string httpPort = "8888";
        string urlWithoutProtocol = "{0}://localhost:{1}/WcfServiceTest";
        string netTcpAddress = string.Format(urlWithoutProtocol, "net.tcp", tcpPort);
        string httpAddress = string.Format(urlWithoutProtocol, "http", httpPort);

        string netTcpMexAddress = netTcpAddress + "/mex";
        string httpMexAddress = httpAddress + "/mex";
        if (_servicehost != null)
        {
            _servicehost.Close();
        }
        _servicehost = new ServiceHost(typeof(wcftest.WcfServiceTest));
        ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
        //smb.HttpGetUrl = httpUri;
        //smb.HttpGetEnabled = true;
        _servicehost.Description.Behaviors.Add(smb);
        _servicehost.AddServiceEndpoint(typeof(wcftestcontract.IContract), new NetTcpBinding(), netTcpAddress);
        Binding mexTcpBinding = MetadataExchangeBindings.CreateMexTcpBinding();
        _servicehost.AddServiceEndpoint(typeof(IMetadataExchange), mexTcpBinding, netTcpMexAddress);

        _servicehost.AddServiceEndpoint(typeof(wcftestcontract.IContract), new BasicHttpBinding(), httpAddress);
        Binding mexHttpBinding = MetadataExchangeBindings.CreateMexHttpBinding();
        _servicehost.AddServiceEndpoint(typeof(IMetadataExchange), mexHttpBinding, httpMexAddress);

        _servicehost.Open();
        Console.ReadKey();
    }

If you want to check that it creates only once for Singleton mode, and each time for PerCall mode, then you need to increment your variable in constructor: 如果要检查它为Singleton模式仅创建一次,还是为PerCall模式每次创建,那么您需要在构造函数中增加变量:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class WcfServiceTest : IContract
{
    static int _counter;

    public WcfServiceTest()
    {
        _counter++;
    }
    public string GetData(string p1)
    {
        return Convert.ToString(_counter);
    }
}

Then for each call number will increase for PerCall, and it won't increase for Singleton mode 然后,对于每个电话,PerCall的电话号码将增加,而对于Singleton模式,此电话号码不会增加

Yes, tutorial has a bug. 是的,教程有一个错误。 In their sample should be this without static modifier: 在他们的示例中应该没有静态修饰符:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class MyService:IMyService
{
    int m_Counter = 0;

    public int MyMethod()
    {
        m_Counter++;
        return m_Counter;
    }       
}

Static variable means that it should be the same for all instances of object. 静态变量意味着它对于对象的所有实例都应该相同。 So, for static variable there is no difference between PerCall and Signleton modes, as it will be the same. 因此,对于静态变量,PerCall和Signleton模式之间没有区别,因为它是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将InstanceContextMode.Single转换为InstanceContextMode.PerCall - convert InstanceContextMode.Single to InstanceContextMode.PerCall 将WCF Windows服务作为InstanceContextMode.PerCall运行 - Running WCF Windows Service as InstanceContextMode.PerCall WCF ConcurrencyMode Single和InstanceContextMode PerCall - WCF ConcurrencyMode Single and InstanceContextMode PerCall WCF中的InstanceContextMode.PerCall是否会实例化所有静态变量/方法? - Will InstanceContextMode.PerCall in WCF instantiate all static variables/methods? 使用DI和InstanceContextMode.Percall定制ServiceHost - Custom ServiceHost with DI and InstanceContextMode.Percall 使用设置为ConcurrencyMode.Multiple和I​​nstanceContextMode.PerCall的WCF服务行为属性时是否可能出现并发问题? - Are concurrency issues possible when using the WCF Service Behavior attribute set to ConcurrencyMode.Multiple and InstanceContextMode.PerCall? InstanceContextMode.Single中的许多回调 - Many callback in InstanceContextMode.Single 温莎城堡Wcf自身托管实例为InstanceContextMode.Single - Castle Windsor Wcf self host with InstanceContextMode.Single InstanceContextMode.Single和maxConcurrentSessions之间的关系 - Relationship between InstanceContextMode.Single and maxConcurrentSessions IDispatchMessageInspector和InstanceContextMode。不保留单个顺序 - IDispatchMessageInspector and InstanceContextMode.Single order not maintained
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM