gpt4 book ai didi

WCF 服务不能同时运行

转载 作者:行者123 更新时间:2023-12-04 02:49:26 25 4
gpt4 key购买 nike

我的服务代码如下:

 [ServiceContract]
public interface IService1
{
[OperationContract]
void MyOperation1();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession,ConcurrencyMode=ConcurrencyMode.Multiple)]
public class service1 : IService1
{
public service1()
{
Console.WriteLine("creating instance");
}
public void MyOperation1()
{
Console.WriteLine("starting..");
Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(1000);
Console.WriteLine("Ending..");
}
}

我的客户端代码如下:

static void Main(string[] args)
{
Thread[] t = new Thread[10];
NetTcpBinding myBinding = new NetTcpBinding();
EndpointAddress myEndpoint = new EndpointAddress("net.tcp://localhost:8000/MyService");
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);
IService1 instance = myChannelFactory.CreateChannel();
for (int i = 0; i < 10; i++)
{
t[i] = new Thread(new ThreadStart(delegate()
{
instance.MyOperation1();
}));
}
for (int i = 0; i < 10; i++)
{
t[i].Start();
}
}

我得到一个输出(一个一个地执行调用):

creating instance
starting..
3
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..
starting..
12
Ending..
starting..
3
Ending..

我希望所有调用都像这样并行开始:

creating instance
starting..
starting..
starting..
starting..
3
Ending..
4
6
Ending..

最佳答案

这可能是因为您使用的是单个客户端( channel )实例。

您创建的新线程必须等待前一个线程释放 channel 才能使用它。

关于WCF 服务不能同时运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com