首页 文章

如何在C#中将我自己的wsdl包含在我的Web服务中

提问于
浏览
5

我有一个.wsdl文件,我的Web服务(旧的asmx样式)必须实现 . 这是照顾 . 当我发布Web服务时,您可以使用?wsdl参数调用它来获取生成的wsdl .

如何包含我的.wsdl文件,以便返回的文件而不是生成的文件?

是否可以在我的Web服务类中使用属性?

3 回答

  • 2

    是否与“旧式”ASMX保持一致?或者你可以升级到WCF?这真的是微软最新的网络服务产品,如果你正在做一些新的东西而且你使用的是.NET 3.0或更高版本 - 为什么要花时间在“旧”技术上呢?

    在WCF中,您肯定可以定义一个静态物理WSDL文件,供连接到元数据 endpoints 的客户端使用(您的“...?wsdl”URL) . 不确定你是否也可以在ASMX中完成 .

    好的,在ASMX / .NET 2.0上,您当然可以将实际的WSDL文件放在您网站的根目录下,然后像这样引用它:

    http://yourwebserver/YourVirtDir/MyService.wsdl
    

    我不知道是否有办法“重定向”

    http://yourwebserver/YourVirtDir/MyService.asmx?wsdl
    

    请致电转到该固定网址 . 不过我相信别人会知道的!

  • 2

    为了避免在Web服务应用程序中在两个不同的URL(即* .asmx?wsdl URL和自定义URL)上使用两个不同的WSDL混淆,您可以编写一个HttpModule来拦截对* .asmx的请求? wsdl URL并返回您的自定义WSDL .

    EDIT: 这是一个例子,根据我之前编写的一些代码进行了改编和简化,使得标准的* .asmx?wsdl URL可以使用自定义WSDL .

    using System;
    using System.IO;
    using System.Web;
    using System.Web.Services.Configuration;
    
    namespace DemoWebService
    {
     public class CustomWsdlModule :
      IHttpModule
     {
      public void
      Init(HttpApplication application)
      {
       // hook up to BeginRequest event on application object
       application.BeginRequest += new EventHandler(this.onApplicationBeginRequest);
      }
    
      public void
      Dispose()
      {
      }
    
      private void
      onApplicationBeginRequest(object source, EventArgs ea)
      {
       HttpApplication application = (HttpApplication)source;
       HttpRequest request = application.Request;
       HttpResponse response = application.Response;
    
       // check if request is for WSDL file
       if ( request.Url.PathAndQuery.EndsWith(".asmx?wsdl", StringComparison.InvariantCultureIgnoreCase) )
       {
        // if Documentation protocol is not allowed, throw exception
        if ( (WebServicesSection.Current.EnabledProtocols & WebServiceProtocols.Documentation) == 0 )
        {
         throw new System.InvalidOperationException("Request format is unrecognized.");
        }
    
        // get path to physical .asmx file
        String asmxPath = request.MapPath(request.Url.AbsolutePath);
    
        // build path to .wsdl file; should be same as .asmx file, but with .wsdl extension
        String wsdlPath = Path.ChangeExtension(asmxPath, ".wsdl");
    
        // check if WSDL file exists
        if ( File.Exists(wsdlPath) )
        {
         // read WSDL file
         using ( StreamReader reader = new StreamReader(wsdlPath) )
         {
          string wsdlFileContents = reader.ReadToEnd();
    
          // write WSDL to response and end response without normal processing
          response.ContentType = "text/xml";
          response.Write(wsdlFileContents);
          response.End();
         }
        }
       }
      }
     }
    }
    

    此简化代码假定您的自定义WSDL与扩展名为.wsdl的.asmx文件位于同一文件夹中 . 需要通过web.config文件将HttpModule挂钩到您的Web服务应用程序中:

    <?xml version="1.0"?>
    <configuration>
        <!-- ... -->
        <system.web>
      <!-- ... -->
      <httpModules>
       <add
        type="DemoWebService.CustomWsdlModule"
        name="CustomWsdlModule"/>
       <!-- ... -->
      </httpModules>
      <!-- ... -->
        </system.web>
        <!-- ... -->
    </configuration>
    
  • -1

    您可以通过在Web服务上指向.NET Framework附带的disco.exe工具来生成WSDL和DISCO文件 .

    disco.exe http://webserver/MyWebService.asmx
    

    创建以下文件:

    results.discomap
     MyWebService.disco
     MyWebService.wsdl
    

相关问题