我是服务堆栈的新手,并且已经挣扎了几个小时,试图让服务堆栈为我工作。目前主要的表演障碍是我无法使异常部分起作用。我按本书注册了所有插件,服务适用于 REST、Soap、CSV、XML 和 JSV。该项目包含 4 种基本测试方法,用于对客户对象进行 crud 操作。抛出错误时,我没有得到预期的错误:未设置 ResponseStatus 并生成一般错误。有人可以帮我找出原因吗?
https://dl.dropbox.com/u/101619220/TestingServiceStack.zip
编辑:感谢您的评论:)
我创建了一个简单的 AppHost 文件:
命名空间 TestingServiceStack { public class AppHost : AppHostBase { public AppHost() : base("StarterTemplate ASP.NET Host", typeof(CustomersService).Assembly) { }
public override void Configure(Container container)
{
Plugins.Add(new ValidationFeature());
Plugins.Add(new RequestLogsFeature());
SetConfig(new EndpointHostConfig
{
DebugMode = true, //Enable StackTraces in development
});
LogManager.LogFactory = new Log4NetFactory(true);
JsConfig.EmitCamelCaseNames = true;
JsConfig.DateHandler = JsonDateHandler.ISO8601;
Routes.Add<GetCustomers>("/customers", "GET")
.Add<GetCustomers>("/customers/{Id}", "GET")
.Add<AddCustomer>("/customers", "POST")
.Add<UpdateCustomer>("/customers/{Id}", "PUT")
.Add<DeleteCustomer>("/customers/{Id}", "DELETE");
}
public static void Start()
{
new AppHost().Init();
}
}
}
还有一项服务:
命名空间 TestingServiceStack { 公共类 CustomersService : 服务 { #region 日志记录
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#endregion
public object Any(GetCustomers request)
{
GetCustomersResponse response = null;
try
{
if (request.Id != "0")
throw HttpError.NotFound("Id {0} throws error".Fmt(request.Id));
response = new GetCustomersResponse {Id = request.Id ?? "notset", Name = "GetCustomers"};
}
catch (Exception ex)
{
Log.Error(base.RequestContext.Get<IHttpRequest>(), ex);
throw;
}
return response;
}
public object Any(AddCustomer request)
{
return new AddCustomerResponse {Id = request.Id, Name = "AddCustomer"};
}
public object Any(UpdateCustomer request)
{
return new UpdateCustomerResponse {Id = request.Id, Name = request.Name};
}
public object Any(DeleteCustomer request)
{
return new DeleteCustomerResponse {Id = request.Id, Name = "DeleteCustomer"};
}
}
}
交换的对象是:
使用 System.Runtime.Serialization;使用 ServiceStack.ServiceInterface.ServiceModel;
命名空间 TestingServiceStack { [DataContract] public class GetCustomers { [DataMember] public string Id { get; 放; } }
[DataContract]
public class UpdateCustomer
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class AddCustomer
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
}
[DataContract]
public class DeleteCustomer
{
[DataMember]
public string Id { get; set; }
}
[DataContract]
public class GetCustomersResponse : IHasResponseStatus
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
[DataContract]
public class UpdateCustomerResponse : IHasResponseStatus
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
[DataContract]
public class AddCustomerResponse : IHasResponseStatus
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
[DataContract]
public class DeleteCustomerResponse : IHasResponseStatus
{
[DataMember]
public string Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public ResponseStatus ResponseStatus { get; set; }
}
}
我使用 SoapUi 调用 GetCustomers 方法,如果 id 等于 0,则会引发错误,我希望设置 ResponseStatus,但事实并非如此。从 SoapUi 调用时,出现以下错误:
我不知道如何正确设置 reponsestatus,任何提示都表示赞赏。