有没有人有一个使用ServiceStack.net与asp.net网站,而不是Web应用程序的例子

时间:2013-01-30 15:47:14

标签: servicestack

我已经去了一个现有的asp.net网站(不属于我),他们要求添加一些网络服务。每个ServiceStack.net示例都适用于Web应用程序而非Web站点。

有人这样做过吗?我用Google搜索了很多,但没有找到任何东西。

我知道web.config部分完全不同,但我正在寻找有用的东西。

感谢。

2 个答案:

答案 0 :(得分:2)

Global.asax文件:

<%@ Application Language="C#" %>
<%@ Import Namespace="ServiceStack.ServiceHost" %>
<%@ Import Namespace="ServiceStack.ServiceInterface" %>

<script runat="server">

[Route("/hello")]
[Route("/hello/{Name}")]
public class Hello
{
   public string Name { get; set; }
}
public class HelloResponse
{
    public string Result { get; set; }
}
public class HelloService : Service
{
    public object Any(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}
public class HelloAppHost : ServiceStack.WebHost.Endpoints.AppHostBase
{
    //Tell Service Stack the name of your application and where to find your web services
    public HelloAppHost() : base("Russ", typeof(HelloService).Assembly) { }

    public override void Configure(Funq.Container container)
    {
        //register any dependencies your services use, e.g:
        //container.Register<ICacheClient>(new MemoryCacheClient());
    }
} 
void Application_Start(object sender, EventArgs e) 
{
    new HelloAppHost().Init();

}

void Application_End(object sender, EventArgs e) 
{
    //  Code that runs on application shutdown

}

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs

}

void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started

}

void Session_End(object sender, EventArgs e) 
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate mode
    // is set to InProc in the Web.config file. If session mode is set to StateServer 
    // or SQLServer, the event is not raised.

}

</script>

对于web.config文件,我发现它与Web应用程序示例相同。只需找到标签的相应部分,并输入它们所属的两条线:

<system.web>
   <httpHandlers>
       <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*"/>
   </httpHandlers>
</system.web>


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

并且对于引用我添加了nuget包并执行了cmd行调用:install-package ServiceStack。但你是bin应该有这些文件:

service stack bin files

答案 1 :(得分:1)

我刚试了一个新的ASP.NET网站,它确实有效。元数据页面未正确显示,但所有端点都有效。我想通过一点点修改你可以让元数据页面工作。

我通过nuget安装了ServiceStack,由于网站没有项目文件,引用将以不同的方式显示。所有dll都将直接加载到bin文件夹中。

然后添加最小所需的DTO,服务和AppHost代码,它将起作用。