创建没有ASMX文件的Web服务?

时间:2022-12-04 08:06:56

I have written an ASP.NET composite control which includes some Javascript which communicates with a web service.

我编写了一个ASP.NET复合控件,其中包含一些与Web服务通信的Javascript。

I have packaged the classes for the control and the service into a DLL to make it nice and easy for people to use it in other projects.

我已经将控件和服务的类打包成了一个DLL,使人们可以在其他项目中使用它。

The problem I'm having is that as well as referencing the DLL in their project, the consumer of my control must also include a .ASMX file for the web service. Whilst it isn't a complicated file (just a one-liner which refers to the class in the DLL), I would like to avoid having it if I can.

我遇到的问题是,除了在他们的项目中引用DLL之外,我的控件的使用者还必须包含用于Web服务的.ASMX文件。虽然它不是一个复杂的文件(只是一个单行程序,指的是DLL中的类),但如果可以,我想避免使用它。

Is there any way to avoid having to have the .ASMX file?

有没有办法避免必须有.ASMX文件?

  • Can I register the service with the web server in Application_Start?
  • 我可以在Application_Start中使用Web服务器注册服务吗?

  • Can I make a web.config change to reference it somehow?
  • 我可以进行web.config更改以某种方式引用它吗?

All suggestions gratefully received!

感谢所有建议!

UPDATE: The article linked to in John Sheehan's response (below) does work - but not if you want to call the web service using AJAX. Does anybody know of an AJAX friendly version?

更新:在John Sheehan的回复(下面)中链接的文章确实有效 - 但如果你想使用AJAX调用Web服务则不行。有人知道AJAX友好版吗?

5 个解决方案

#1


5  

Try something like this. I don't know if it will work though. I got this idea from ELMAH, which creates a handler for a page that doesn't physically exist and then serves it up from the assembly.

尝试这样的事情。我不知道它是否会起作用。我从ELMAH得到了这个想法,它为一个实际上不存在的页面创建了一个处理程序,然后从程序集中提供它。

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="*WebService.asmx" type="MyHandler.WebServiceHandler, MyHandler" />
      </httpHandlers>
   </system.web>
</configuration>

EDIT: I was close, see this article (in VB though): http://www.codeproject.com/KB/aspnet/wsinaclasslibrary.aspx

编辑:我很接近,看到这篇文章(虽然在VB中):http://www.codeproject.com/KB/aspnet/wsinaclasslibrary.aspx

#2


3  

I know this is very old question, but it hasn't been answered properly, so here it is:

我知道这是一个非常古老的问题,但它没有得到适当的回答,所以这里是:

Every *.ASMX request is by default handled by System.Web.Services.Protocols.WebServiceHandlerFactory.

默认情况下,每个* .ASMX请求都由System.Web.Services.Protocols.WebServiceHandlerFactory处理。

Looking into source code of this class in .NET reflector, it's possible to have webservice without ASMX file but you will need to call internal method CoreGetHandler through reflection.

在.NET反射器中查看此类的源代码,可以在没有ASMX文件的情况下使用webservice,但是您需要通过反射调用内部方法CoreGetHandler。

Following method will take your webservice and return its IHttpHandler.

以下方法将获取您的Web服务并返回其IHttpHandler。

public IHttpHandler GetHttpHandlerForWebService(WebService webService, HttpContext context)
{
    var webServiceType = webService.GetType();
    var wshf = new System.Web.Services.Protocols.WebServiceHandlerFactory();
    var coreGetHandler = wshf.GetType().GetMethod("CoreGetHandler");
    var httpHandler = (IHttpHandler)coreGetHandler.Invoke(wshf, new object[] { webServiceType, context, context.Request, context.Response });
    return httpHandler;
}

Once you have your httphandler, it's just matter of calling

一旦你拥有了你的httphandler,它只需要打电话

httpHandler.ProcessRequest(context)

Done. No ASMX and no web.config entries.

完成。没有ASMX,也没有web.config条目。

#3


2  

Here is a very good working article about your problem:

这是一篇关于你的问题的非常好的工作文章:

Creating Web Services in a Class Library project on Codeproject.

在Codeproject上的类库项目中创建Web服务。

#4


0  

Consider trying page methods (see this blog post). All you have to do is add the web method attribute to a static method in the aspx code behind. Then access the PageMethod object from your clientside code (javascript). No ASMX files needed. Hope this helps.

考虑尝试页面方法(请参阅此博客文章)。您所要做的就是将web方法属性添加到后面的aspx代码中的静态方法。然后从客户端代码(javascript)访问PageMethod对象。不需要ASMX文件。希望这可以帮助。

#5


-7  

Short answer is no. The ASMX is the entry point for any web service. There are alternatives if you use WCF, but that's not entirely the same thing.

简短的回答是否定的。 ASMX是任何Web服务的入口点。如果您使用WCF,还有其他选择,但这并不完全相同。

#1


5  

Try something like this. I don't know if it will work though. I got this idea from ELMAH, which creates a handler for a page that doesn't physically exist and then serves it up from the assembly.

尝试这样的事情。我不知道它是否会起作用。我从ELMAH得到了这个想法,它为一个实际上不存在的页面创建了一个处理程序,然后从程序集中提供它。

<configuration>
   <system.web>
      <httpHandlers>
         <add verb="*" path="*WebService.asmx" type="MyHandler.WebServiceHandler, MyHandler" />
      </httpHandlers>
   </system.web>
</configuration>

EDIT: I was close, see this article (in VB though): http://www.codeproject.com/KB/aspnet/wsinaclasslibrary.aspx

编辑:我很接近,看到这篇文章(虽然在VB中):http://www.codeproject.com/KB/aspnet/wsinaclasslibrary.aspx

#2


3  

I know this is very old question, but it hasn't been answered properly, so here it is:

我知道这是一个非常古老的问题,但它没有得到适当的回答,所以这里是:

Every *.ASMX request is by default handled by System.Web.Services.Protocols.WebServiceHandlerFactory.

默认情况下,每个* .ASMX请求都由System.Web.Services.Protocols.WebServiceHandlerFactory处理。

Looking into source code of this class in .NET reflector, it's possible to have webservice without ASMX file but you will need to call internal method CoreGetHandler through reflection.

在.NET反射器中查看此类的源代码,可以在没有ASMX文件的情况下使用webservice,但是您需要通过反射调用内部方法CoreGetHandler。

Following method will take your webservice and return its IHttpHandler.

以下方法将获取您的Web服务并返回其IHttpHandler。

public IHttpHandler GetHttpHandlerForWebService(WebService webService, HttpContext context)
{
    var webServiceType = webService.GetType();
    var wshf = new System.Web.Services.Protocols.WebServiceHandlerFactory();
    var coreGetHandler = wshf.GetType().GetMethod("CoreGetHandler");
    var httpHandler = (IHttpHandler)coreGetHandler.Invoke(wshf, new object[] { webServiceType, context, context.Request, context.Response });
    return httpHandler;
}

Once you have your httphandler, it's just matter of calling

一旦你拥有了你的httphandler,它只需要打电话

httpHandler.ProcessRequest(context)

Done. No ASMX and no web.config entries.

完成。没有ASMX,也没有web.config条目。

#3


2  

Here is a very good working article about your problem:

这是一篇关于你的问题的非常好的工作文章:

Creating Web Services in a Class Library project on Codeproject.

在Codeproject上的类库项目中创建Web服务。

#4


0  

Consider trying page methods (see this blog post). All you have to do is add the web method attribute to a static method in the aspx code behind. Then access the PageMethod object from your clientside code (javascript). No ASMX files needed. Hope this helps.

考虑尝试页面方法(请参阅此博客文章)。您所要做的就是将web方法属性添加到后面的aspx代码中的静态方法。然后从客户端代码(javascript)访问PageMethod对象。不需要ASMX文件。希望这可以帮助。

#5


-7  

Short answer is no. The ASMX is the entry point for any web service. There are alternatives if you use WCF, but that's not entirely the same thing.

简短的回答是否定的。 ASMX是任何Web服务的入口点。如果您使用WCF,还有其他选择,但这并不完全相同。