[英]Windows Service An exception occurred during a WebClient request
I receive the following error from a Windows Service application 我从Windows服务应用程序收到以下错误
System.Net.WebException = {"An exception occurred during a WebClient request."}
-2146233079
InnerException = {"Configuration system failed to initialize"}
The same code on a Console application works fine. 控制台应用程序上的相同代码可以正常工作。
The Service file has not setup Security on "Local Service" (in my test environment) 服务文件尚未在“本地服务”上设置安全性(在我的测试环境中)
Could you point me out what could cause the error and how to solve it? 您能否指出我可能导致该错误的原因以及如何解决该错误?
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
my App.config 我的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>
The problem was in the App.config file. 问题出在App.config文件中。 This the correct version, only one instance of
<configSections>
and should be the first element after <configuration>
. 这是正确的版本,只有
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.