Over the last month I have noticed an odd behavior where the wrong REST service is called with service stack.
Everything seems to be working and then when I add a new REST service, some how my working REST service is not working correctly (i.e. the wrong REST service is called).
I will explain how I have my datacontracts for the services which may explain why I am getting the problem.
I initially had:
[DataContract(Name = "MyService", Namespace = "")]
[RestService("/api/v1/dvbs/{ServiceName}", "GET", "application/xml")]
public class GetServiceDto
{
[DataMember(Name = "SessionId", Order = 1)]
public string SessionId { get; set; }
[DataMember(Name = "UserName", Order = 2)]
public string UserName { get; set; }
[DataMember(Name = "ServiceName", Order = 3)]
public string ServiceName { get; set; }
}
and as I was passing the same data (sessionid, username and servicename) in all my calls, any new service started as:
[DataContract(Name = "MyService", Namespace = "")]
[RestService("/api/v1/dvbs/{ServiceName}/tuners", "GET", "application/xml")]
public class GetTunersDto : GetServiceDto
{
}
As I mentioned this all worked fine but when I added a new service, when I called “api/v1/dvbs/{ServiceName} the service “api/v2/dvbs/{ServiceName}/tuners” would be called.
I removed the inherited relationship so that I have :
[DataContract(Name = "MyService", Namespace = "")]
[RestService("/api/v1/dvbs/{ServiceName}/tuners", "GET", "application/xml")]
public class GetDvbConfigTunersDto
{
[DataMember(Name = "SessionId", Order = 1)]
public string SessionId { get; set; }
[DataMember(Name = "UserName", Order = 2)]
public string UserName { get; set; }
[DataMember(Name = "ServiceName", Order = 3)]
public string ServiceName { get; set;}
}
and it works fine now.
What is strange is it was working fine and only when I added a new service at a later date that the wrong mapping incurred.
Any ideas?
You can only have 1 Request DTO per service and inheriting Request DTOs inherits the attribute routes which makes it GetTunersDto inherit the same routes as GetServiceDto which populates ServiceStack with conflicting and ambiguous routes.
The solution is to not use inheritance on DTOs (especially Request DTOs).