【问题标题】:Error calling ServiceStack service with mvc4 Controller使用 mvc4 控制器调用 ServiceStack 服务时出错
【发布时间】:2013-10-08 17:40:51
【问题描述】:

我试图弄清楚如何从 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?

但我仍然无法理解以下内容: 为什么注册返回一个接口而不是一个 GreeterResponse? container.Register(c => new Greeter());

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

【问题讨论】:

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


    【解决方案1】:

    由于 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&lt;T&gt;() 解析 ServiceStack 服务

    请参阅MVC Integration docs 了解更多信息。

    但我仍然无法理解以下内容:为什么注册返回一个接口而不是一个 GreeterResponse? container.Register(c => 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; }
    

    鉴于它是一个可模拟的界面,因此更容易测试。

    【讨论】:

    • 我不确定你是否看过我发布的所有代码......或者我很困惑,因为在我的 StatusService 中我没有方法 GET 而是我有一个方法 Any(StatusQuery 请求) 返回一个 StatusResponse 对象。所以我实在看不懂你贴的代码。
    • 这些是我尝试该代码时遇到的错误 [link]s24.postimg.org/500b3m7it/error2.png [link]s7.postimg.org/a8lx5y48r/error3.png @mythz
    • @user1629278 你调用你自己的类就像一个普通的 C# 方法调用 - 你只在你的服务中定义了 Any() 所以调用它(如果这是你想做的)。
    • 我刚刚做了,但在 ResolveService 上仍然出现错误?为什么? [链接]s13.postimg.org/e1g9wlvsn/error4.png@mythz
    • @user1629278 您只需要提供HttpContext.Current。查看更新的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多