How can I use reflection during Application_BeginRequest to instantitate a web service method?

StackOverflow https://stackoverflow.com/questions/21770091

سؤال

During Application_BeginRequest any Global.asax or System.Web.Services.WebService event, is there a reliable way to instantiate or examine the intended web service method using reflection? I need to determine what the value of attributes are on the web service being called.

I'm pretty sure the neccessary information is available in HttpContext.Current.Request to do this, but I'm not familiar enough with reflection syntax.

GLOBAL ASAX

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim InboundRequest As HttpRequest = HttpContext.Current.Request

    'ASSUMING REQUEST IS FOR DemoService.asmx/ExampleMethod'

    'NEED CODE HERE TO DETERMINE '
    'WHAT THE VALUE OF THE "ScriptMethod" ATTRIBUTE IS'
End Sub

WEB SERVICE

<ToolboxItem(False), ScriptService()> _
Public Class DemoService
    Inherits System.Web.Services.WebService

    <WebMethod(), ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function ExampleMethod()
        Return "Hello World"
    End Function
End Class

VB.Net or C# answers are welcome, I can convert as neccessary

لا يوجد حل صحيح

نصائح أخرى

You could, but I think it'd be better to do whatever you are planning to do in the PreRequestHandlerExecute event as you'll have the web service object instance created by asp.net already. Just remember that this event is fired for all types of handlers (pages, web services, generic, etc).

If you do want to go this route on your own, then you'd just need to do Activator.CreateInstance(type) where type is the web service type.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top