我正在使用带有 IReturn 接口的“新”api。我所有的调用都被解析到 /api/json/syncreply 路由,而不是插件注册中指定的那些。
如果我在浏览器中点击 url,我会得到正确的回复,因此该部分正在工作,但如果使用 JsonServiceClient,它不会正确解析路由。
var dataService = new JsonServiceClient(baseUri);
dataService.Get(new BaseProductBenefitTypeEdit() { Code = ((BaseProductBenefitTypeInfo)obj).Code }
, onSuccess, onError);
Dtos
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());
}
}