Saving WCF data between sessions

We are developing a system based on WCF. In the process, we try to block some data from being changed by several users. Therefore, we decided to create a data structure that will contain the necessary information to execute the blocking logic (for example, to store the identifier of blocked objects).

The problem we are facing is storing data between sessions. In any case, can we avoid making expensive database calls? I'm not sure how we can do this in WCF, since it can only save data (in memory) during an open session.

+3
source share
4 answers

.

+4

, . WCF, IP-. PerCall. , , IP-. .

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    void Start(IPAddress address);
}

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
public class MyService : IMyService
{
    private static readonly List<IPAddress> _addresses = new List<IPAddress>();
    public void Start(IPAddress address)
    {
        lock(((ICollection)_addresses).SyncRoot)
        {
            if (!_addresses.Contains(address)
            {
                // Open the connection here and then store the address.
                _addresses.Add(address);
            }
        }
    }
}

"" () , . , .

, , , . , , , . , .

Single service PerCall.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class MyService : IMyService
{ ... }

, , PerCall .

.

, , , - . , #. , , , ..

+3
0

Create a second class and set its InstanceContextMode instance to a single and move all the expensive methods there, then use these methods in your original class.

0
source

Source: https://habr.com/ru/post/1699329/


All Articles