向WCF服务提交Stream类型的数据

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

}


服务端配置文件: Web.config

<?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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值