专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Web Service

WCF为何不能并发访问

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
WCF为什么不能并发访问?
C#客户端调用只两条线程,不知道为什么?另有用Java调用服务,可以并行访问。
接口定义
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”。
[ServiceContract]
public interface IService
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: 在此添加您的服务操作
}

// 使用下面示例中说明的数据约定将复合类型添加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";

[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}

[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}

服务定义


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.IO;
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、服务和配置文件中的类名“Service”。
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
public class Service : IService
{
public string GetData(int value)
{
        DateTime startTime = DateTime.Now;
        Int64 n = 30000000;
        while (n > 0)
        {
            Math.Pow(33333333333333333.33333, 23333333);
            n--;
        }
        DateTime endTime = DateTime.Now;
        string msg = string.Format("服务端耗时:{2}ms,start:{0}_{1}", startTime.ToString("HH:mm:ss.ffffff"), endTime.ToString("HH:mm:ss.ffffff"),
            (endTime - startTime).TotalMilliseconds);
        Console.WriteLine(msg);
return string.Format("Server:{0},{1}", value,msg);
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

客户端
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication1
{
    
    class Program
    {
        static Stopwatch sw = new Stopwatch();
        static GTService.Service client = new GTService.Service();
        class AAA
        {
            public int Value = -1;
            public AAA(int value)
            {
                Value = value;
            }
        }
        static void Main(string[] args)
        {
            sw.Start();
            ThreadPool.SetMinThreads(25, 25);
            for (int i = 0; i < 20; i++)
            {
                AAA a = new AAA(i);
                System.Threading.ThreadPool.QueueUserWorkItem(DoWork,a);
            }
            Console.ReadKey();
        }
        static void DoWork(Object valuei)
        {
            AAA a = valuei as AAA;
            int value = a.Value;
            DateTime startTime = DateTime.Now;
           
            string serverMsg=client.GetData(value, true);
            DateTime endTime = DateTime.Now;
            string clientMsg = string.Format("客户端耗时:{2}ms,start:{0}_{1},", 
                startTime.ToString("HH:mm:ss.ffffff"), endTime.ToString("HH:mm:ss.ffffff"),
                (endTime - startTime).TotalMilliseconds);
            string msg = string.Format("累计时长:{3}ms,client:{0},{1}__{2}", 
                value, clientMsg, serverMsg,sw.ElapsedMilliseconds);
            Console.WriteLine(msg);
            Debug.WriteLine(msg);
        }
    }
}

------解决思路----------------------
找到原因了,你的服务协议估计应该用的http一类,http一类都存在这个问题,改成tcp就没问题了
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: