1

I have created a server user control that I want it to be able to use a WebMethod. If the web method is in my main application (as an ASMX file), it works fine. The problem is that I want to include this method in the same project library as the user control, so that I can distribute the DLL as a stand alone project. Because the project is a class library, I had to make the web service a VB file, instead of .ASMX. When I try to call the web method in my .VB file, nothing seems to happen (no errors but my break point on my webmthod is never reached) Can this be accomplished? The following is an example of how I created my control:

Web Method, in myClass.VB:

 <System.Web.Script.Services.ScriptService()> _
 <WebService(Namespace:="http://tempuri.org/")> _
 <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _     
 Public Class myClass
     Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function TestMethod(ByVal prefixText As String) As String
    return "Hello World"
end Function

and my Server Controls is set up like this, in miniActiveDirectorySearch.VB:

Public Class miniActiveDirectorySearch
 Inherits WebControl
   Private Sub attachWebResources()  
     ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "myScripts.js")
     ScriptManager.RegisterClientScriptResource(Me, Me.GetType, "jquery-1.4.1.min.js")
   End Sub
    Protected Overrides Sub CreateChildControls()
        createDynamicControls()
        MyBase.CreateChildControls()
    End Sub
    Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
        attachWebResources()
        MyBase.OnInit(e)
    End Sub
    Private Sub createDynamicControls()
        Controls.Clear()

        Try
            tblMainLayout = buildMaintable() 'builds out the control
            'to save space, I removed how this works.  But, it creates a textbox that
            'has a onKeyPress Event.  When the user hits return, it calls the jQuery
            'function serviceCall
            Controls.Add(tblMainLayout)
        Catch ex As Exception
            Throw New ApplicationException("Exception Occurred", ex.InnerException)
        End Try
    End Sub
 end Class

and my JQuery Method, found in myScripts.js:

function serviceCall(getText, tbId, divId, bgColor) {
   $.ajax({
    type: "POST",
    url: 'myClass.asmx/TestMethod',
    data: '{"prefixText":"'some test'"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
       alert('Success')           
    },
    error: function (e) {
         alert('Error')   
    }
});

I have created the service class and included it within the same project as my user control but the service is never called. On the other hand, if i place the service in an ASMX file in my web project, it will reach the web method just fine.

Any help would be great

thanks jason

jason
  • 3,821
  • 10
  • 63
  • 120

1 Answers1

0

Sorry, but if you use the old ASMX technology, then you're stuck using ASMX files. On the other hand, you can do this with WCF.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • How would this be accomplished in WCF? Is this something that would require changes to IIS? I'm kind of locked in on some things. – jason Apr 23 '13 at 15:32
  • No changes to IIS. WCF supports self-hosting. I do this right now - I have a WCF JSON service hosted in part of my web application. – John Saunders Apr 23 '13 at 17:51
  • Yeah but my code is not in a web application. Can I include the WCF in a class library and have a server control in that library consume it? – jason Apr 23 '13 at 18:01
  • Oh, I forgot. Yes, you can do that. You would need to have a static constructor for your control class which would host host the WCF service. See "[Hosting in a Managed Application](http://msdn.microsoft.com/en-us/library/ms733765.aspx)" – John Saunders Apr 23 '13 at 18:04