我怎么有我自己的WSDL在C#我的web服务(How do I include my own wsd

2019-08-03 07:35发布

我有我的web服务(ASMX旧风格)必须实现一个.wsdl文件。 这是照顾。 当我在网上发布服务,您可以用?WSDL参数调用它来获得生成WSDL。

如何包括我的.wsdl文件,这样是返回,而不是产生一个一个?

是否有可能做在我的web服务类中的属性?

Answer 1:

它是一个给定的留在“旧式” ASMX? 或者,你能向上移动到WCF? 这真的是微软最新的Web服务产品,如果你正在做一些新的东西,你是在.NET 3.0或更高版本 - 为什么要花费时间的“老”技术?

在WCF,你肯定可以定义通过连接到您的元数据终结点(你的“......?WSDL” URL)的客户端使用一个静态的物理WSDL文件。 不知道你是否能做到这一点的ASMX了。

OK,在ASMX / .NET 2.0,你当然可以始终把实际的WSDL文件你的网站的根目录下,然后就引用它是这样的:

http://yourwebserver/YourVirtDir/MyService.wsdl 

我不知道是否有到“重定向”的方式

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

打电话去那个固定的URL来代替。 我敢肯定,别人会知道,但!



Answer 2:

为了避免可在两个不同的网址,两个不同的WSDL的困惑(即*的.asmx?WSDL URL和一个自定义URL)在Web服务应用程序,你可以写拦截请求*的.asmx一个HttpModule? WSDL URL并返回您的自定义WSDL来代替。

编辑:下面是一个例子,改编和一些代码,我以前写的,使得可在标准*的.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文件。 该HTTP模块需要被钩到通过web.config文件的Web服务应用程序:

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


Answer 3:

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

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

将创建下列文件:

 results.discomap
 MyWebService.disco
 MyWebService.wsdl


文章来源: How do I include my own wsdl in my web service in C#