通过WCF将FileStream从客户端传递到服务器

时间:2017-09-04 07:28:42

标签: c# wcf stream

我需要将FileStream从客户端传递给服务器。

服务器端是:

[ServiceContract]
public interface IDocumentService
{
     [OperationContract]
     void ValidateXml(Stream stream);
}

public class DocumentService : IDocumentService
{
     public void ValidateXml(Stream stream)
     {
        if(stream != null)
        {
           stream.Seek(0, SeekOrigin.Begin);
           var reader = XmlReader.Create(stream);
           var doc = XDocument.Load(reader);
           doc.Validate(schema, ValidateEventHadler);           
        }
     }

     private static void ValidationEventHandler(object sender, ValidationEventArgs e)
     {
         throw e.Exception;
     }
}

客户方:

private void LoadXml_Click(object sender, EventArgs e)
{
            var fileDialog = new OpenFileDialog {Filter = @"xml files (*.xml)|*.xml"};
            if (fileDialog.ShowDialog() != DialogResult.OK) return;
            try
            {
                using (Stream fs = new FileStream(fileDialog.FileName, FileMode.Open))
                {                    
                    Stream ms = new MemoryStream();
                    fs.CopyTo(ms);

                Proxy.ValidateXml(ms);
            }       
        }
        catch (Exception ex)
        {
            ErrorDialog.Show(ex);
        }
    }

当我发送流时它会变空。 服务器在本地网络中,我找到了this文章,但我实际上并没有意识到我应该怎么做这个方法:

public static Binding CreateStreamingBinding()
{
    TcpTransportBindingElement transport = new TcpTransportBindingElement();
    transport.TransferMode = TransferMode.Streamed;
    BinaryMessageEncodingBindingElement encoder = new BinaryMessageEncodingBindingElement();
    CustomBinding binding = new CustomBinding(encoder, transport);
    return binding;
}

请帮忙吗?

0 个答案:

没有答案