4
votes

I'm trying to use the new API approach for ServiceStack and I'm building a test console application to host it. So far I have the route instantiating the request DTO, but before the request reaches my service's Any method, I get this exception:

Error Code NullReferenceException 
Message Object reference not set to an instance of an object. 
Stack Trace at ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(Type requestDtoType) at 
ServiceStack.WebHost.Endpoints.EndpointHost.ApplyRequestFilters(IHttpRequest httpReq, IHttpResponse httpRes, Object requestDto) at 
ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)

Below is my test Service using IReturn and Service (at this point I'm only trying to return hard-coded results to see it working)

[DataContract]
public class AllAccounts : IReturn<List<Account>>
{
    public AllAccounts()
    {

    }
}
[DataContract]
public class AccountTest : IReturn<string>
{
    public AccountTest()
    {
        this.Id = 4;
    }
    [DataMember]
    public int Id { get; set; }
}

public class AccountService : Service
{
    public AccountService()
    {
    }

    public object Any(AccountTest test)
    {
        return "hello";
    }
    public object Any(AllAccounts request)
    {
        var ret = new List<Account> {new Account() {Id = 3}};
        return ret;
    }
}

All ServiceStack references come from NuGet. I get the same error with either route. Any suggestions?

1

1 Answers

2
votes

It might help to see your AppHost code and the code in your Configure() method. Nothing you provided in the code above stands out. Below is how I would set up a simple Console app using the code/classes you have provided.

Initialize and Start the ServiceStack AppHost

class Program
{
    static void Main(string[] args)
    {
        var appHost = new AppHost();
        appHost.Init();
        appHost.Start("http://*:1337/");
        System.Console.WriteLine("Listening on http://localhost:1337/ ...");
        System.Console.ReadLine();
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
    }
}

Inherit from AppHostHttpListenerBase and Configure (not configuring anything for this example)

public class AppHost : AppHostHttpListenerBase
{
    public AppHost() : base("Test Console", typeof(AppHost).Assembly) { }

    public override void Configure(Funq.Container container)
    {
    }

}

Dto/Request classes

public class Account
{
    public int Id { get; set;  }
}

[Route("/AllAccounts")]
[DataContract]
public class AllAccounts : IReturn<List<Account>>
{
    public AllAccounts()
    {

    }
}

[Route("/AccountTest")]
[DataContract]
public class AccountTest : IReturn<string>
{
    public AccountTest()
    {
        this.Id = 4;
    }
    [DataMember]
    public int Id { get; set; }
}

Service code to handle your requests - URLS: localhost:1337/AllAccounts & localhost:1337/AccountTest

public class AccountService : Service
{
    public AccountService()
    {
    }

    public object Any(AccountTest test)
    {
        return "hello";
    }
    public object Any(AllAccounts request)
    {
        var ret = new List<Account> { new Account() { Id = 3 } };
        return ret;
    }
}