1

我是 servicestack 的新手,我真的很喜欢它,但是我无法终生了解为什么会发生这种情况。

I have mapped it as ROUTES.Add<images>("/Images"); in the APPHOST.cs

我使用 Rest Console(chrome 插件)来测试和发布以下 JSON:(我知道它不是实际的 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)"
    }
}
4

1 回答 1

3

我看不到你的images课,但我认为你需要改变

ROUTES.Add<images>("/Images");

ROUTES.Add<ImageRequest>("/Images")

因为您的 ImageService 上的 Post 正在寻找 ImageRequest 类型的请求。

于 2013-06-07T06:19:16.517 回答