1
votes

I've been struggling with this issue now for a few hours. I have a ServiceStack Service where only GET-Requests are working.

Those are my routes:

[Route("/test", "POST")]
public class TestRequest : IReturnVoid { }

[Route("/test2", "GET")]
public class TestRequest2 : IReturnVoid { }

And my service:

public void Post(TestRequest request)
{
    Console.WriteLine("Doesnt Work!");
}

public void Get(TestRequest2 request)
{
    Console.WriteLine("Works!");
}

Its pretty simple. I do not have any RequestFilters configured. This is my Configure method:

public AppHost() : base(nameof(LogService), typeof(ServiceInterface.Services.LogService).Assembly) { }

public override void Configure(Container container)
{
    //container.Register(c => new LoggingService()).ReusedWithin(ReuseScope.Container);
    container.Register<IUserAuthRepository>(c => new LogUserAuthRepository());
    container.Register<ICacheClient>(new MemoryCacheClient());

    InitLogContext();

    //Add Plugins
    var authFeature = new AuthFeature
    (
        () => new AuthUserSession(),
        new IAuthProvider[] 
        {
            new CredentialsAuthProvider()
        }
    )
    { IncludeAssignRoleServices = false };

    base.Plugins.Add(authFeature);
    base.Plugins.Add(new ServiceStack.ProtoBuf.ProtoBufFormat());

    //Disable Cookies
    SetConfig(new HostConfig
    {
        AllowSessionCookies = false
    });

    base.GlobalResponseFilters.Add((req, res, obj) =>
    {
        // Handle void responses -> http://stackoverflow.com/questions/25260549/how-to-return-http-204-response-on-successful-delete-with-servicestack
        if (obj == null && res.StatusCode == 200)
        {
            res.StatusCode = (int)HttpStatusCode.NoContent;
            res.StatusDescription = "No Content";
        }
    });
}

I am sorry that I cant provide more information. I have already made my whole code as simple as possible. Creating another Service in the same assembly did not workout -> same result.

Does anybody know why this happens and how I can solve the issue? Thanks$

EDIT: Using this:

[Route("/test")]
public class TestRequest : IReturnVoid, IPost { }

Instead of the above code gives me the following exception:

NotImplementedException: Could not find method named Get(TestRequest) or Any(TestRequest) on Service LogService

StackTrace: at ServiceStack.Host.ServiceExec 1.Execute(IRequest request, Object instance, Object requestDto, String requestName) at ServiceStack.Host.ServiceRequestExec 2.Execute(IRequest requestContext, Object instance, Object request) at ServiceStack.Host.ServiceController.ManagedServiceExec(ServiceExecFn serviceExec, IService service, IRequest request, Object requestDto) at ServiceStack.Host.ServiceController.<>c__DisplayClass37_0.b__0(IRequest req, Object dto) at ServiceStack.Host.ServiceController.ExecuteAsync(Object requestDto, IRequest req) at ServiceStack.Host.RestHandler.ProcessRequestAsync(IRequest req, IResponse httpRes, String operationName)

1
How are you testing? What client code? And when you say "not working", how so? Do you get an error? Does the computer crash? Does the computer walk out in disgust? Does the gestapo come to seize your computer?jklemmack

1 Answers

1
votes

This Exception:

NotImplementedException: Could not find method named Get(TestRequest) or Any(TestRequest) on Service LogService

Is because you tried to use a GET request to call the TestRequest Service which you've only declared a Post implementation for:

public void Post(TestRequest request)
{
    Console.WriteLine("Doesnt Work!");
}

You could only use a GET Request to call the TestRequest2 Service:

public void Get(TestRequest2 request)
{
    Console.WriteLine("Works!");
}

Which is only available on:

[Route("/test2", "GET")]
public class TestRequest2 : IReturnVoid { }

Although GET Requests are supposed to return something as HTTP GET Requests should be
"safe" and idempotent.

Anyway it looks like you want to use the same Request DTO with the same routes in which case you want to use the same Request DTO for each HTTP Method you want to support, e.g:

[Route("/test")]
public class TestRequest : IReturn<string> { }

Service implementation:

public string Post(TestRequest request) => "POST!";

public string Get(TestRequest request) => "GET!";

Which you can call both with a GET /test and a POST /test HTTP Request.

Otherwise you can use different Request DTOs with the same route as long as you specify which HTTP Verb the route should match:

[Route("/test", "POST")]
public class PostRequest : IReturnVoid { }

[Route("/test", "GET")]
public class GetRequest : IReturn<string> { }

And have the Service implement their respective Request DTOs:

public void Post(PostRequest request) => Console.WriteLine("POST!");

public string Get(GetRequest request) => "GET!";

This will let you call different services with the same route:

GET  /test => Get(GetRequest)
POST /test => Post(PostRequest)