gpt4 book ai didi

How to implement my WCF service to achive multi thread function(如何实现我的WCF服务以实现多线程功能)

转载 作者:bug小助手 更新时间:2023-10-22 15:10:33 25 4
gpt4 key购买 nike



I am developing one client-server system, the architecture is as below:
system architecture

我正在开发一个客户端-服务器系统,体系结构如下:系统体系结构


There are multi clients which will send different kinds of request to the Server by calling the WCF service API provided. Once the request received(one of the WCF service API be called), the WCF API will pack it into another message object then send to the main thread by the queue to proceed. After the message been proceed, response will be given.

有多个客户端将通过调用提供的WCF服务API向服务器发送不同类型的请求。一旦收到请求(调用WCF服务API之一),WCF API将把它打包到另一个消息对象中,然后由队列发送到主线程以继续。消息进行后,将给出响应。


My problems are:

我的问题是:



  1. How to pass the Queue which is instanced in the main thread to the WCF service before the service started?

  2. How to start the WCF service?

  3. Is there any other better way to implement my original idea?


The code of WCF service are:

WCF服务的代码为:


[ServiceContract]
public interface IWcfService
{
[OperationContract]

int OpenConnection(string clientId);

bool CloseConnection(string clientId, int connectionId);

int AllocateResource(string connectionId, int resourceType);

int ReleaseResource(string connectionId, int resourceId);
}

public class WcfService : IWcfService
{
Queue MainQueue;

public WcfService(Queue mainQueue)
{
this.MainQueue = mainQueue;
}

public int OpenConnection(string clientId)
{
OpenConnection req = new OpenConnection(clientId);
MainQueue.Enqueue(req);//send request to main thread
int connectionId = req.WaitforResponse();//wait for response
return connectionId;
}

public bool CloseConnection(string clientId, int connectionId)
{
CloseConnection req = new CloseConnection(clientId, connectionId);
MainQueue.Enqueue(req);//send request to main thread
bool succeed = req.WaitforResponse();//wait for response
return succeed;
}

public int AllocateResource(string connectionId, int resourceType)
{
throw new NotImplementedException();
}

public int ReleaseResource(string connectionId, int resourceId)
{
throw new NotImplementedException();
}
}

The message class are:

消息类包括:


public class OpenConnection : Message
{
public string ClientId;

公共类OpenConnection:Message{public string ClientId;


    public OpenConnection(string clientId)
{
this.ClientId = clientId;
this.MsgQueue = new Queue();//create queue to receive message from the main thread
}

public int WaitforResponse()
{
int connectionId = -1;
while (true)
{
if (MsgQueue.Count > 0)
{
connectionId = (int)MsgQueue.Dequeue();
break;
}
}

return connectionId;
}

public void SendResponse(int connectionId)//this function will be called by main thread
{
MsgQueue.Enqueue(connectionId);
}
}

public class CloseConnection : Message
{
public string ClientId;
public int ConnectionId;

public CloseConnection(string clientId, int connectionId)
{
ClientId = clientId;
ConnectionId = connectionId;
this.MsgQueue = new Queue();//create queue to receive message from the main thread
}

public bool WaitforResponse()
{
bool result = false;
while (true)
{
if (MsgQueue.Count > 0)
{
result = (bool)MsgQueue.Dequeue();
break;
}
}

return result;
}

public void SendResponse(bool succeed)//this function will be called by main thread
{
MsgQueue.Enqueue(succeed);
}
}

the main program is:

主要程序是:


static void Main(string[] args)
{
Queue MainQueue = new Queue();//creat queue for communication between WCF service and main thread

static void Main(string[]args){Queue MainQueue=new Queue();//为WCF服务和主线程之间的通信创建队列


        WcfService.WcfService wcf = new WcfService.WcfService(MainQueue);

System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(wcf);

host.Open();
Console.WriteLine("WCF Service Started...");

while(true)
{
if (MainQueue.Count > 0)//proceed message received from the wcf APIs
{
Message.Message msg = (Message.Message)MainQueue.Dequeue();
Type msgType = msg.GetType();
if (msgType == typeof(OpenConnection))
{
OpenConnection newMsg = (OpenConnection)msg;
newMsg.SendResponse(12345);
}
else if (msgType == typeof(CloseConnection))
{
CloseConnection newMsg = (CloseConnection)msg;
newMsg.SendResponse(true);
}
}
}
}

更多回答
优秀答案推荐


How to pass the Queue which is instanced in the main thread to the WCF
service before the service started?



Queued transport in WCF uses MSMQ for queued communication. It comes with Windows as an optional component and runs as an NT service. The MSMQ queue manager implements a reliable message transfer protocol that captures transport messages in the transport queue and delivery messages in the target queue. These two links lead to detailed introduction and installation.

WCF中的排队传输使用MSMQ进行排队通信。它作为可选组件随Windows一起提供,并作为NT服务运行。MSMQ队列管理器实现可靠的消息传输协议,该协议捕获传输队列中的传输消息和目标队列中的传递消息。这两个环节引出了详细的介绍和安装。



How to start the WCF service?



In wcf, services need to be hosted. It cannot operate independently and needs to be hosted in program. There are several ways: Self-Host in a Managed Application, Managed Windows Services, IIS and Windows Process Activation Service. They have their own application, you can take a look at this document.

在wcf中,服务需要托管。它不能独立运行,需要在程序中托管。有几种方法:托管应用程序中的自主机、托管Windows服务、IIS和Windows Process Activation Service。他们有自己的应用程序,你可以看看这个文档。


Besides, you can see this case for other ideas about multithreading. Hope it helps.

此外,您可以在本例中看到有关多线程的其他想法。希望能有所帮助。


更多回答

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