如何写出来与内存中的永久存储WCF服务? [英] How to write a WCF service with in-memory persistent storage?

查看:219
本文介绍了如何写出来与内存中的永久存储WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个WCF服务,而是存储在服务实现数据不挖墙角调用之间,甚至如果储存在一个静态变量。我该怎么办?

该服务实现如下:

 公开级存储:的IStorage
{
    受保护的静态对象[] _data;

    #地区的IStorage会员

    公共无效插入(对象[]数据)
    {
        锁定(_data)
        {
             _data = _data.Concat(数据).ToArray();
        }
    }

    公共对象[]全选()
    {
        锁定(_data)
        {
            返程(对象[])_ data.Clone();
        }
    }

    #endregion
}
 

该服务主机是一个控制台应用程序:

 静态无效的主要(字串[] args)
{
    ServiceHost的ServiceHost的=
       新的ServiceHost(typeof运算(TimeSpanStorage));
    serviceHost.Open();
    Console.WriteLine(服务运行请确认键退出......。);
    到Console.ReadLine();
}
 

解决方案

在默认情况下WCF instanceMode设置为每通话,这意味着在服务中使用的数据是特定于客户端的方法调用。

在您的实现尝试添加

  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Single)
公共类为MyService:IService
 

这使得该服务实际上是一个单例。

I wrote a WCF service, but the data stored in the Service implementation doesn't persists between calls, not even if stored in a static variable. What can I do?

The service implementation is as follows:

public class Storage : IStorage
{
    protected static object[] _data;

    #region IStorage Members

    public void Insert(object[] data)
    {
        lock (_data)
        {
             _data = _data.Concat(data).ToArray();
        }
    }

    public object[] SelectAll()
    {
        lock (_data)
        {
            return (object[])_data.Clone();
        }
    }

    #endregion
}

The service host is a console application:

static void Main(string[] args)
{
    ServiceHost serviceHost =
       new ServiceHost(typeof(TimeSpanStorage));
    serviceHost.Open();
    Console.WriteLine("Service running.  Please 'Enter' to exit...");
    Console.ReadLine();
}

解决方案

By default WCF instanceMode is set to Per call, meaning data used in the service is specific to that client for that method call.

On your implementation try adding

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Single)]
public class MyService: IService

This makes the service essentially a singleton.

这篇关于如何写出来与内存中的永久存储WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆