【问题标题】:How to override Wsdl generation in Web Services .Net如何覆盖 Web Services .Net 中的 Wsdl 生成
【发布时间】:2010-08-04 11:45:36
【问题描述】:

我想在 .Net 中创建一个公开多个 WebMethods 的 WebService

我需要每个新实现的 WebService 版本(WebMethod 或业务对象中的新属性),如下所示:

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

    [WebMethod]
    [WebServiceVersion("1.1")]
    public string NewMethodInVersion1_1()
    {
        return "Hello World";
    }
}

使用 UrlRewriting 或 HttpHandler :

仅限HelloWorld WebMethod:http://localhost/Service/1.0/Service.asmx

HelloWorld WebMethod 和 NewMethodInVersion1_1 : http://localhost/Service/1.1/Service.asmx

如何为客户使用的特定版本动态生成 wsdl?

【问题讨论】:

    标签: c# asp.net web-services wsdl


    【解决方案1】:

    我通过使用 HttpModule 提供另一个 wsdl 文件暂时解决了我的问题

    public class WsdlModule : IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException();
        }
    
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            HttpContext context = app.Context;
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
    
            string url = request.Url.AbsoluteUri.ToLower();
    
            if (url.Contains("wsdl"))
            {
                response.WriteFile(context.Server.MapPath("wsdl/1.0/Service.wsdl"));
                response.End();
            }
        }
    }
    

    如果可能,我更喜欢动态生成 wsdl 文件

    【讨论】:

      【解决方案2】:

      解决办法:

      1. 为每个 Web 服务版本创建一个目录:/1/; /2/; /3/ ...
      2. 每个版本的网络服务都继承了以前的版本:/2/Service:_1.Service; /3/服务:_2.Service ...
      3. 通过使用 IXmlSerializable 接口的自定义序列化,在 WebMethod 直接返回的对象上实现 XmlSchemaProvider
      4. 使用 WebServiceVersionAttribute 公开属性(例如:属性 Account 仅针对大于或等于 2 的 Web 服务版本公开)
      5. 使用 HttpModule 拦截 Web 服务版本(使用正则表达式:new Regex("/([0-9])+/(.)*")
      6. IXmlSerializable 接口的 WriteXml 方法检查 WebServiceVersionAttribute 以过滤 Xml 结果(为了不序列化更高版本的属性)

      最大的难点是实现XmlSchemaProvider ...

      【讨论】:

        【解决方案3】:

        如果您这样做,您不会发布两个单独的 Web 应用程序/网站吗?

        这比让两个客户指向一个 Web 服务更安全,并且有他们调用错误方法的风险。

        然后您只需指向相关的 Web 应用程序/网站并获取 WSDL。

        【讨论】:

        • 如果 1.0 版本出现错误,我需要发布修复而不部署新版本的新 webmethod 或属性并且我不想在 VSS 或 TFS 中分支多个版本我的网络服务
        • 您是否必须发布,因为您必须修复版本 1.0 后面的代码。我的方法可以让你只发布 1.0 版本的代码而不影响 1.1 版本的网络服务?
        • 我不能使用2个WebApplication,因为未来会添加业务对象的新属性,所以我需要在每个版本中单独修改wsdl。在一个旧项目中,我将每个版本部署在 IIS 上的分隔符 WebApplication 中的服务器上。但是当出现 bug 时,很难在每个 Web 应用程序版本中修复
        • 您能更清楚地了解您想要实现的目标吗?您将业务对象作为 Web 服务层的一部分是否正确?
        猜你喜欢
        • 2019-02-27
        • 1970-01-01
        • 2010-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多