1

我正在尝试设置模块化 ServiceStack 实现,但我似乎无法弄清楚如何解决我的插件。

这是我的 ASP.Net MVC 4 Global.asax.cs:

 public class MvcApplication : System.Web.HttpApplication
{
    [Route("/heartbeat")]
    public class HeartBeat
    {
    }

    public class HeartBeatResponse
    {
        public bool IsAlive { get; set; }
    }

    public class ApiService : Service
    {
        public object Any(HeartBeat request)
        {
            var settings = new AppSettings();

            return new HeartBeatResponse { IsAlive = true };
        }
    }
    public class AppHost : AppHostBase
    {
        public AppHost() : base("Api Services", typeof(ApiService).Assembly) { }

        public override void Configure(Funq.Container container)
        {
            Plugins.Add(new ValidationFeature());
            Plugins.Add(new StoreServices());
        }
    }
    protected void Application_Start()
    {
        new AppHost().Init();
    }

这很好,我可以看到可用的“心跳”服务。但是找不到插件加载的服务。

下面是插件代码:

public class StoreServices: IPlugin
{
    private IAppHost _appHost;

    public void Register(IAppHost appHost)
    {
        if(null==appHost)
            throw new ArgumentNullException("appHost");

        _appHost = appHost;
        _appHost.RegisterService<StoreService>("/stores");
    }
}

以及它加载的相应服务:

 public class StoreService:Service
{
    public Messages.StoreResponse Get(Messages.Store request)
    {
        var store = new Messages.Store {Name = "My Store", City = "Somewhere In", State = "NY"};
        return new Messages.StoreResponse {Store = store};
    }
}


[Route("/{State}/{City}/{Name*}")]
[Route("/{id}")]
public class Store : IReturn<StoreResponse>
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string State { get; set; }
}

public class StoreResponse
{
    public Store Store { get; set; }
}

运行心跳的 url 来自 localhost}/heartbeat,meta 可以在 localhost}/metadata 中找到。

当我尝试调用 {from localhost}/stores/1234 时,虽然我得到了一个未解析的路由?但是如果您在服务调用中看到路由属性,它应该会解析吗?

以下是我对商店请求的响应:

Handler for Request not found: 


Request.ApplicationPath: /
Request.CurrentExecutionFilePath: /stores/123
Request.FilePath: /stores/123
Request.HttpMethod: GET
Request.MapPath('~'): C:\Source Code\White Rabbit\SpiritShop\SpiritShop.Api\
Request.Path: /stores/123
Request.PathInfo: 
Request.ResolvedPathInfo: /stores/123
Request.PhysicalPath: C:\Source Code\White Rabbit\SpiritShop\SpiritShop.Api\stores\123
Request.PhysicalApplicationPath: C:\Source Code\White Rabbit\SpiritShop\SpiritShop.Api\
Request.QueryString: 
Request.RawUrl: /stores/123
Request.Url.AbsoluteUri: http://localhost:55810/stores/123
Request.Url.AbsolutePath: /stores/123
Request.Url.Fragment: 
Request.Url.Host: localhost
Request.Url.LocalPath: /stores/123
Request.Url.Port: 55810
Request.Url.Query: 
Request.Url.Scheme: http
Request.Url.Segments: System.String[]
App.IsIntegratedPipeline: True
App.WebHostPhysicalPath: C:\Source Code\White Rabbit\SpiritShop\SpiritShop.Api
App.WebHostRootFileNames: [global.asax,global.asax.cs,packages.config,spiritshop.api.csproj,spiritshop.api.csproj.user,spiritshop.api.csproj.vspscc,web.config,web.debug.config,web.release.config,api,app_data,bin,obj,properties]
App.DefaultHandler: metadata
App.DebugLastHandlerArgs: GET|/stores/123|C:\Source Code\White Rabbit\SpiritShop\SpiritShop.Api\stores\123
4

1 回答 1

1

此代码不会像您假设的那样为您的服务提供 url 前缀:

_appHost.RegisterService<StoreService>("/stores");

相反,可选params string[] atRestPaths仅指定该服务的DefaultRequest路由的路由。[DeafultRequest]您可以使用该属性指定哪个操作是默认操作,例如:

[DefaultRequest(typeof(Store))]
public class StoreService : Service { ... }

它允许您在线指定路由,而不是在请求 DTO 上,即:

_appHost.RegisterService<StoreService>(
   "/stores/{State}/{City}/{Name*}",
   "/stores/{Id}");

但是由于您已经获得了 Request DTO 上的路由,您可以在此处忽略它们,即:

_appHost.RegisterService<StoreService>();

但是您需要包含缺少的/storesurl 前缀,例如:

[Route("/stores/{State}/{City}/{Name*}")]
[Route("/stores/{Id}")]
public class Store : IReturn<StoreResponse> { .. }
于 2013-11-08T23:15:32.580 回答