【问题标题】:WCF service is receiving null value from clientWCF 服务正在从客户端接收空值
【发布时间】:2015-12-01 13:21:16
【问题描述】:

我写了一个实现视频处理功能的服务。

从客户端(控制台项目),我使用客户端服务引用调用服务的函数,并将 FileStream 作为参数发送给函数(我验证它确实在客户端获得了正确的值)。

但是当FileStream 参数进入服务时 - 我遇到空异常问题,FileStream 中没有正确的值。

我该如何解决?

我的代码:

服务:

public class VideoProcess : IVideoProcess
{
    public void UploadVideo(int VideoPartNumber, FileStream videoFile, Guid ApplicatId, Guid TransactionCode)
    {
    }
}

我的客户:

 FileStream videoFile = new FileStream(@"C:\VJob\gizmo.mp4", FileMode.Open, FileAccess.Read);     

//vpc id the client service reference
vpc.UploadVideo(2222, videoFile, new Guid("324792c9-d43c-4e38-8f94-7fc0ed2d7492"), Guid.NewGuid());

【问题讨论】:

  • 您是否调试过服务代码以查看引发异常的位置?
  • 是的,当试图从 FileStream 中获取 name 属性时
  • 那段代码是什么?当时究竟是什么?将该信息编辑到您的问题中。
  • 是的,从一开始我就可以打开文件流并在字段中看到 nullExceptions,尝试从文件流中获取 name 属性并获得“未知”,并在尝试转换文件时发生执行从名称到另一种类型。
  • 当我将鼠标悬停在 FileStream 上方并打开它时,我可以看到内部参数:Handle: 'videoFile.Handle' 抛出了 'System.Null ReferenceException' 类型的异常,与 Length 字段和名称中的内容相同是“未知”

标签: c# web-services wcf argumentnullexception filestreams


【解决方案1】:

当您在服务中收到 wcf 请求时,FileStream 对象被序列化,然后反序列化为一个新对象,这个新对象将是一个 Stream,而不是 FileStream。 Stream 对象没有 Name 属性。从另一个角度来看,FileStream 由本地文件系统备份。由于将文件内容发送到远程服务而不是文件系统是显而易见的,因此发送 Name 属性是不合逻辑的。

如果您的服务应用程序依赖于 Name 属性,那么您可以使用另一个参数将名称数据发送到服务,例如:

public class VideoProcess : IVideoProcess
{
    public void UploadVideo(int VideoPartNumber, Stream videoData, String videoFileName, Guid ApplicatId, Guid TransactionCode)
    {
    }
}

或创建一个模型,然后像这样使用它:

public class VideoPart {
    public Stream data {get;set;}
    public String Name {get;set;}
    public int VideoPartNumber {get;set;}
}
//then the server method signature would be
//...
public class VideoProcess : IVideoProcess
{
    public void UploadVideo(VideoPart part, Guid ApplicatId, Guid TransactionCode)
    {
        // ... some process ...
    }
}

【讨论】:

    【解决方案2】:

    不要在 WCF 中使用 FileStream 作为参数。 FileStream 是绑定到本地文件系统的流,因此您会在服务器端收到 NullReferenceException 尽管您确实从客户端发送了一个正确的 FileStream 对象。我的建议如下:

    使用 byte[] 作为 WCF 参数 > 在服务器端写入文件 > 在本地读取文件

    客户端:

        // Use byte[] as WCF parameter
        FileInfo fileInfo = new FileInfo(path);
        long length = fileInfo.Length;
        FileStream fileStream = new FileStream(path, FileMode.Open);
        byte[] buffer = new byte[length];
        fileStream.Read(buffer, 0, (int)length);
        fileStream.Close();
    
        UploadVideo(buffer);
    

    服务器端:

        // write File in server side
        string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyyMMddHHmmss"));
    
        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }
    
        string filePath = Path.Combine(path, "FileName");
    
        File.WriteAllBytes(filePath , excelByte);
    
        // read file locally
        using (FileStream fileStream = new FileStream(filePath , FileMode.Open, FileAccess.Read))
        {
            // TO DO
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2011-02-01
      相关资源
      最近更新 更多