我正在使用带有 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/