日期:2014-05-18  浏览次数:21036 次

wcf多线程
我有一个wcf服务。宿主是这么写的,前面是连接oracle数据库
C# code

            string ConnectionString = "Data Source=SDETEST;user=sde;password=sde;Pooling=true;Min Pool Size=4;Max Pool Size=1000";
            OracleConnection conn = new OracleConnection(ConnectionString);
            conn.Open();
            RestServiceImpl rest = new RestServiceImpl(conn);
            WebServiceHost host = new WebServiceHost(rest);
            host.Open();
            Console.WriteLine("Service is running");
            Console.ReadLine();
            host.Close();
;

之前服务里这样用[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
客户端调用没问题。可是我要让数据库能够并发访问,先是将oncurrencyMode = ConcurrencyMode.Single改为Multiple,可是本身service并发明显不行。于是就将InstanceContextMode = InstanceContextMode.Single改为percall,可是改了以后host.open()提示出错了,错误内容是:
  “In order to use one of the ServiceHost constructors that takes a service instance, the InstanceContextMode of the service must be set to InstanceContextMode.Single. This can be configured via the ServiceBehaviorAttribute. Otherwise, please consider using the ServiceHost constructors that take a Type argument.”
  请教各位怎么改才行。。或者给我个新思路,最终目的是用rest能并发访问数据库。

------解决方案--------------------
WebServiceHost host = new WebServiceHost(rest);

有这么个东西么?CaptureWCFService 是我写的WCF实现类的类名。我用的Console做宿主。
你开启数据库为什么要放在Host不放在WCF里呢?

C# code

using (ServiceHost host = new ServiceHost(typeof(CaptureWCFService)))
{
    host.Open();
}

------解决方案--------------------
真好笑,居然把数据库连接写在服务外部,如果你的数据库异常断开了,服务必须重启才能恢复连接太没效率了。