使用Windows服务将文件上载到Dropbox期间捕获到System.Configuration.ConfigurationErrorsException

时间:2017-05-25 06:29:51

标签: c# windows-services dropbox dropbox-api

此代码在控制台应用程序中运行良好,但我收到Windows服务错误,说配置系统无法初始化(捕获了System.Configuration.ConfigurationErrorsException)

 private static async Task Backup()
    {
        using (var dbx = new DropboxClient("<REDACTED>")) 
        {
            string folder = "DropboxUpload";
            string file = "setup.exe";
            string content = @"D:\Share\setup.exe";
            log.Info("Chunk upload file...");
            // Chunk size is 100 mb.
            const int chunkSize = 2 * 1024 * 1024;
            using (var stream = new FileStream(content, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                int numChunks = (int)Math.Ceiling((double)stream.Length / chunkSize);
                byte[] buffer = new byte[chunkSize];
                string sessionId = null;
                for (var idx = 0; idx < numChunks; idx++)
                {
                    log.Info("Start uploading chunk {0}" +idx);
                    var byteRead = stream.Read(buffer, 0, chunkSize);
                    using (MemoryStream memStream = new MemoryStream(buffer, 0, byteRead))
                    {
                        if (stream.Length <= chunkSize)
                        {  
                          // here i am getting exception
                         await dbx.Files.UploadAsync(new CommitInfo("/" +folder + "/" + file), body: memStream);
                        }
                        else if (idx == 0)
                        {
                            var result = await dbx.Files.UploadSessionStartAsync(body: memStream);
                            sessionId = result.SessionId;
                        }
                        else
                        {
                            UploadSessionCursor cursor = new UploadSessionCursor(sessionId, (ulong)(chunkSize * idx));
                            if (idx == numChunks - 1)
                            {
                                await dbx.Files.UploadSessionFinishAsync(cursor, new CommitInfo("/" + folder + "/" + file), memStream);
                                log.Info("BackUp Completed...!!!");
                            }
                            else
                            {
                                await dbx.Files.UploadSessionAppendV2Async(cursor, body: memStream);
                            }
                        }
                    }
                }
            }
        }
    }

App Config文件

   <?xml version="1.0" encoding="utf-8"?>
   <configuration>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
   </startup>
   <configSections>
          <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
    </configSections>
  <log4net>
   <!-- Define some output appenders -->
      <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="Logs\RIPBackupService.txt" />
    <appendToFile value="false" />
     <maxSizeRollBackups value="10" />
     <maximumFileSize value="100MB" />
     <rollingStyle value="Size" />
  <LockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <staticLogFileName value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <header value="START SESSION" />
    <footer value="END SESSION" />
    <conversionPattern value="%date [%thread] %-5level %logger | %message%newline" />
  </layout>
</appender>
<!-- Setup the root category, add the appenders and set the default level -->
<root>
     <level value="ALL" />
    <appender-ref ref="RollingLogFileAppender" />
  </root>
   <logger name="Raid.LogFile">
     <level value="ALL" />
   </logger>
 </log4net>

  <system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
  <providers>
    <add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
      </providers>
    </membership>
       <roleManager defaultProvider="ClientRoleProvider" enabled="true">
    <providers>
         <add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
  </providers>
</roleManager>
 </system.web>
</configuration>

1 个答案:

答案 0 :(得分:2)

您的应用设置中有一个重复的密钥,您可以检查应用程序设置app.confg