3
我正在使用带有IReturn接口的“新”API。我所有的呼叫都被解析为/ api/json/syncreply路由,而不是在插件注册中指定的路由。ServiceStack:为什么我的呼叫没有解决以更正路由?
如果我在浏览器中点击了url,我得到了正确的回复,所以这部分工作,但如果使用JsonServiceClient它不能正确解析路线。
var dataService = new JsonServiceClient(baseUri);
dataService.Get(new BaseProductBenefitTypeEdit() { Code = ((BaseProductBenefitTypeInfo)obj).Code }
, onSuccess, onError);
的DTO
public class BaseProductBenefitTypeEdit : IReturn<BaseProductBenefitTypeEditResponse>
{
public string Code { get; set; }
}
public class BaseProductBenefitTypeEditResponse : IHasResponseStatus
{
public BaseProductBenefitTypeEditInfo BenefitType { get; set; }
public List<KeyValuePair> AvailableMostCommon { get; set; }
public List<KeyValuePair> AvailableTypes { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
我有插件
public class MaintenanceModule : IPlugin
{
public void Register(IAppHost appHost)
{
//baseProductBenefitTypes
appHost.Routes.Add<BaseProductBenefitTypeList>("/baseProductBenefitTypes", "GET");
appHost.Routes.Add<BaseProductBenefitTypeEdit>("/baseProductBenefitTypes/{code}/editForm", "GET");
appHost.Routes.Add<BaseProductBenefitTypeCreate>("/baseProductBenefitTypes/createForm", "GET");
appHost.Routes.Add<BaseProductBenefitTypeSave>("/baseProductBenefitTypes/{code}", "PUT");
appHost.Routes.Add<ChangeBaseProductBenefitTypesDisplayOrder>("/baseProductBenefitTypes/displayOrders", "POST");
appHost.RegisterService<BaseProductBenefitTypeService>();
在Global.asax中的应用在Application_Start开始我打电话
ServiceStackInitilizer.Run();
,它看起来像这
public static class ServiceStackInitilizer
{
public static void Run()
{
var type = typeof(ServiceStack.ServiceInterface.Service);
var types = AppDomain.CurrentDomain.GetAssemblies()
.Where(x=>x.GetName().Name.StartsWith("MyApplication"))
.SelectMany(a => a.GetTypes())
.Where(type.IsAssignableFrom);
var assemblies = types.GroupBy(t => t.Assembly).Select(g => g.Key).ToArray();
new WebServiceAppHost("WebServiceAppHost", assemblies).Init();
}
}
在WebServiceAppHost配置方法我注册的插件:
public override void Configure(Funq.Container container)
{
JsConfig.IncludeNullValues = true;
SetConfig(new EndpointHostConfig
{
ServiceStackHandlerFactoryPath = "api",
DefaultContentType = ServiceStack.Common.Web.ContentType.Json,
DefaultJsonpCacheExpiration = new TimeSpan(0, 0, 0, 0),
GlobalResponseHeaders = { { "Cache-Control", "no-cache" } },
});
container.Adapter = new StructureMapContainerAdapter();
RegisterMyPlugins();
}
private void RegisterMyPlugins()
{
var type = typeof(IPlugin);
var plugins = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => x.GetName().Name.StartsWith("Application"))
.SelectMany(a => a.GetTypes())
.Where(type.IsAssignableFrom);
foreach (var plugin in plugins)
{
Plugins.Add((IPlugin)plugin.CreateInstance());
}
}
就在你回答之前,我查看了ServiceClientBase的代码,并发现它正在寻找dto属性中的路由信息。但是,我是否需要两部分,在Plugin中注册路线,并使用Route属性注释dto? – epitka 2013-05-06 20:31:10
@epitka不,你只需要在一个地方注册路由 - 我通常更喜欢使用'[Route]'属性,即元数据在客户端可用。 – mythz 2013-05-06 20:36:39
非常感谢您的快速响应。 – epitka 2013-05-06 20:40:30