有多个客户端,它们会通过调用WCF提供的服务API向服务器发送不同类型的请求。一旦收到请求(调用其中一个 WCF 服务 API),WCF API 会将其打包到另一个消息对象中,然后通过队列发送到主线程继续处理。消息处理后,将给予回复。
我的问题是:
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();
}
}
消息类别为:
公共类 OpenConnection:消息 { 公共字符串 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);
}
}
主要程序是:
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);
}
}
}
}
如何将主线程中实例化的Queue传递给WCF 服务启动前的服务?
WCF 中的队列传输使用 MSMQ 进行队列通信。它作为 Windows 的可选组件提供,并作为 NT 服务运行。 MSMQ 队列管理器实现可靠的消息传输协议,该协议捕获传输队列中的传输消息并在目标队列中传递消息。这两个链接指向详细介绍和安装。
如何启动WCF服务?
在wcf中,需要托管服务。它不能独立运行,需要托管在程序中。有多种方法:在托管应用程序中自托管、托管 Windows 服务、IIS 和 Windows 进程激活服务。他们有自己的应用程序,你可以看看这个文档。
此外,您可以查看此案例了解有关多线程的其他想法。希望有帮助。