使用mvc4 Controller调用ServiceStack服务时出错

时间:2013-10-08 17:40:51

标签: c# asp.net-mvc-4 controller servicestack

我试图弄清楚如何从asp mvc4控制器调用服务,但我不能。我已经设置了服务但是当我尝试从我的控制器方法调用它时会显示:

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

这是我的代码:

的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

命名空间mvc4_servicestack {     公共类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 };
        }
    }


}

}

我一直在阅读帖子Should ServiceStack be the service layer in an MVC application or should it call the service layer?

但我仍然无法理解如下: 为什么Register返回一个接口而不是GreeterResponse? container.Register(c =&gt; new Greeter());

我真的希望你们能帮助我......!

1 个答案:

答案 0 :(得分:3)

由于ServiceStack与MVC托管在同一个AppDomain中,因此您无需使用ServiceClient并通过HTTP访问ServiceStack服务,而只需resolve and call the Service normally,例如:

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

在Controller之外,可以通过HostContext.ResolveService<T>()

解决ServiceStack服务

有关详细信息,请参阅MVC Integration docs

  

但是我仍然无法理解:WHY Register返回一个接口而不是GreeterResponse? container.Register(c =&gt; new Greeter());

Greeter只是一个普通的C#依赖(即它不是ServiceStack服务)。容器没有在这里返回IGreeter,而是说“向IGreeter接口注册Greeter实现”:

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

如果你没有注册,它只会根据具体类型进行注册,例如:这些都是等价的:

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

这意味着您需要指定具体的依赖关系以使其自动连线,例如:

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

而不是能够使用:

    public IGreeter Greeter { get; set; }

考虑到它是一个可模拟的界面,哪个更容易测试。