动态C#.NET web服务 [英] Dynamic C#.NET Webservice

查看:140
本文介绍了动态C#.NET web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是类在C#ASP.NET项目允许写一些随机的脚本语言动态web服务公开方法的脚本 - 换句话说,脚本应该能够公开的任何名称的方法与任何签名(只要它是有效的,反正)到外面的世界,通过此SOAP接口(可以随意添加和删除,而无需硬code修改),因此我需要能够创建一个web服务同时能够动态地添加和在运行时删除方法在C#类。

I am using a class in a C# ASP.NET project to allow a script written in some random scripting language to expose webservice methods dynamically - in other words, the script should be able to expose a method of any name with any signature (as long as it's valid, anyway) to the outside world through this SOAP interface (able to add and remove them at will, without needing a hard code change), and as such I need to be able to create a webservice class in C# while being able to dynamically add and remove methods at runtime.

现在,我已经能够拿出这么迄今为止最好的计划是(运行时)生成C#code重新present web服务,使用System.Reflection.Emit编译它,然后加载装配在运行时 - 只要所有的脚本添加或从服务中删除的方法来/(应该不会经常发生,记)

Now, the best plan I've been able to come up with so far is (runtime) generating C# code to represent the webservice, using System.Reflection.Emit to compile it and then loading the assembly at runtime - all whenever the script adds or removes a method to/from the service (should not happen very often, mind).

有没有人有一个更好的主意比这?

Does anyone have a better idea than this?

推荐答案

您可以通过使用<一个修改WSDL href=\"http://msdn.microsoft.com/en-us/library/system.web.services.description.soapextensionreflector.aspx\">SoapExtensionReflector类。从柯克·埃文斯博客

You can modify WSDL by using SoapExtensionReflector class. From Kirk Evans Blog:

当被反射在你的类型来为您服务提供WSDL定义的SoapExtensionReflector被调用。您可以利用这种类型的拦截反射调用和修改WSDL输出。

The SoapExtensionReflector is called when your type is being reflected over to provide the WSDL definition for your service. You can leverage this type to intercept the reflection call and modify the WSDL output.

以下示例删除第一种方法出2 Web服务方法:

The following example removes the first method out of 2 web service methods:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
   [WebMethod]
   public string HelloWorld()
   {
      return "Hello World";
   }

   [WebMethod]
   public int Multiply(int a, int b)
   {
      return a * b;
   }
}

创建SoapExtensionReflector继承的类:

Create a class inherited from SoapExtensionReflector:

namespace TestWebservice
{
   public class MyReflector : SoapExtensionReflector
   {
      public override void ReflectMethod()
      {
         //no-op
      }

      public override void ReflectDescription()
      {
         ServiceDescription description = ReflectionContext.ServiceDescription;
         if (description.PortTypes[0].Operations.Count == 2)
            description.PortTypes[0].Operations.RemoveAt(0);
         if (description.Messages.Count == 4)
         {
            description.Messages.RemoveAt(0);
            description.Messages.RemoveAt(0);
         }
         foreach (Binding binding in description.Bindings)
         {
            if (binding.Operations.Count == 2)
               binding.Operations.RemoveAt(0);
         }
         if (description.Types.Schemas[0].Items.Count == 4)
         {
            description.Types.Schemas[0].Items.RemoveAt(0);
            description.Types.Schemas[0].Items.RemoveAt(0);
         }
      }
   }
}

这添加到配置/的System.Web节在web.config中:

Add this to configuration/system.web section in web.config:

<webServices>
   <soapExtensionReflectorTypes>
      <add type="TestWebservice.MyReflector, TestWebservice" />
   </soapExtensionReflectorTypes>
</webServices>

这应该给你一个起点,以动态地去除WSDL文档的方法。您还需要从web方法抛出NotImplementedException,如果它被禁用。

This should give you a starting point to dynamically removing methods from WSDL document. You would also need to throw NotImplementedException from web method if it is disabled.

最后,你需要禁用调用的.asmx端点,而无需?WSDL参数产生的Web服务文档。 wsdlHelpGenerator元素设置href属性的一些网址。您可以使用DefaultWsdlHelpGenerator.aspx作为自己的文档处理程序的起点。查看Web服务文档方面的疑问在 XML文件,2002年8月

Finally, you need to disable web service documentation produced by invoking .asmx endpoint without ?WSDL parameter. Set href attribute of wsdlHelpGenerator element to some URL. You can use DefaultWsdlHelpGenerator.aspx as a starting point for your own documentation handler. See question on web service documentation in XML Files, August 2002.

这篇关于动态C#.NET web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆