使用WCF将同一文件(大)复制到多个服务器中

时间:2016-04-17 22:07:26

标签: .net wcf c#-4.0

我想以编程方式将大文件复制到同一网络中的多个服务器中。文件大小从500MB到1GB不等。

我已经使用WCF实现了逻辑,但是将文件复制到一台服务器需要30分钟以上,因此需要一些时间才能复制到多台服务器上。如果有人想到更好的方法来实现这一目标,我将不胜感激。

现在,我正在考虑断开用户并使用C#windows服务(和WCF)在后台复制文件,并在复制过程完成后通知用户。有什么想法吗?

下面提到的代码:

[MessageContract]
public class FileCopyRequest : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName { get; set; }

    [MessageHeader(MustUnderstand = true)]
    public string[] DestinationFilePaths { get; set; }

    [MessageBodyMember(Order = 1)]
    public Stream FileContent { get; set; }

    public void Dispose()
    {
        if (FileContent != null)
        {
            FileContent.Close();
            FileContent = null;
        }
    }
}

public class FileCopyService : IFileCopyService
{
    public void CopyFile(FileCopyRequest request)
    {

       foreach(var destinationPath in request.DestinationFilePaths)
       {
            var buffer = new byte[65000];
            int read;

            using (var destinationStream = new FileStream(destinationPath, FileMode.Create))
            {
                while ((read = request.FileContent.Read(buffer, 0, buffer.Length)) > 0)
                {
                    destinationStream.Write(buffer, 0, read);
                }
            }
        }

    }
}

的Web.Config

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="serviceBehavior">
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" />
      <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="false"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

  <services>
  <!--http://services.myserviceaddress.com/service.svc-->
  <service behaviorConfiguration="serviceBehavior" name="Services.FileCopyService">
    <endpoint address=""
    name="basicHttpStream"
    binding="basicHttpBinding"
    bindingConfiguration="httpLargeMessageStream"
    contract="Services.IFileCopyService" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:37708/" />
      </baseAddresses>
    </host>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<bindings>
  <basicHttpBinding>
    <binding name="httpLargeMessageStream"
                maxReceivedMessageSize="2147483647"
                transferMode="Streamed"
                messageEncoding="Mtom" />
  </basicHttpBinding>
</bindings>


<!--<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>-->    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />


 </system.serviceModel>

0 个答案:

没有答案