Rafael Reyes
Rafael Reyes

Reputation: 2655

Error calling ServiceStack service with mvc4 Controller

I'm trying to figure out how to call a service from an asp mvc4 controller, but I just can't. I already set up the services but when I'm trying to call from my controller method it displays this :

http://s10.postimg.org/5ojcryn7t/error_1.png

Here's my code:

Global.asax.cs

namespace mvc4_servicestack
{
// Nota: para obtener instrucciones sobre cómo habilitar el modo clásico de IIS6 o IIS7, 
// visite http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {

        public class AppHost : AppHostBase
        {
            public AppHost()
                : base("Nombre del Host de los WebService", typeof(SS_WebServices.StatusService).Assembly)
            { }

            public override void Configure(Funq.Container container)
            {

            }



        }

        protected void Application_Start()
        {

            new AppHost().Init();

            AreaRegistration.RegisterAllAreas();

           // WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();


        }
    }
}

SS_WebServices.cs

namespace mvc4_servicestack { public class SS_WebServices {

    //Request DTO Objet
    [ServiceStack.ServiceHost.Route("/Home/About/statuss")]
    [ServiceStack.ServiceHost.Route("/Home/About/statuss/{Name}")]
    public class StatusQuery : ServiceStack.ServiceHost.IReturn<StatusResponse>
    {
        public string Name { get; set; }
        public int Id { get; set; }

    }

    //Response DTO Object
    public class StatusResponse
    {
        public string Result { get; set; }
    }


    //Service
    public class StatusService : ServiceStack.ServiceInterface.Service
    {
        //Implement teh Method VERB (POST,GET,PUT,DELETE) OR Any for ALL VERBS
        public object Any(StatusQuery request)
        {
            //Looks strange when the name is null so we replace with a generic name.
            var name = request.Name ?? "John Doe";
            return new StatusResponse { Result = "Hello, " + name };
        }
    }


}

}

I've been reading the post Should ServiceStack be the service layer in an MVC application or should it call the service layer?

But Still I can't understand things like : WHY Register returns an Interface instead a GreeterResponse? container.Register(c => new Greeter());

I really hope you guys can help me...!

Upvotes: 3

Views: 311

Answers (1)

mythz
mythz

Reputation: 143389

Since ServiceStack is hosted in the same AppDomain as MVC, you don't need to use a ServiceClient and go through HTTP to access ServiceStack Services, instead you can just resolve and call the Service normally, E.g:

public HelloController : ServiceStackController 
{
    public void Index(string name) 
    {
        using (var svc = base.ResolveService<HelloService>())
        {
           ViewBag.GreetResult = svc.Get(name).Result;
           return View();
        }
    }        
}

Outside of a Controller a ServiceStack Service can be resolved via HostContext.ResolveService<T>()

See the MVC Integration docs for more info.

But Still I can't understand things like : WHY Register returns an Interface instead a GreeterResponse? container.Register(c => new Greeter());

Greeter is just a normal C# dependency (i.e. it's not a ServiceStack Service). The container isn't returning IGreeter here, instead it's saying to "Register the Greeter implemenation against the IGreeter interface":

container.Register<IGreeter>(c => new Greeter());

If you registered it without, it will just be registered against the concrete type, e.g. these are both equivalent:

container.Register(c => new Greeter());
container.Register<Greeter>(c => new Greeter());

Which means you would need to specify the concrete dependency to get it auto-wired, e.g:

public class MyService : Service 
{
    public Greeter Greeter { get; set; }
}

Instead of being able to use:

    public IGreeter Greeter { get; set; }

Which is much easier to test given that it's a mockable interface.

Upvotes: 3

Related Questions