我刚接触服务堆栈并且已经忙碌了好几个小时,试图让servicestak为我工作。目前主要的表演限制是我不能让例外部分工作。我通过书籍和服务注册了所有插件,包括REST,Soap,CSV,XML和JSV。该项目包含4个基本测试方法,用于客户对象的crud操作。当抛出错误时,我没有得到预期的错误:没有设置ResponseStatus并且生成了一个generel错误。有人可以帮我找出原因吗?
https://dl.dropbox.com/u/101619220/TestingServiceStack.zip
编辑:感谢您发表评论:)
我创建了一个简单的AppHost文件:
命名空间TestingServiceStack { 公共类AppHost:AppHostBase { 公共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] 公共类GetCustomers { [数据成员] 公共字符串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 proberly,任何提示都表示赞赏。
答案 0 :(得分:2)
我按书籍和服务注册了所有插件,包括REST,Soap,CSV,XML和JSV 为了回应@mythz,通过明确陈述的错误或例外示例回答直接问题会更容易。我上面提到的语句/概括的问题是我不知道“书中的”是什么意思,也不知道你的工作概念是什么(可以构建成功,显示元数据页面等)
未设置ResponseStatus并生成generel错误。
在您的CustomersService类中,它看起来是抛出错误(HttpError)并捕获/记录它。然后代码将继续返回null响应。 ServiceStack本身支持抛出异常。如果您在throw
中添加catch
(假设您要保留捕获以进行日志记录),则应该获得一个填充的ResponseStatus。
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; //Let ServiceStack do its thing.
}
return response;
<强>了SoapUI 强>
此更改可能会解决soapUI的问题,但我不清楚您收到的“一般错误”。我猜这个问题可能是由于'反序列化'空响应。
答案 1 :(得分:1)
ServiceStack不支持使用第三方SOAP库或客户端代理进行故障排除。
有关集成测试中的异常示例,请参阅WebServicesTests.cs。对于SOAP,您还需要阅读ServiceStack's SOAP Support and its limitations
答案 2 :(得分:1)
我们遇到了一个没有填充ResponseStatus的问题 - 我们使用DataContract / DataMember修饰了Response DTO,但没有使用ResponseStatus属性。一旦我们添加了这些装饰,一切都很快乐。