【问题标题】:Saving file in IIS hosted WCF Service在 IIS 托管的 WCF 服务中保存文件
【发布时间】:2012-04-17 08:05:19
【问题描述】:

问题

当在 IIS 中托管的 WCF 服务中使用 绝对 文件名(例如 C:/tinkiwinki/dipsy.lala)和 System.Drawing.Image.Save(Stream, ImageCodecInfo, EncoderParameters) 方法将图像保存在服务器中时,它不会保存图像甚至不抛出异常。

背景

我们有一个辅助类,它有一个SaveJpeg 方法:

public class Tools
{
    public static void SaveJpeg(string path, Image img, int quality)
    {
        if (quality < 0 || quality > 100)
            throw new ArgumentOutOfRangeException("Quality must be between 0 and 100.");

        EncoderParameter qualityParam =
            new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);

        ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = qualityParam;

        img.Save(path, jpegCodec, encoderParams);
    }
}

我们在 WCF OperationContract 方法中使用它,如下所示:

string destDirectory = ServerDataFilePath.ImagesPath + @"\" + uploadID.ToString();
if (!Directory.Exists(destDirectory))
    Directory.CreateDirectory(destDirectory);
Tools.SaveJpeg(destDirectory + @"\" + fileName, img, 50);

静态ServerDataFilePath.ImagesPath 是这样定义的:

public class ServerDataFilePath
{
    public static string ImagesPath{ get { return ConfigurationManager.ConnectionStrings["ImagesPath"].ConnectionString; } }
}

它从 Web.config 文件中检索 ConnectionString 的位置:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="ImagesPath" connectionString="C:\testServerImagesPath\"/>
  </connectionStrings>
</configuration>

问题

当我们在本地运行它时它工作得很好,但是当我们使用 IIS 将它部署到我们的测试服务器时,它不会保存图像并且不会抛出任何异常。为什么会这样?

一些限制

代码采用 C#,框架 4,在 Visual Studio 2010 Professional 中构建。该服务部署在 IIS 中的 Microsoft Windows Server 2003 R2 中,服务类型为 .NET 4 中的 WCF。

请帮忙。提前致谢。

【问题讨论】:

  • 这张图片是从客户端上传的吗?如果是,您是否正确流式传输文件?
  • @BrijeshMishra 是的,我们实际上使用byte[] 来检索图像并将其转换为Image。我们已经在本地进行了测试,它运行良好,现在的问题是我们在 IIS 中部署服务时保存图像。

标签: c# wcf iis .net-4.0 save


【解决方案1】:

很可能在 IIS 上运行您的 Web 应用程序的帐户没有C:\tinkiwinki 文件夹的写入权限。

【讨论】:

  • 谢谢。实际上,C:\tinkiwinki 文件夹还不存在。我希望 Directory.CreateDirectory() 方法为我创建它,或者不是?我会检查一下我们的 IIS 的权限。
  • 如果运行您的应用的帐户没有对 C: 的写入权限,它也无法创建该目录。
  • 如何在我们驱动的安全权限中添加IIS? IIS 服务是否有对象名称?
  • 您需要弄清楚您的应用在哪个帐户下运行。这将是运行定义您的应用程序的应用程序池的帐户。要了解更多信息,请查看learn.iis.net/page.aspx/624/application-pool-identities
猜你喜欢
  • 2022-12-13
  • 2020-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-18
  • 2012-07-06
相关资源
最近更新 更多