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

本文关键字:异常 服务 WebClient 请求 Windows | 更新日期: 2024-10-23 12:51:04

我从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>

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

问题出现在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>