Windows服务WebClient请求期间发生异常

时间:2013-12-18 07:29:36

标签: c# .net vb.net windows-services

我从Windows服务应用程序

收到以下错误
   System.Net.WebException = {"An exception occurred during a WebClient request."}
    -2146233079
    InnerException = {"Configuration system failed to initialize"}

控制台应用程序上的相同代码可以正常工作。

服务文件未在“本地服务”(在我的测试环境中)设置安全性

你能指出我可能导致错误的原因以及解决方法吗?

    Try
        Using client As New Net.WebClient
            Dim reqparm As New Specialized.NameValueCollection
            reqparm.Add("DeviceId", deviceId)
            reqparm.Add("StatusCode", statusCode)
            reqparm.Add("Type", "printer")
            reqparm.Add("Message", msg)
            Dim responsebytes = client.UploadValues("http://xxx.xxx.com/api/notification", "POST", reqparm)
            Dim responsebody = (New Text.UTF8Encoding).GetString(responsebytes)
        End Using
    Catch ex As Exception
        'do nothing
    End Try

我的App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="app" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <app>
    <add key="version" value="0.0.1"/>
  </app>
</configuration>

1 个答案:

答案 0 :(得分:0)

问题出在App.config文件中。这是正确的版本,只有<configSections>的一个实例,应该是<configuration>之后的第一个元素。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="app" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <app>
    <add key="version" value="0.0.1"/>
  </app>
</configuration>