Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IIS中的WCF服务调用中未找到目录错误_C#_Asp.net_Wcf_Iis - Fatal编程技术网

C# IIS中的WCF服务调用中未找到目录错误

C# IIS中的WCF服务调用中未找到目录错误,c#,asp.net,wcf,iis,C#,Asp.net,Wcf,Iis,我正在使用asp.NET3.5和c# 在我的web应用程序中,有一个名为SlideShow的文件夹,其中包含图像。我想获得带有相应URL的图像名称,并使用WCF以JSON格式返回它。我创建了一个方法,返回包含图像路径和图像数组的json字符串。当我使用ASP.NET development server运行应用程序时,它工作正常,但在IIS上出现错误 我的界面是 [OperationContract] [WebInvoke(Method = "GET", ResponseForm

我正在使用asp.NET3.5和c#

在我的web应用程序中,有一个名为
SlideShow
的文件夹,其中包含图像。我想获得带有相应URL的图像名称,并使用WCF以JSON格式返回它。我创建了一个方法,返回包含图像路径和图像数组的json字符串。当我使用ASP.NET development server运行应用程序时,它工作正常,但在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.\uuu Error.WinIOError(Int32 errorCode,字符串 maybeFullPath)在 System.IO.Directory.InternalGetFileDirectoryNames(字符串路径,字符串 userPathOriginal、字符串搜索模式、布尔i”和 “at System.Text.Encoding.GetBytes(字符串s)at ihv1Role.service.remoteclient.ihv1Role.service.Iremoteclient.GetSlideShowImages() 在E:\VivifyHealth\mainerIHPortal\src\service\remoteclient.svc”中


有什么解决方案吗?

因此,问题是您需要更改以下内容:

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

DirectoryInfo directoryInfo =
    new DirectoryInfo(HttpContext.Current.Server.MapPath("~/SlideShow"));
根据MSDN文档:

…路径开头的斜杠(/)表示站点的绝对虚拟路径

但是,当你在它前面加上
~
时,它将从网站的根目录开始