c# - IIS 中的 WCF 服务调用中找不到目录错误

标签 c# asp.net wcf iis

我正在使用带有 c# 的 asp.net 3.5

在我的 Web 应用程序中有一个名为 SlideShow 的文件夹,其中包含图像。我想获取具有相应 URL 的图像名称,并使用 WCF 以 JSON 格式返回它。我创建了一个返回包含图像路径和图像数组的 json 字符串的方法。当我使用 ASP.NET 开发服务器运行应用程序时它工作正常,但在 IIS 上出错。

我的界面是

[OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        Stream GetSlideShowImages();

服务方式是

   Stream Iremoteclient.GetSlideShowImages()
        {
                byte[] resultBytes = Encoding.UTF8.GetBytes(new AndroidServices().GetSlideShowImages());
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain";
                saveAndroidRequest();
                return new MemoryStream(resultBytes);
            }

        }

获取json字符串的方法是

public string GetSlideShowImages()
        {
            try
            {
                string relativepath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;

                string pathlastchr = relativepath.Trim().Substring(relativepath.Length - 1, 1);
                if (pathlastchr == "/")
                {
                    relativepath = relativepath.Trim() + "SlideShow/";
                }
                else
                {
                    relativepath = relativepath.Trim() + "SlideShow/";
                }
                string json;
                DirectoryInfo directoryInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath("/SlideShow"));
                FileInfo[] file = directoryInfo.GetFiles().Where(f => f.Extension == (".bmp") || f.Extension == (".jpg") || f.Extension == (".png") || f.Extension == (".TIFF") || f.Extension == (".gif")).ToArray();

                AndroidServices objAndroidServices = new AndroidServices();
                objAndroidServices.ImagePath = relativepath;
                objAndroidServices.Images = file.Select(f => f.Name).ToArray();


                using (MemoryStream ms = new MemoryStream())
                {
                    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AndroidServices));
                    ser.WriteObject(ms, objAndroidServices);
                    json = System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, Convert.ToInt16(ms.Length));
                    json = json.Replace(@"\/", @"/");
                }
                return json;
            }
            catch (Exception ex)
            {                   
                return ex.Message;
            }
        }

在 IIS 上它会给出类似

的错误

" at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean i" and " at System.Text.Encoding.GetBytes(String s) at ihv1Role.service.remoteclient.ihv1Role.service.Iremoteclient.GetSlideShowImages() in E:\VivifyHealth\caregiverihportal\src\service\remoteclient.svc"

有什么解决办法吗?

最佳答案

所以问题是你需要改变这个:

DirectoryInfo directoryInfo =
    new DirectoryInfo(HttpContext.Current.Server.MapPath("/SlideShow"));

为此:

DirectoryInfo directoryInfo =
    new DirectoryInfo(HttpContext.Current.Server.MapPath("~/SlideShow"));

根据 MSDN 文档:

... the slash (/) at the beginning of the path indicates an absolute virtual path to the site.

但是,当您以 ~ 开头时,它将从站点的根目录开始。

关于c# - IIS 中的 WCF 服务调用中找不到目录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896328/

相关文章:

wcf - WCF 服务应用程序和 WCF 服务库之间有什么区别?

c# - Jquery Ajax 请求不适用于 IE 10(由于缓存)

asp.net - 如何使用 jquery 调用 Web 服务将记录插入数据库?

android - WCF webHttpBinding 与文件元数据

asp.net - 静态文件的 IIS 500.0 AuthenticateRequest 错误

c# - 如何实现 CCAvenue 支付网关选项

c# - WebService客户端异常: Cannot Import wsdl:binding

c# - 执行 Microsoft Graph API 的 POST 请求以将成员添加到 AD 组

c# - 如何重新启动 C# WinForm 应用程序?

C# 和 MySQL .NET 连接器 - 有什么方法可以防止泛型类中的 SQL 注入(inject)攻击?