【问题标题】:Stumped SelfHosting WebApi Services难倒自托管 Web Api 服务
【发布时间】:2013-01-11 20:31:15
【问题描述】:

请帮忙,我正在尝试自托管一个 web api。

当同一个控制器托管在 Web 项目上,并通过 F5 在 dev 上运行时,一切正常。

但是现在我尝试自行托管,我得到 411 和 404。411,当我使用 Fiddler 连接时,404 当我尝试通过另一个库连接时。

这是应该托管服务的控制台应用程序:

class Program
{        
    static int portNumber;



    static void Main(string[] args)
    {
        portNumber = 8089;

        var config = new HttpSelfHostConfiguration(
            string.Format("http://localhost:{0}", portNumber));

        config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

        using (var server = new HttpSelfHostServer(config))
        {

            var test = new RetrieveGuidService().Execute(Unit.Instance);

            server.OpenAsync().Wait();

            Console.ReadLine();
        }
    }
}

这就是我的控制器的样子,它什么都不做,只是一个测试。

public class RetrieveGuidServiceController : ApiController
{
    public virtual Guid PostExecute(Unit request)
    {
        IQueryService<Unit,Guid> queryService = new RetrieveGuidService();
        return queryService.Execute(request);
    }
}

这就是我尝试通过 fiddler 访问它的方式:

当服务托管在 Web 项目上时,同样适用。我几乎完全按照本教程进行操作:asp.net WebApi self host tutorial,其中包括运行 nugget 脚本、添加依赖项等。

我还缺少什么?

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    411 是因为您没有放置 Content-Length 标头。即使您不发送内容,您也需要包含 Content-Length: 0。

    关于在正确的程序集中使用控制器,我的行为不一致。在某些项目中,它似乎在其他项目中不起作用。不知道我做错了什么。我有一个项目here,它使用不同程序集中的所有控制器同时执行 Web Host 和 Self-Host,它工作得很好。

    【讨论】:

    • 谢谢,这是问题的一部分,另一个是将课程放在同一个程序集中。
    【解决方案2】:

    似乎默认情况下,服务不会在包含服务的程序集中查找控制器。

    我认为这是一个遗漏,除非我正在阅读 wropng 的规格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多