2010-08-04 38 views
2

我想在.NET中創建一個WebService誰揭露多的WebMethods如何覆蓋生成WSDL在Web服務的.Net

我需要每個新執行一個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"; 
    } 
} 

用URL重寫或HttpHandler的:

的HelloWorld唯一的WebMethod:http://localhost/Service/1.0/Service.asmx

^h elloWorld WebMethod和NewMethodInVersion1_1:http://localhost/Service/1.1/Service.asmx

如何爲客戶使用的特定版本動態生成wsdl?

回答

1

解決辦法:

  1. 創建一個每個Web服務版本的目錄:/ 1 /;/2 /;/3/...
  2. Web服務的每個版本都從以前的版本繼承:/ 2/Service:_1.Service;/3 /服務:_2.Service ...
  3. 落實XmlSchemaProvider上的對象直接通過您的WebMethod帶有自定義序列化使用界面IXmlSerializable的
  4. 揭露使用WebServiceVersionAttribute(EX屬性返回:該物業帳戶僅暴露Web服務版本大於或等於2)
  5. 使用HttpModule攔截Web服務版本(使用正則表達式:new Regex(「/([0-9])+ /(。)*」))
  6. IXmlSerializable接口的WriteXml方法檢查WebServiceVersionAttribute以過濾Xml結果(爲了不序列化更大版本的屬性)

最大的困難是落實XmlSchemaProvider ...

0

如果你這樣做,你會不會發布兩個獨立的Web應用程序/網站?

這會比兩個客戶指向一個Web服務更安全,並且有可能導致他們調用錯誤的方法。

然後,您只需指向相關的Web應用程序/網站並獲取WSDL。

+0

如果錯誤發生在1.0版本,我需要釋放修補程序,無需部署的 新版本的新的WebMethod或財產,我不想要在VSS或TFS中分支我的WebService的多個版本 – Jni 2010-08-04 11:59:33

+0

難道你不得不以任何方式發佈,因爲你必須修復版本1.0後面的代碼。 我的方法可以讓你發佈1.0版本的代碼,而不會影響web服務的1.1版本? – Datoon 2010-08-04 12:04:45

+0

我不能使用2個WebApplication,因爲將來會添加業務對象的新屬性,所以我需要在每個版本中分開wsdl修改。 在舊項目中,我在IIS上的分隔符WebApplication上將每個版本部署在服務器上。但是,當發生錯誤時,在每個Web應用程序版本中都很難修復。 – Jni 2010-08-04 12:11:51

1

我暫時解決了我的問題,通過服務於另一個WSDL文件用的HttpModule

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文件

相關問題