C# 向ServiceStack添加服务
我正在尝试向ServiceStack添加新服务,但无法识别该服务,并且我的路由未显示在元数据中 这是我的服务:C# 向ServiceStack添加服务,c#,web-services,rest,
servicestack,C#,Web Services,Rest,
servicestack,我正在尝试向ServiceStack添加新服务,但无法识别该服务,并且我的路由未显示在元数据中 这是我的服务: public class EventService : Service { public object Post(EventRequest event_request) { return new EventResponse() { name = "FirstEvent" } } } public cla
public class EventService : Service
{
public object Post(EventRequest event_request)
{
return new EventResponse() {
name = "FirstEvent"
}
}
}
public class EventRequest
{
public int event_id { get; set; }
}
[Route("/event", "POST")]
public class EventResponse {
public string name { get; set; }
}
我甚至在AppHost中显式引用了EventService,即使它们位于同一程序集中。我只是在基本服务教程代码中添加了一个服务,我的服务定义在与HelloService相同的名称空间中
public AppHost() //Tell ServiceStack the name and where to find your web services
: base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly, typeof(EventService).Assembly) { }
我还尝试过停止和启动IIS express服务
我遗漏了什么?它不会工作,因为您将
[Route]
应用到了错误的类。您需要在请求DTO而不是响应DTO上定义路由。所以你应该这样定义它:
[Route("/event", "POST")]
public class EventRequest : IReturn<EventResponse>
{
public int event_id { get; set; }
}
您现在没有定义元数据,因为没有方法将您的响应EventResponse
用作请求DTO。所以,只是一件很小的事情引起了你的问题
箱中的旧维修组件
:
从bin
中删除SecondWbService.dll
。这是一个较旧的服务,正在加载,而不是MainWebService.dll
——您实际上正在编辑并希望运行的服务。由于ServiceStack不允许使用多个AppHost,WebActivator将首先查找旧的DLL并运行它,因此您的服务将被隐藏。删除该DLL后,请重新运行解决方案,并应正确选取该解决方案。您可以通过添加断点来确认这一点:
public AppHost() //Tell ServiceStack the name and where to find your web services
: base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly, typeof(EventService).Assembly)
{ // BREAKPOINT HERE, confirm the assembly is loaded
}
元数据和服务应该可以正常工作。这是一个有用的响应,但并没有解决我的问题。我已经按照您指定的方式进行了这些更改,但是“事件”路由仍然没有显示在metadata@Shanehello服务是否显示在元数据中?是的,根据基本示例,定义为:public class HelloService:service的服务在元数据中显示为“hello”。这就是你的意思吗?@Shane是的。您的定义没有任何错误,因为您删除了响应DTO到请求DTO的路由属性。你能发布你的完整代码,包括HelloService吗?我已经将源代码上传到mediafire,如果这样做更容易的话
public AppHost() //Tell ServiceStack the name and where to find your web services
: base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly, typeof(EventService).Assembly)
{ // BREAKPOINT HERE, confirm the assembly is loaded
}