WCF实现文件的上传与下载

本文详细介绍了如何使用WCF(Windows Communication Foundation)实现文件的上传与下载功能。内容涵盖创建服务、配置宿主机、客户端代码实现及关键配置参数如TransferMode、MaxReceivedMessageSize和MaxArrayLength的设置。通过示例代码展示了服务接口和服务实现,以及客户端如何调用这些接口进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一. 实现描述:

1. 创建文件上传下载的服务:StreamService

2. 宿主机加载文件上传下载服务并发布服务:Host

3. 在宿主中配置服务的ABC,配置文件接收与发送的传输模式/消息传输最大值:host.config

4. 客户端检索引用宿主发布的服务,并调用服务中的接口实现文件的上传与下载:Client

5. 配置客户端中引用服务的文件接收与发送的传输模式/消息传输最大值:client.config

 

二. 实现举例

1. 创建服务

    通过流的方式进行文件的传输,实例中采用MemoryStream。

    服务接口代码IStreamService:

    [ServiceContract]
    public interface IStreamService
    {
        [OperationContract]
        MemoryStream DownloadFile(string fileName);

        [OperationContract]
        void DownloadFileByOutStream(out MemoryStream stream, string fileName);

        [OperationContract]
        void UploadFile(MemoryStream stream);
    }

 

   服务接口实现代码StreamService:

    public class StreamService : IStreamService
    {
        public MemoryStream DownloadFile(string fileName)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
            byte[] content = File.ReadAllBytes(filePath);
            MemoryStream memory = new MemoryStream(content);

            Console.WriteLine("Begin to send file");
            return memory;
        }

        public void DownloadFileByOutStream(out MemoryStream stream, string fileName)
        {
            stream 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值