如何访问服务堆栈服务实现/方法中的头信息

时间:2013-06-28 05:26:45

标签: servicestack

我是servicestack.net的新手,我正在努力访问我的方法中的头信息。我附上了我正在使用的代码。它在vb.net

服务堆栈类 a)服务栈请求类

Public Class LeaveManagementDashboardRequest

    Public Property ClientID As String
    Public Property DateFormatID As String
    Public Property UserID As String
    Public Property NOOFROWS As String

End Class

b)服务堆栈响应类

Public Class LeaveManagementDashboardResponse
    Public Property data As String
End Class

c)服务堆栈服务类(实际服务实施)

Public Class LeaveManagementDashboardService
    Implements IService(Of LeaveManagementDashboardRequest)
    Private sqlCon As New SqlConnection
    Public Function Execute(ByVal request As LeaveManagementDashboardRequest) As Object Implements ServiceStack.ServiceHost.IService(Of LeaveManagementDashboardRequest).Execute
        Dim ds As New DataSet
        If sqlcon.State = ConnectionState.Closed Then
            Common.OpenConnection(sqlCon)
        End If

        Dim ClientID As String = request.ClientID
        Dim UserID As String = request.UserID
        Dim DataFormatID As String = request.DateFormatID
        Dim NOOFROWS As String = request.NOOFROWS

        Dim sqlcmd As New SqlCommand("abcdefg", sqlcon)
        sqlcmd.CommandType = CommandType.StoredProcedure

        sqlcmd.Parameters.Add(New SqlParameter("@UserID", SqlDbType.Int, 0))
        sqlcmd.Parameters.Add(New SqlParameter("@DateFormatID", SqlDbType.TinyInt, 0))
        sqlcmd.Parameters.Add(New SqlParameter("@Count", SqlDbType.SmallInt, 0))
        sqlcmd.Parameters.Add(New SqlParameter("@ClientID", SqlDbType.Int, 0))

        sqlcmd.Parameters(0).Value = UserID
        sqlcmd.Parameters(1).Value = DataFormatID
        sqlcmd.Parameters(2).Value = NOOFROWS
        sqlcmd.Parameters(3).Value = ClientID

        Dim dsResult As New DataSet
        Dim sqlda As New SqlDataAdapter(sqlcmd)
        sqlda.Fill(dsResult)

        Dim obj As String = Common.GetJson(dsResult.Tables(0))
        ' obj.countries = lstCountries

        sqlcon.Close()
        sqlcon.Dispose()
        Return New LeaveManagementDashboardResponse With {.data = obj}

        Return dsResult
    End Function
End Class

路线在Global.aspx.cs文件中定义,如下所示

Public Class _Global
    Inherits System.Web.HttpApplication

    Public Class HelloAppHost
        Inherits AppHostBase

        Public Sub New()
            MyBase.New("Plant Best Services", GetType(HelloAppHost).Assembly)
        End Sub

        Public Overrides Sub Configure(ByVal container As Container)
Routes.Add(Of LeaveManagementDashboardRequest)("/pml/Dashboard/LeavesRequests")
        End Sub
    End Class
    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Dim apphost = New HelloAppHost()
        apphost.Init()
    End Sub

1 个答案:

答案 0 :(得分:1)

很难解释这里提出的问题,我假设您想知道如何访问服务中的标题信息。

首先应该使用ServiceStack's New API来获取新服务。从Service继承后,您可以使用以下命令访问HTTP标头:

public class MyService : Service
{
    public LeaveManagementDashboardResponse Any(LeaveManagementDashboard request)
    {
        var httpHeader = base.Request.Headers["headerName"];
    }
}

如果您想继续使用旧API(例如IService<T>),那么您希望实现IRequiresRequestContext接口以使ServiceStack将RequestContext注入您的服务。 Read the wiki docs for more info on this