1.打开VS选择控制台项目新建一个解决方案Server,然后添加两个类库Contract和Service.

2.在Contract中添加一个接口IFileDownload

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; namespace Contract
{
[ServiceContract]
public interface IFileDownload
{
[OperationContract]
Dictionary<string, byte[]> DownloadFile(string[] fileNames);
}
}

3.在Service中实现接口IFileDownload

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Contract;
using System.IO; namespace Service
{
public class FileDownload : IFileDownload
{
public Dictionary<string, byte[]> DownloadFile(string[] fileNames)
{
Dictionary<string, byte[]> filedata = new Dictionary<string, byte[]>();
string queryDirectory = AppDomain.CurrentDomain.BaseDirectory + "数据"; //假设服务器数据存放在运行目录下的“数据”文件夹中
DirectoryInfo drtInfo = new DirectoryInfo(queryDirectory);
FileInfo[] fileInfo = drtInfo.GetFiles();
foreach (FileInfo item in fileInfo)
{
foreach (string filename in fileNames)
{
if (filename == item.Name)
{
FileStream fsRead = new FileStream(queryDirectory+"\\"+filename,FileMode.Open,FileAccess.Read);
byte[] bytDt = new byte[fsRead.Length];
fsRead.Read(bytDt,,bytDt.Length);
fsRead.Close();
filedata.Add(filename,bytDt);
break;
}
}
} return filedata;
}
}
}

3.在Server中添加如下代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Service; namespace Server
{
class Program
{
static void Main(string[] args)
{
ServiceHost download_host = new ServiceHost(typeof(FileDownload));
download_host.Open();
Console.WriteLine("文件下载服务已启动");
Console.Read();
}
}
}

4.在Server中添加一个应用程序配置文件App.xml,并选择菜单栏->工具->WCF服务配置编辑器对配置文件进行编辑,具体操作可参考http://www.cnblogs.com/judastree/archive/2012/08/26/2657555.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="DownloadFileBindingConfig" closeTimeout="20:56:00" openTimeout="20:30:00" sendTimeout="20:56:00" maxBufferSize="2073741824" maxReceivedMessageSize="2073741824" transferMode="Buffered" maxBufferPoolSize="2073741824" >
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
<message clientCredentialType="Windows"/>
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="DownloadBehavior">
<serviceMetadata httpGetEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.FileDownload" behaviorConfiguration="DownloadBehavior">
<endpoint address="net.tcp://localhost:2310/Download" binding="netTcpBinding"
bindingConfiguration="DownloadFileBindingConfig" contract="Contract.IFileDownload" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:2310" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>

5.启动Server

6.新建一个窗体应用程序的解决方案Client,添加服务引用

7.修改Client的配置文件app.xml为

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IFileDownload" maxBufferSize="2073741824" maxReceivedMessageSize="2073741824">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:2310/Download" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IFileDownload" contract="ServiceReference1.IFileDownload"
name="NetTcpBinding_IFileDownload" />
</client>
</system.serviceModel>
</configuration>

8.添加一个TextBox和一个Button,在Button下添加代码

 private void button1_Click(object sender, EventArgs e)
{
ServiceReference1.FileDownloadClient fileDownload = new ServiceReference1.FileDownloadClient();
List<string> filenames = new List<string>();
filenames.Add(textBox1.Text); //为了简单,这里只查找一个文件
Dictionary<string,byte[]> downloadfiles = fileDownload.DownloadFile(filenames.ToArray());
foreach (var item in downloadfiles)
{
if (item.Value!=null)
{
FileStream fsWrite = new FileStream(@"C:\Users\yujian\Desktop\DownloadFiles\"+item.Key,FileMode.Create);//这里下载wav文件,可根据实际情况调整
fsWrite.Write(item.Value,,item.Value.Length);
fsWrite.Close();
}
}
MessageBox.Show("下载成功");
}

9.运行结果

源码

WCF客户端从服务器下载数据的更多相关文章

  1. java技术用ssh从linux服务器下载数据

    通常需要从linux服务器获取数据文件,而通常能有的访问方式只有ssh,所以就可以用ssh进行数据下载. java连接远程主机的方式有多种,这里和大家分享的是通过ssh方式连接远程主机,使用的是jsc ...

  2. ICE学习第四步-----客户端请求服务器返回数据

    这次我们来做一个例子,流程很简单:客户端向服务器发送一条指令,服务端接收到这条指令之后,向客户端发送数据库中查询到的数据,最终显示在DataGridView上. 根据上一篇文章介绍的Slice语法,我 ...

  3. C语言Socket-单工通信(客户端向服务器发送数据)

    服务端(server) #include <stdio.h> #include <winsock2.h> #pragma comment(lib,"ws2_32.li ...

  4. tcp客户端从服务器下载文本文件

    代码讲解: server import socket def send_file_client(new_client_socket, new_client_addr): # 接收客户端需要下载的文件名 ...

  5. WCF客户端和服务器时间不一致,导致通道建立失败的问题)

    本文转载:http://www.cnblogs.com/bcbr/articles/2288374.html 最近,经常有客户反应,前天还用的好好的系统,今天就不能用了. 考虑到系统近来没有做过改动和 ...

  6. WCF客户端获取服务器返回数据报错

    错误信息:An error occurred while receiving the HTTP response to http://127.0.0.1/SIHIS/Infection/PubExec ...

  7. nodejs的cs模式聊天客户端和服务器实现

    学习完nodejs的基础后,自然要写点东西练练手,以下是一个基于nodejs的cs模式的聊天软件代码: net模块是nodejs的网络编程必定用到的一个模块,对socket通信进行了封装 实现的功能: ...

  8. 加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证

    加密解密(2)*客户端,服务器,CA(Certificate Authority),公钥,私钥,证书,签名,验证 各角色比喻 客户端:通常为请求方,要验证服务器的身份. 服务器:通常为响应方,有时也要 ...

  9. WCF客户端调用服务器端错误:"服务器已拒绝客户端凭据"。

    WCF客户端和服务器端不在同一台机器上时,客户端调用服务器端会报如下错误:"服务器已拒绝客户端凭据". 解决办法:在服务端配置文件与客户端配置文件中加入下面红色部分

随机推荐

  1. Hystrix介绍以及服务的降级限流熔断

    (dubbo熔断,Hystrix问的少) 无论是缓存层还是存储层都会有出错的概率,可以将它们视同为资源.作为并发量较大的系统,假如有一个资源不可用,可能会造成线程全部 hang (挂起)在这个资源上, ...

  2. FZU 2254 英语考试

    在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习. 这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m. YellowSatr准备采用联想记忆法来背诵这 ...

  3. 006、容器 What、Why、How(2018-12-21 周五)

    参考https://www.cnblogs.com/CloudMan6/p/6751516.html   What - 什么是容器?       容器是一种轻量级.可移植.自包含的软件打包技术,是应用 ...

  4. 在O(n) 时间复杂度,O(1)空间复杂度内反转单链表

    在LeetCode中看到判断回文的程序:https://leetcode.com/problems/palindrome-linked-list/ 里面用单链表来存储数据,先反转前半部分的单链表,然后 ...

  5. Element link doesn't have required attribute property

    前端标准http://validator.w3.org/ 拒绝你的代码时报 Element link doesn't have required attribute property 把样式链接 &l ...

  6. Java图片比对

    在自动化测试中,除了普通的值验证,经常还有一些图片验证,比如图片的匹配率,输出图片的差异图片等.本文主要用到了BufferedImage类来操作图片比对和输出差异图片,大体的思路如下: 1. 通过Im ...

  7. 阿里云服务器安装SQLServer本地无法远程访问

    新买的阿里云服务器,安装上sqlserver2012,本机连接测试没有问题,但是回到本地,使用ip远程连接报错. 尝试了网上各种办法,都是失败.最后找到原因,原来在阿里云的控制台上有设置: 首先进入安 ...

  8. buildroot构建项目(二)--- u-boot 2017.11 建立 2440 开发板

    一.准备工作 在建立之前,先需要将下载的u-boot 拷贝一份出来解压,在此工程下进行更改和创建.同时根据前面搜索到的 mini2440开发板所在的版本,下载一份u-boot 拷贝出 mini2440 ...

  9. C语言入门教程-(6)运算符

    1.运算符概述 运算符是一种编译器执行特定的数学或逻辑操作的符号.C语言提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 条件运算符 其他运算符 2.算术运算符 算术 ...

  10. django错误笔记(xadmin)——AttributeError: 'Settings' object has no attribute 'TEMPLATE_CONTEXT_PROCESSORS'

    使用Xadmin,执行makemigrations和migrate时运行报错提示: AttributeError: 'Settings' object has no attribute 'TEMPLA ...