我有一些服务实现:
public class RemoteService : Hub
{
public string GetImage()
{
SharpDX.Direct3D11.Device device;
Output1 output1;
int width, height;
Texture2D screenTexture;
InitDx(out device, out output1, out width, out height, out screenTexture);
MemoryStream memoryStream = MakeScreenShot(device, output1, width, height, screenTexture);
MemoryStream compressedMemoryStream = Compress(memoryStream);
return JsonConvert.SerializeObject(compressedMemoryStream, Formatting.Indented, new MemoryStreamJsonConverter());
}
//...
}
在客户端上,我调用函数GetImage
:
myHub.Invoke<string>("GetImage").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("There was an error calling send: {0}",
task.Exception.GetBaseException());
}
else
{
Console.WriteLine(task.Result);
}
});
我只想在服务端初始化InitDx(out device, out output1, out width, out height, out screenTexture);
一次,而不是多次调用函数MakeScreenShot(device, output1, width, height, screenTexture);
。
问题在于,SignalR为GetImage
的每个请求创建RemoteService
的新实例,而我将无法在RemoteService
中保存初始化的变量。如何使用SignalR解决此问题。我在这个领域是新手,也许存在一些简单的常用方法。