3

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...!

Community
  • 1
  • 1
Rafael Reyes
  • 2,615
  • 8
  • 34
  • 51

1 Answers1

3

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • I'm not sure if you watch all the code I post....or I'm confused because in my StatusService I do not have a method GET instead I have a method Any(StatusQuery request) wich return a StatusResponse object. So I really can't understand the code you paste. – Rafael Reyes Oct 08 '13 at 18:46
  • These are the errors I get when I try that code [link]http://s24.postimg.org/500b3m7it/error2.png [link]http://s7.postimg.org/a8lx5y48r/error3.png @mythz – Rafael Reyes Oct 08 '13 at 18:58
  • @user1629278 You're calling your own class just like a normal C# method call - you only have `Any()` defined in your service so call that instead (if that's what you want to do). – mythz Oct 08 '13 at 19:03
  • I just did it but still get the error at the ResolveService? Why? [link]http://s13.postimg.org/e1g9wlvsn/error4.png @mythz – Rafael Reyes Oct 08 '13 at 19:17
  • @user1629278 You just need to provide `HttpContext.Current`. See updated answer. – mythz Oct 08 '13 at 19:40
  • In MVC4, `HttpContext.Current` does not resolve. You need `System.Web.HttpContext.Current` – Rosdi Kasim Nov 18 '13 at 02:52