我已经尝试按照此处的建议在 Umbraco 和 vanilla MVC 应用程序中实现自定义 IImageService :
https://github.com/JimBobSquarePants/ImageProcessor/issues/105
但我想我可能会错过一个步骤,因为我只收到这个错误:
[NullReferenceException: Object reference not set to an instance of an object.]
ImageProcessor.Web.HttpModules.<ProcessImageAsync>d__b.MoveNext() +755
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
System.Web.TaskAsyncHelper.EndTask(IAsyncResult ar) +71
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +380
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
下面的 DatabaseImageService 在 Prefix 属性和 IsFileLocalService 上被调用,但随后我得到了上面的错误。IsValidRequest() 和 GetImage() 永远不会被调用。
该代码是LocalFileImageService.cs的副本
我也尝试在 ImageProcessor.Web.Config 附带的 security.config 文件中指定此 ImageService,但发生相同的错误。
namespace ImageProcessorTest
{
public class DatabaseImageService : IImageService
{
private string prefix = "database.axd";
public string Prefix
{
get
{
return this.prefix;
}
set
{
this.prefix = value;
}
}
public bool IsFileLocalService
{
get
{
return true;
}
}
public Dictionary<string, string> Settings { get; set; }
public Uri[] WhiteList { get; set; }
public bool IsValidRequest(string path)
{
return ImageHelpers.IsValidImageExtension(path);
}
public async Task<byte[]> GetImage(object id)
{
string path = id.ToString();
byte[] buffer;
FileInfo fileInfo = new FileInfo(path);
if (!fileInfo.Exists)
{
throw new HttpException((int)HttpStatusCode.NotFound, "No image exists at " + path);
}
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
{
buffer = new byte[file.Length];
await file.ReadAsync(buffer, 0, (int)file.Length);
}
return buffer;
}
}
}
更新 1
上面的代码示例是在一个普通的 MVC 应用程序中。配置如下:
<?xml version="1.0" encoding="utf-8"?>
<security>
<services>
<service name="LocalFileImageService" type="ImageProcessor.Web.Services.LocalFileImageService, ImageProcessor.Web" />
<!--Disable the LocalFileImageService and enable this one when using virtual paths. -->
<!--<service name="CloudImageService" type="ImageProcessor.Web.Services.CloudImageService, ImageProcessor.Web">
<settings>
<setting key="Container" value=""/>
<setting key="MaxBytes" value="8194304"/>
<setting key="Timeout" value="30000"/>
<setting key="Host" value="http://yourhost.com/"/>
</settings>
</service>-->
<service prefix="remote.axd" name="RemoteImageService" type="ImageProcessor.Web.Services.RemoteImageService, ImageProcessor.Web">
<settings>
<setting key="MaxBytes" value="4194304" />
<setting key="Timeout" value="3000" />
<setting key="Protocol" value="http" />
</settings>
<whitelist>
</whitelist>
</service>
**<service prefix="database.axd" name="DatabaseImageService" type="ImageProcessorTest.DatabaseImageService, ImageProcessorTest" />**
</services>
</security>
为了清楚起见,我已经完成了以下步骤来达到这一点:
- 香草 MVC 应用程序
- 安装:ImageProcessor ImageProcessor.Web 和 ImageProcessor.Web.Config 通过 NuGet
- 上面创建了 DatabaseImageService 类
- 如图所示编辑了security.config
我在网站“hami-1.jpg”的根目录中有一张图片。我可以使用默认的 LocalFileImageService http://localhost:11832/hami-1.jpg?width=200 正确操作它。
我为自定义 IImageService 指定的前缀是“database.axd”,因此 url 应该是:?
- http://localhost:11832/database.axd/hami-1.jpg?width=200
- http://localhost:11832/database.axd?hami-1.jpg?width=200
? 在这一点上我不确定,因为我已经看到了两者的例子。?
在任何情况下,当我调试时,我都不会在 IsValidRequest 或 GetImage 方法中遇到任何断点。我确实在 IsFileLocalService 中遇到了断点。
这是一个非常基本的设置,所以我确定这是我所缺少的。
最终,我想用我自己的方法替换 GetImage 方法,该方法将使用以下方法从数据库中检索图像:
private AppContext _db = new AppContext();
private MembershipService MembershipService
{
get
{
if (ms == null)
{
ms = new MembershipService(_db, UmbracoContext.Current);
}
return ms;
}
}
public async Task<byte[]> GetImage(object id = null)
{
var image = await _db.Images.FindAsync(id);
var currentMember = MembershipService.GetCurrentMember();
if (image != null)
{
var content = image.Content;
_db.Dispose();
switch (image.Privacy)
{
case Models.Base.Privacy.Private:
if (image.Owner.Id == currentMember.Id ||MembershipService.FindMembersInRole(currentMember.UmbracoMember.Username, "Judges").Any())
return content;
break;
case Models.Base.Privacy.Unlisted:
return content;
case Models.Base.Privacy.Public:
return content;
}
}
throw new HttpException(404, "Image not found for id: " + id);
}