服务端代码: 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”