Error calling ServiceStack service with mvc4 Controller

Rafael Reyes

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

mythz

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error while calling AngularJS service function in controller?

From Dev

ServiceStack Service Calling Async Methods

From Dev

Calling service for factory in controller

From Dev

ServiceStack AutoQuery MVC controller

From Dev

ServiceStack AutoQuery MVC controller

From Dev

MVC4 + ServiceStack +....Glimpse?

From Dev

Calling secure ServiceStack service from within

From Dev

Calling View of different folder from Asp.net mvc4 controller

From Dev

Calling View of different folder from Asp.net mvc4 controller

From Dev

Jtable "listaction" url always calling action controller with old parameters value MVC4 Jquery

From Dev

Calling one ServiceStack 4 service from another with a file upload thrown in for fun

From Dev

how to call wcf service asynchronously from MVC4's controller

From Dev

MVC4 and ServiceStack session in Redis

From Dev

MVC4 and ServiceStack session in Redis

From Dev

ServiceStack: Can we Pass Data through a RequestFilterAttribute to the calling service

From Dev

error calling metod and/or action controller

From Dev

Calling another controller from service class in spring

From Dev

AngularJS - Calling a controller function from a service

From Dev

Calling a Service function from a Controller in AngularJS

From Dev

AngularJs calling service function from controller

From Dev

Angularjs calling a service function from the controller

From Dev

Error 101 when calling service

From Dev

Error 101 when calling service

From Dev

Method not calling of RestController of Spring mvc4

From Dev

MVC 4 controller error after adding a controller

From Dev

Post Enum to MVC4 Controller

From Dev

Ninject / MVC4 - integration test of a controller

From Dev

Set default controller in MVC4

From Dev

MVC4 / Mocking Controller.Request

Related Related

  1. 1

    Error while calling AngularJS service function in controller?

  2. 2

    ServiceStack Service Calling Async Methods

  3. 3

    Calling service for factory in controller

  4. 4

    ServiceStack AutoQuery MVC controller

  5. 5

    ServiceStack AutoQuery MVC controller

  6. 6

    MVC4 + ServiceStack +....Glimpse?

  7. 7

    Calling secure ServiceStack service from within

  8. 8

    Calling View of different folder from Asp.net mvc4 controller

  9. 9

    Calling View of different folder from Asp.net mvc4 controller

  10. 10

    Jtable "listaction" url always calling action controller with old parameters value MVC4 Jquery

  11. 11

    Calling one ServiceStack 4 service from another with a file upload thrown in for fun

  12. 12

    how to call wcf service asynchronously from MVC4's controller

  13. 13

    MVC4 and ServiceStack session in Redis

  14. 14

    MVC4 and ServiceStack session in Redis

  15. 15

    ServiceStack: Can we Pass Data through a RequestFilterAttribute to the calling service

  16. 16

    error calling metod and/or action controller

  17. 17

    Calling another controller from service class in spring

  18. 18

    AngularJS - Calling a controller function from a service

  19. 19

    Calling a Service function from a Controller in AngularJS

  20. 20

    AngularJs calling service function from controller

  21. 21

    Angularjs calling a service function from the controller

  22. 22

    Error 101 when calling service

  23. 23

    Error 101 when calling service

  24. 24

    Method not calling of RestController of Spring mvc4

  25. 25

    MVC 4 controller error after adding a controller

  26. 26

    Post Enum to MVC4 Controller

  27. 27

    Ninject / MVC4 - integration test of a controller

  28. 28

    Set default controller in MVC4

  29. 29

    MVC4 / Mocking Controller.Request

HotTag

Archive