私はサービススタックに不慣れで、本当に楽しんでいますが、なぜこれが起こっているのかを一生理解することはできません.
I have mapped it as ROUTES.Add<images>("/Images"); in the APPHOST.cs
Rest Console(chrome プラグイン) を使用して、次の JSON をテストおよび POST します: (実際の base64 エンコーディングではないことはわかっています)。
任意の考えをいただければ幸いです。
// Testing JSON
{"base64Encoding":"asdasdasdasd"}
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ServiceStack.ServiceInterface;
using RewoServiceLayer.RequestDTOs;
using System.Data.Entity;
using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.Configuration;
using ServiceStack.Common;
using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface.Auth;
using ServiceStack.WebHost.Endpoints;
using System.IO;
using System.Web.Hosting;
namespace Blah.Services
{
public class ImageResponse
{
public images image { get; set; }
public ResponseStatus ResponseStatus { get; set; } //Where Exceptions get auto-serialized
}
public class ImageRequest
{
public String base64Encoding;
}
public class ImageService : Service
{
//Give me something that looks like this:
//"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAABG4...YII=";
public ImageResponse Post(ImageRequest request)
{
String base64EncodingImg = request.base64Encoding;
using (var db = new BlahDB())
{
//save to db
images imgToSave = new images();
//get base64
string base64 = base64EncodingImg.Substring(base64EncodingImg.IndexOf(',') + 1);
base64 = base64.Trim('\0');
byte[] imgBinData = Convert.FromBase64String(base64);
//get the
imgToSave.image_type = base64EncodingImg.Substring(0,base64EncodingImg.IndexOf(','));
db.images.Add(imgToSave);
db.SaveChanges();
//then save string contents to disk using ID
imgToSave.image_disk_loc = getPathFromImage(imgToSave);
writeByteArrToDisk(imgBinData, imgToSave.image_disk_loc);
db.SaveChanges();
ImageResponse imgResponse = new ImageResponse();
imgResponse.image = imgToSave;
return imgResponse;
}
}
private Boolean writeByteArrToDisk(byte[] toWrite, String path)
{
try
{
File.WriteAllBytes(path, toWrite);
return true;
}
catch (Exception e)
{
return false;
}
}
private String getAbsolutePathToImagesFolder()
{
return HostingEnvironment.MapPath(@"~/App_Data/UploadedImages");
}
private String getPathFromImage(images imgModel)
{
if (imgModel.image_disk_loc.IsNullOrEmpty())
{
return getAbsolutePathToImagesFolder() + imgModel.image_id;
}
else
{
return imgModel.image_disk_loc;
}
}
}
}
次のエラーが発生し、デバッグ時に Post メソッドにヒットしません。
{
"responseStatus": {
"errorCode": "NullReferenceException",
"message": "Object reference not set to an instance of an object.",
"stackTrace": " at ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(Type requestDtoType)\r\n at ServiceStack.WebHost.Endpoints.EndpointHost.ApplyRequestFilters(IHttpRequest httpReq, IHttpResponse httpRes, Object requestDto)\r\n at ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)"
}
}