Asp.net web api 流式传输视频文件时,文件被另一个进程使用PushStreamContent锁定。如何解决此问题

Asp.net web api 流式传输视频文件时,文件被另一个进程使用PushStreamContent锁定。如何解决此问题,asp.net-web-api,video-streaming,pushstreamcontent,Asp.net Web Api,Video Streaming,Pushstreamcontent,我正在尝试流式传输视频文件。当我在浏览器的另一个选项卡中打开同一个视频文件时,我收到消息“文件正被另一个进程使用”。若我在file.open方法中使用FileShare.ReadWrite,那个么错误会消失,但视频不会在浏览器中播放。有人能帮忙吗 public HttpResponseMessage Get([string id) { var path = HttpContext.Current.Server.MapPath(ConfigurationMa

我正在尝试流式传输视频文件。当我在浏览器的另一个选项卡中打开同一个视频文件时,我收到消息“文件正被另一个进程使用”。若我在file.open方法中使用FileShare.ReadWrite,那个么错误会消失,但视频不会在浏览器中播放。有人能帮忙吗

public HttpResponseMessage Get([string id)
        {
            var path = HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["path"] + "/" + id);
            var video = new VideoStream(path);
            HttpResponseMessage response = Request.CreateResponse();
            var contentType = ConfigurationManager.AppSettings[Path.GetExtension(id)];
            response.Content = new PushStreamContent(video.WriteToStream,  new MediaTypeHeaderValue(contentType));

            return response;
        }


        public class VideoStream
        {
            private readonly string _filename;

            public VideoStream(string filename)
            {
                _filename = filename;
            }


            public async void WriteToStream(Stream outputStream, HttpContent content, TransportContext context)
            {
                try
                {
                    var buffer = new byte[65536];

                    using (var video = File.Open(_filename, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        var length = (int) video.Length;
                        var bytesRead = 1;

                        while (length > 0 && bytesRead > 0)
                        {
                            bytesRead = video.Read(buffer, 0, Math.Min(length, buffer.Length));
                             await outputStream.WriteAsync(buffer, 0, bytesRead);

                            length -= bytesRead;
                            video.Flush();

                        }
                    }
                }
                catch (HttpException ex)
                {
                    return;
                }
                finally
                {


                 //   outputStream.Close();
                  //  outputStream.Flush();
                }
            }



        }
你应使用: File.Open(名称,FileMode.Open,FileAccess.Read,FileShare.Read)


假设文件锁来自服务器。是这样的,还是客户端的问题?

我已经尝试了文件共享的所有给定选项,但没有任何效果。我每次都会看到相同的黑屏。如果你有权访问服务器,你可能需要查看filemon,查看文件的保存过程,看它是你自己的应用还是其他什么。另外,您没有回答邮件来自何处-是来自客户端还是服务器。感谢Yishai查找我的问题!我已经在我的项目中本地添加了一个文件来调试它,这样服务器上就不会运行其他进程了。原因是视频文件很大,所以当视频仍在播放时,我尝试使用浏览器中另一个选项卡的file.open打开文件,这会导致此错误消息或黑屏,具体取决于是否添加了文件共享。它在服务器端。你能粘贴异常,并验证锁定的文件名是视频文件名而不是另一个文件吗?我有一些其他的意见想法:1。请注意,这里没有理由使用推送流,如果您从文件中获取视频,您知道它来自何处,并且您知道前面的长度。Push stream适用于从未知源获取开放流并将其向下推送到客户端的场景。我不认为你的问题的根源,只是一个设计问题。2.请注意,您实际上只是下载一个文件,而不是流式视频,您所做的工作无法索引到视频3的中间部分。寻找防病毒锁定你的文件,也许禁用测试