大网数据广告活动
Web服务器与PHP

FileNotFoundException when you call web service

· 52CE 我爱测

This article provides resolutions for the System.IO.FileNotFoundException error that occurs when a Microsoft ASP.NET Web application calls a web service.

This article helps you resolve the problem that System.IO.FileNotFoundException occurs when an ASP.NET Web application calls a web service.

Original product version:   ASP.NET
Original KB number:   823196

Symptoms

When you call a web service in an ASP.NET web application, you may receive the following error:

System.IO.FileNotFoundException

Cause

You may receive the error if one of the following conditions is true:

  • The worker process does not have permissions to read to the process Temp directory, and the worker process does not have permissions to write to the process Temp directory.

    [!NOTE] The XmlSerializer class generates and compiles code on the fly to perform serialization and deserialization. XmlSerializer uses Code Document Object Model (CodeDom) to perform the compilation. CodeDom compilation uses temporary files on a disk. If the worker process doesn't have the read permissions on the Temp directory and the write permissions on the Temp directory, then all calls to the Web service fail. Therefore, the worker process requires the read permissions on the Temp directory and the write permissions on the Temp directory.

  • There are compilation errors in the code that XmlSerializer generated.

Resolution 1: Assign permissions to worker process account on the Temp directory

To resolve this problem, the ASP.NET worker process account (the ASPNET account or the NETWORK SERVICE account if your application is deployed on Internet Information Services (IIS) 6.0) must have read access and write access on the Temp directory.

Note: If you use impersonation, the impersonated user must have full access on the Temp directory.

To assign required permissions to the worker process account on the Temp directory, follow these steps:

  1. In Windows Explorer, locate the %windir%\temp directory.

  2. Right-click %windir%\temp, and then select Properties.

  3. In the Properties window, select the Security tab.

  4. Select Add, type ServerName\ASPNET, and then select OK.

    [!NOTE] Replace ServerName with the name of the web server.

    Replace ASPNET with NETWORK SERVICE if you deployed your application on IIS 6.0.

  5. Under Allow, select the Full Control check box, and then select OK.

Resolution 2: Find compiler errors in the code that XmlSerializer generated

To find errors that are generated by the compiler, you must add a switch to the Web.config file to keep compiler-generated files. To do this, follow these steps:

  1. Open the Web.config file in a text editor, such as Notepad.

  2. Add an XmlSerialization.Compilation switch to the <system.diagnostics> section of the code, as follows:

    <configuration>
        <system.diagnostics>
            <switches>
                <add name="XmlSerialization.Compilation" value="4"/>
            </switches>
        </system.diagnostics>
    </configuration>
    
  3. Run the client application.

    The client application calls the web service.

  4. Verify that the %windir%\temp directory has the _tmpname.00.cs file and the _tmpname.out file.

    The _tmpname.00.cs file is the generated source. The _tmpname.out file should have the compiler errors.

    [!NOTE] Enable the read permissions and enable the write permissions to the worker process account (ASPNET or NETWORK SERVICE) to write %tmpname% files in the Temp directory.

Status

This behavior is by design.

Steps to reproduce the behavior

The following sections provide information about steps to reproduce the behavior.

Create a web service

  1. Start Visual Studio .NET.

  2. Create a new ASP.NET Web service project by using Visual C# .NET or Visual Basic .NET.

    By default, Service1.asmx is created.

  3. Name the project WebServiceTemp.

  4. In Solution Explorer, right-click Service1.asmx, and then select View Code.

  5. In the Service1.asmx.cs file (or the Service1.asmx.vb file if you are using Visual Basic .NET), uncomment the default HelloWorld() web method.

  6. On the Build menu, select Build Solution.

Create a client web application

  1. Create a new ASP.NET Web application by using Visual C# .NET or Visual Basic .NET.

  2. Name the project WebAppTemp.

  3. In Solution Explorer, right-click References, and then select Add Web Reference.

  4. In the Address text box, type the following URL for WebServiceTemp:
    http://localhost/WebServiceTemp/Service1.asmx

  5. Select Go, and then select Add Reference.

  6. Double-click WebForm1 to open the Page_Load event code.

  7. Append the following code to the Page_Load event handler.

    • Visual C# .NET sample code

      // Start an instance of the Web service client-side proxy.
      localhost.Service1 myProxy = new localhost.Service1();
      Response.Write( myProxy.HelloWorld());
      
    • Visual Basic .NET sample code

      'Start an instance of the Web service client-side proxy.
      Dim myProxy As localhost.Service1 = New localhost.Service1()
      Response.Write(myProxy.HelloWorld())
      
  8. On the Build menu, select Build Solution.

Set permissions on the Temp directory

To assign required permissions to the worker process account on the Temp directory, follow these steps:

  1. In Windows Explorer, locate the %windir% directory.

  2. Right-click %windir%\temp, and then select Properties.

  3. On the Properties window, select the Security tab.

  4. Select Add, type ServerName\ASPNET, and then select OK.

  5. Make sure that the Write check box is not selected under Allow, and then select OK.

  6. Run the web application.

    You may receive the error that is mentioned in the Symptoms section of this article.

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 03/26/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。