2011-05-05 54 views
2

我目前正在使用C#來理解SOAP協議,我在Google中找到了一些示例並瞭解了信封,標題,正文。使用SOAP訪問數據庫的最佳方式

我使用webservice進行身份驗證,但我想知道在哪裏可以實現一個類或方法來訪問提供的用戶名和密碼的數據庫,我的意思是,肥皂標題具有user =「john」pass =「odos223kiwi0X」服務器收到標題,現在用戶提供訪問數據庫並檢查密碼。

如果一個正確的選項在soap類中創建一個自定義方法來做到這一點?

+2

我建議使用Windows Communication Foundation來抽象SOAP實現的基本細節,以便您可以專注於編寫處理業務邏輯所需的代碼 – 2011-05-05 13:28:53

+0

您不清楚您在此處使用的是什麼技術。你自己實現了整個服務堆棧嗎?或者你在使用asmx服務? – MattDavey 2011-05-05 13:45:43

+0

嗨MattDavey!我正在使用asmx服務。 – Dev9 2011-05-05 14:04:06

回答

2

,你可以創建一個類,就像下面:

using System.Diagnostics; 
using System.Xml.Serialization; 
using System; 
using System.Web.Services.Protocols; 
using System.Web.Services; 
using System.Net; 

[System.Web.Services.WebServiceBindingAttribute(
Name = "FunctionName", 
Namespace = "nameSpace")] 
public class ClassName: 
System.Web.Services.Protocols.SoapHttpClientProtocol 
{ 
    public ClassName(string uri) // Constractor 
    { 
     this.Url = uri; // the full path for your server we will make later on in the answer 
    } 

    [System.Web.Services.Protocols.SoapDocumentMethodAttribute(
    "nameSpace/ClassName", 
    RequestNamespace = "nameSpace", 
    ResponseNamespace = "nameSpace", 
    Use = System.Web.Services.Description.SoapBindingUse.Literal, 
    ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] 

    public object[] FunctionName(string Parameter1) 
    { 
     object[] results = { }; 

     try 
     { 
      results = this.Invoke("FunctionName", new object[] { Parameter1}); 
      return ((object[])(results[0])); 
     } 
     catch (Exception error) 
     { 
      object[] webException = { -1, error.Message }; 
      return (webException); 
     } 
    } 
} 

現在我們創建的asmx服務:

創建一個Web服務和命名空間下補充一點:

[WebService(Namespace = "NameSpace")] //same namespace you wrote in the class 

然後添加你的函數和Object []作爲返回值。

[WebMethod] 
public object[] FunctionName(string Parameter1) // function name and parameters should be the same in your class where you called the web service (case sensitive) 
{ 
    ... // your code 
} 

**你可以下載http://www.fiddler2.com/fiddler2/version.asp,將允許您查看和跟蹤傳出請求

,請給我回來,如果你需要任何進一步的信息。

+0

-1:爲什麼不在.NET 2.0中使用「添加服務引用」或「添加Web引用」? – 2013-09-11 22:34:41

+0

@JohnSaunders,我正在尋找調用服務的最簡單方法。我嘗試了「添加Web引用」(它不再適用於VS 2010)和「添加服務引用」。我也看了一下wsdl.exe和svcutil.exe。所有4種方法爲我生成大量代碼。我打電話的服務有634個方法,但我只需要一種方法。所以我只拿了Arrabi的答案,因爲代碼更易讀(這實際上是由wsdl.exe生成的代碼的一部分,減去了異步部分)。 – AaA 2015-01-22 08:40:01

+0

@BobSort:爲什麼Reference.cs中的代碼量對您很重要?通常很重要的是您必須維護的代碼的大小,而不是爲您生成的代碼的大小。 – 2015-01-22 13:51:59

相關問題