IIS中的ServiceStack部署(404例外)

时间:2014-01-10 22:59:25

标签: c# asp.net .net iis servicestack

我在Stackoverflow上看了很多关于ServiceStack的问题并且实际上用完了选项。花了很多时间尝试了很多选项,但却无法在IIS中运行我的ServiceStack服务。

我在名为api的默认网站下有一个虚拟目录,指向它的物理位置是ServiceStack程序集的bin目录。

为了测试,我在bin文件夹中放了一个index.htm。当我导航到localhost/api时,我从bin文件夹中获取index.htm的内容。

但是,正如您在下面的代码中看到的那样,我通过JSONServiceClient调用ServiceStack服务会导致404异常。我不确定我错过了什么。

非常感谢。

  • 服务堆栈版本:3.9.69.0
  • IIS 8.0版

using System.Configuration;
using ServiceStack.OrmLite;
using ServiceStack.OrmLite.SqlServer;

//  logging
using ServiceStack.Logging;

//  Service Interface project
public class xxxService   : Service
{
    public List<xxxResponse> Get(xxxQuery xxxQuery) 
}

[Route("/xxxFeature/{xxxSerialNo}/{xxxVersion}")]
public class xxxQuery : IReturn<List<xxxResponse>>
{
    public string xxxSerialNo { get; set; }
    public string xxxVersion { get; set; }
    public string xxxId { get; set; }
    public string xxxName { get; set; }
}

public class xxxResponse
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Version { get; set; }
    public string Size { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}

的Web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <location path="api"> 
    <system.web>
      <httpHandlers>
        <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
      </httpHandlers>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
    <!-- Required for IIS7 -->
    <system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
      <validation validateIntegratedModeConfiguration="false" />
      <handlers>
        <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
      </handlers>
    </system.webServer>
  </location>
  <system.webServer>
    <directoryBrowse enabled="false" />
  </system.webServer>
</configuration>

的Global.asax.cs

public class Global : System.Web.HttpApplication
{
    public class xxxServiceAppHost : AppHostBase
    {
        public xxxServiceAppHost() : base("xxx Services", typeof(xxxService).Assembly)
        {
            ServiceStack.Logging.LogManager.LogFactory = new Log4NetFactory(true);
            Log4NetUtils.ConfigureLog4Net(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ServerDB"]].ConnectionString);
        }

        public override void Configure(Funq.Container container)
        {
            container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ServerDB"]].ConnectionString, SqlServerDialect.Provider));
            SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api" });
        }
    }

还尝试使用routes.ignore注释为避免与ASP.NET MVC发生冲突,请在Global.asax中添加忽略规则。 RegisterRoutes方法,例如:routes.IgnoreRoute ("api/{*pathInfo}");

    public void RegisterRoutes(RouteCollection routes)
    {
        routes.Ignore("api/{*pathInfo}");
    }

    protected void Application_Start(object sender, EventArgs e)
    {
        new xxxServiceAppHost().Init();
    }
}

客户端调用。我还尝试使用..../api/api,因为我在IIS上的vdir是api

try
{
    xxxServiceClient = new JsonServiceClient("http://111.16.11.111/api");
    List<xxxResponse> xxxResponses = xxxServiceClient.Get(new xxxQuery { xxxSerialNo = "22222", xxxVersion = "0.0" });
}
catch (WebServiceException excp)
{
    throw excp;
}

1 个答案:

答案 0 :(得分:4)

在我看来,您可以尝试使用web.config进行一些尝试。您不需要在服务器上拥有虚拟目录。根据您使用的IIS版本,您可能仍需要httpHandlers和处理程序配置部分。我看到你在位置路径=“api”中嵌套ServiceStack的配置设置。这可能对您所需的安全要求有意义,以及为什么您有一个“api”虚拟目录。您可以尝试不使用该位置元素。

尝试以下 :删除location元素并将设置与其他配置部分(system.web ...等)结合使用,删除httpHandlers部分,保留处理程序部分,并将处理程序配置更改为具有“api *。

的路径

这会将网址映射到服务,因此当您转到localhost:12345 / api / metadata时,您应该会看到您的服务。如果您看不到元数据页面,则您知道某些内容不对。

独立于web.config更改,您的服务代码存在问题。你的代码似乎有一些不合适的地方。您的请求对象(xxxQuery)应该是一个带有路由属性的简单POCO。 Get服务需要将该对象作为其参数。如果要返回该属性,响应应该实现IHasResponseStatus。

<handlers>
    <add path="api*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true"/>
</handlers>

//  Service Interface project
public class xxxService   : Service
{
    public xxxResponse Get(xxxQuery xxxQuery)
    { 
        //return object of type xxxResponse after doing some work to get data
        return new xxxResponse();
    } 
}

[Route("/xxxFeature/{xxxSerialNo}/{xxxVersion}")]
public class xxxQuery
{
    public string xxxSerialNo { get; set; }
    public string xxxVersion { get; set; }
    public string xxxId { get; set; }
    public string xxxName { get; set; }
}

public class xxxResponse : IHasResponseStatus
{
    public xxxResponse()
    {
        // new up properties in the constructor to prevent null reference issues in the client
        ResponseStatus  = new ResponseStatus();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Version { get; set; }
    public string Size { get; set; }
    public ResponseStatus ResponseStatus { get; set; }
}