Thanks for your answer, this seems to be a valuable solution.
Do you know some code examples for that ?
Patrick
Actually, one of the best examples is inside the source code for ASP.NET AJAX itself, which can be downloadedhere.
The class to look for would be System.Web.Script.Services.ScriptHandlerFactory. You can also browse to locate where the ProfileService and AuthenticationService web services are handled (allowing access to authentication and ASP.NET profile information from Javascript on the client side).
You can also reflect on the assembly using Lutz Roeder's .NET Reflector tool.
Hi Patrick and GQAdonis,
I believe I am trying to achieve the exact same thing as you; I have a custom control in an assembly, I have an embedded javascript file that I want to call a webservice to update details on that control. Using the HttpHandler solution, is it possible to have an assembly with a class tagged [ScriptService], the relevant methods marked [WebMethod] (much like in ProfileService.cs in the ASP.NET AJAX source code) and have my embedded javascript call those WebMethods? I understand the principles behind the setup, intercepting the requests and so on, but I just can't quite figure out exactly how I refer to that web service from my embedded javascript?
Many thanks,
Josh
It seems that what the developers of ASP.NET Ajax did inside the ScriptManager code is "pre-generate" a proxy with the necessary call interface for the "hidden" web service and embed that into their embedded resource script.
The other solution would be to have the "URL" that, inside your handler factory, instantiates your WebService instance as part of IHttpHandlerFactory processing, added as a script reference dynamically to the ScriptManager during the OnInit() or OnPreRender() phases. This would generate a script tag in the resulting document that asks for your internal WebService handler and generates the script on the fly in the same manner that occurs when a .ASMX page is referenced.
Personally, I think the first choice is more optimal, since it skips the generation process at runtime altogether.
oooo
/\ Sorry my computer locked up temporarily I was, er, "testing" the keyboard and then it somehow post the above...!
Right, so back to normality, Iwastrying to write a reply after all! Anyone know how to edit posts on these forums if at all possible? I couldn't see a link anywhere...
Thanks for your help GQAdonis! I reflected out the code for System.Web.Extensions into a project, and I've been examining the code for the UpdatePanel, the ScriptManager and the ScriptHandlerFactory. As I see it the UpdatePanel does what I'm looking from from a dll (talks to the web service). I just can't see how it's all happening. A quick look at AssemblyInfo.cs doesn't seem to point me in any more a certain direction. How is the proxy web service being embedded, and how is it called? I had everything working from within a website, but moving out to an assembly has thrown me this one significant hurdle!
After some lengthy investigation into the matter, I think I follow what you mean a lot more. So what I should do is put my web service into a website, add a reference to it in the ScriptManager, then grab the proxy javascript that is generated when the page loads in the browser. Then all I need to do is embed that javascript in my assembly... nice! How exactly, though, do I go about embedding the web service and it's asmx file into the assembly and enable the proxy script to access it? The script uses "WebService.get_path()", which surely gets the path of a .asmx file that is embedded as a web resource with a dynamic URL?
Many thanks again,
Josh
Well, in this scenario, you never actually need a .ASMX file. That is what your custom "IHttpHandlerFactory" implementation is for. You designate some "virtual" URL for yourself to look for (or pattern) and use your IHttpHandlerFactory implementation (similar to the ScriptHandlerFactory in the ASP.NET Ajax source code) to instantiate your WebService-derived class (which is also an implementation of IHttpHandler) to handle the request to that URL. No .ASMX file needed.
The beauty is you get to define the URL pattern yourself (relative to the application root or wherever) that is used as the "path" for the proxy JavaScript code you embed as a resource. This ensures that they always match, and the developer never has to do anything other than reference your proxy.
I've written an HttpHandler before for giving users a more friendly url (i.e.www.domain.com/theirname instead ofwww.domain.com/user/profile.aspx?UserID=12 or something of the sorts), so I'm not completely foreign to it. But examining the ScriptHandlerFactory is leading me nowhere; surely I don't need to use an HttpHandler to retrieve the embedded javascript - I can just add the javascript as a link to the page header by resolving the url for the pre generated javascript usingGetWebResourceUrl()? What I need to do is grab the javascripts calls for the web service and reroute it to the correct place?
Ok so I'm a step closer. Basically I have my pregenerated proxy javascript, I've modified the path to be something unique for my HttpHandler. I've intercepted the call and from the url established what method has been called. The "GetHandler" method in the HttpHandler that is called returns an object of base type IHttpHandler. What exactly goes on in here to find the contents of the web service query and pass it on to my web service code? What gets returned from this method and how do I return the result of my web service to the client? Sorry if I'm asking lots of questions, I think I'm pretty much there.
Hi,
I have the same questions now , I would be happy to know how you solved this.
Thanks,
Tal
No comments:
Post a Comment