1

我是 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)Service Stack 服务类(实际服务实现)

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
4

1 回答 1

1

It's hard to decipher what question is being asked here, I'm going to assume you want to know how to access the header information in your services.

You should first look at using ServiceStack's New API for new services. When you inherit from Service you can access the HTTP Headers with:

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

If you want to continue to use the Old API (e.g. IService<T>) then you want to implement the IRequiresRequestContext interface to get ServiceStack to inject the RequestContext into your service. Read the wiki docs for more info on this.

于 2013-06-28T05:41:44.403 回答