向WCF服务提交Stream类型的数据
来源:互联网 发布:加拿大木结构设计软件 编辑:程序博客网 时间:2024/06/03 20:59
服务端代码: service.svc
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.Test" %>// Copyright (c) Microsoft Corporation. All Rights Reserved.using System;using System.ServiceModel;namespace Microsoft.ServiceModel.Samples{ // Define a service contract. [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")] public interface ITest { [OperationContract] void UploadFile(FileData file); } // Service class which implements the service contract. public class Test : ITest { public void UploadFile(FileData file) { System.IO.FileStream fs = new System.IO.FileStream(@"C:\data\CIS\Inbound\Receive\" + Guid.NewGuid() + file.filename, System.IO.FileMode.OpenOrCreate); try { System.IO.BinaryReader reader = new System.IO.BinaryReader(file.data); byte[] buffer; System.IO.BinaryWriter writer = new System.IO.BinaryWriter(fs); long offset = fs.Length; writer.Seek((int)offset, System.IO.SeekOrigin.Begin); do { buffer = reader.ReadBytes(1024); writer.Write(buffer); } while (buffer.Length > 0); } catch (Exception e) { } finally { fs.Close(); file.data.Close(); } } } [MessageContract] public class FileData { [MessageHeader] public string filename; [MessageBodyMember] public System.IO.Stream data; }}
<?xml version="1.0" encoding="utf-8" ?><configuration> <system.serviceModel> <services> <service name="Microsoft.ServiceModel.Samples.Test" behaviorConfiguration="BindingBehavior"> <endpoint binding="basicHttpBinding" bindingConfiguration ="DocumentExplorerServiceBinding" contract="Microsoft.ServiceModel.Samples.ITest" address="" /> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="BindingBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <bindings> <basicHttpBinding> <binding name="DocumentExplorerServiceBinding" transferMode="Streamed" maxReceivedMessageSize="9223372036854775807"> </binding> </basicHttpBinding> </bindings> </system.serviceModel></configuration>
客户端代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Data.SqlClient;using System.Data;using System.Data.Sql;using System.Configuration;using System.Runtime.Serialization.Formatters.Binary;using System.Xml.Serialization;using System.Xml;using System.Diagnostics;using System.Threading;using System.Runtime.Remoting.Messaging;using System.Net;using System.Net.Sockets;using System.Runtime.Serialization;using SharedUtility;using System.IO.Compression;using System.Xml.Schema;using System.Xml.XPath;using System.Xml.Xsl;using System.Reflection;namespace FirstConsole{ class Program { public static void Main(string[] args) { TestClient client = new TestClient(); client.UploadFile("test.xml", new FileStream(@"test.xml", FileMode.Open)); PressQtoQuit(); } public static void PressQtoQuit() { Console.WriteLine("Hit Q to exit"); ConsoleKey key; do { key = Console.ReadKey().Key; } while (key != ConsoleKey.Q); } }}
客户端配置文件: App.config
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IGetDataService" transferMode="Streamed"> </binding> </basicHttpBinding> </bindings> <client> <endpoint address="http://localhost/calculatorservice/service.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGetDataService" contract="ITest"/> </client> </system.serviceModel></configuration>
在向WCF提交Stream类型的数据的时候,需要设置bingding 的transferMode=“Streamed”
0 0
- 向WCF服务提交Stream类型的数据
- Okhttp使用post向服务器提交 sring[] 类型的数据
- 给WCF增加提交数据的容量。
- AJAX中利用POST方法向服务提交数据时乱码的解决方案
- AJAX中利用POST方法向服务提交数据时乱码的解决方案
- AJAX中利用POST方法向服务提交数据时乱码的解决方案
- 创建Wcf数据服务的事例
- 利用WCF的Duplex服务向Winform程序推送消息
- 利用WCF的Duplex服务向Winform程序推送消息
- 承载WCF 数据服务
- WCF 传送二进制流数据 stream
- WCF+Restfull服务 提交或获取数据时数据大小限制问题解决方案
- 向数据库提交大量数据,提交方式的选择
- WCF处理不同方式提交的数据的方法
- 向网页提交数据
- WCF - Stream
- wcf 服务 List 类型的参数问题1
- 如何向IIS中注册WCF服务
- DNS BIND主辅同步部署
- 2015/4/18(2)
- 再读 K-Means
- poj1068
- HTML编码规范
- 向WCF服务提交Stream类型的数据
- Scala学习笔记
- Oracle数据库PL/SQL学习笔记——函数定义
- 安卓开发中用volley框架请时,中文返回乱码问题
- VI中的多行删除与复制 [转载]
- 想听懂用户的声音,至少得先学会数据分析吧
- 数学逻辑
- Mac下Lua Sublime Text2/3 开发环境搭建(补充)
- 习题7-6 重叠的正方形 UVa12113