簡體   English   中英

如何從部署在另一台服務器上的MVC4.net應用程序調用部署在遠程服務器上的Servicestack服務?

[英]How To Call Servicestack service deployed on remote server from MVC4.net application deployed on another server?

我是Servicestack的新手,正在嘗試為當前項目實施它。現在,我將MVC.NET應用程序部署在一台服務器上(例如http:// server1:8080 ),並將servicestack服務部署在另一台服務器上(例如http:// server2:9080 )。

我已經經歷了

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

但我不明白如何將其更改為從http:// server2:9080呼叫服務

如何從我的MVC控制器調用服務?

    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)
            {

            }
        }

//請求DTO

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

            }

//回應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; }
        }

//服務

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();
        }
}

在我的MVC應用中

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

            //call service from here 

            return View();
        }
}

我有C#代碼來調用此服務,但是我不確定這是否是在MVC控制器中使用的最佳方法

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

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

            });

請以最好的方式幫助我在MVC中調用遠程服務。

進程外調用ServiceStack Services

如果ServiceStack和MVC不在同一Web應用程序中托管在一起,則您可以使用ServiceStack Server DTO和.NET Service Client訪問與任何.NET Service Client一樣的ServiceStack Service ,例如:

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

共享Server DTO .dll的另一種方法是使用“ 添加ServiceStack參考”提供的VS.NET集成,它使您可以從遠程URL生成Server DTO。

正在處理ServiceStack + MVC

如果將ServiceStack和MVC一起托管在同一AppDomain中,請參閱ServiceStack與MVC的集成,它允許您的控制器可以通過繼承ServiceStackController來訪問大多數ServiceStack功能。

然后,您可以在MVC控制器中通過以下方式在過程中執行ServiceStack服務:

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

這等效於從IOC解析ServiceStack服務並直接調用它,而您可以這樣做,例如:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM