0
votes

I am new to Servicestack and trying to implement it for my current project.Now I have my MVC.NET application deployed on one server (say http://server1:8080) and servicestack service deployed on different one (say http://server2:9080).

I have already gone through

https://github.com/ServiceStack/ServiceStack/wiki/Mvc-integration

but i am not able to understand how to change that to call service from http://server2:9080

how to call service from my MVC controller?

    Service code is as following  

// APPHOST.cs

    public class AppHost : AppHostBase
        {
            public AppHost() : base("Flight app host",
                typeof(flightService).Assembly) { }

            public override void Configure(Container container)
            {

            }
        }

// Request DTO

    [Route("/flights/","POST")]
        [Route("/flights/{departure}/{arrival}","POST")]
        public class TravelServiceRequest : IReturn<TravelServiceResponce>
            {
                public string departure { get; set; }
                public string arrival { get; set; }

            }

// Response DTO

    public class TravelServiceResponce
        {

            public string departure { get; set; }
            public string arrival { get; set; }
            public string airline { get; set; }
            public decimal fare { get; set; }
            public DateTime arrivalTime { get; set; }
            public DateTime departureTime { get; set; }
        }

// service

public class flightService:Service
    {

        public object Post(TravelServiceRequest request)
        {
            var response = new TravelServiceResponce
            {

                departure =request.departure,
                arrival =request.arrival,
                airline="jet airways",
                fare =677,
                arrivalTime =new DateTime(2014,12,12,5,6,2),
                departureTime = new DateTime(2014,11,12,5,6,2)    

            };

            return response;
        }

    }

  // Gloable.asax.cs file 

public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            var appHost = new AppHost();
            appHost.Init();
        }
}

In my MVC apllication

public class HomeController : Controller
    {
        public ActionResult Index()
        {

            //call service from here 

            return View();
        }
}

I have c# code to call this service but i am not sure if this is the best way to use in MVC controller

           var client = new JsonServiceClient("http://server2:9080/");

            var responce = client.Post(new TravelServiceRequest
            {
                departure = departure,
                arrival = arrival

            });

Please help me with best way to call remote service in MVC.

1

1 Answers

2
votes

Calling ServiceStack Services out-of-process

If ServiceStack and MVC are not hosted together in the same Web Application then you would just access the ServiceStack Service as you would from any .NET Service Client using the ServiceStack Server DTO's and a .NET Service Client, e.g:

var response = client.Post(new TravelServiceRequest { ... });

Another alternative to sharing the Server DTO .dll is to use the VS.NET Integration offered by Add ServiceStack Reference which lets you generate Server DTO's from a remote url.

ServiceStack + MVC In Process

If ServiceStack and MVC are hosted together in the same AppDomain refer to the ServiceStack Integration with MVC which allow your Controllers can access most of ServiceStack functionality by inheriting from ServiceStackController.

You can then Execute a ServiceStack Service in-process in your MVC Controllers with:

var response = base.Execute(new TravelServiceRequest { ... });

This is equivalent to resolving the ServiceStack Service from the IOC and calling it directly which you can do instead, e.g:

using (var service = base.ResolveService<TravelServices>())
{
    var response = service.Post(new TravelServiceRequest { ... });
}