2011-02-24 50 views
0

好吧,它已經有一段時間,因爲我已經有一個Web引用工作。我需要一個進修。我想我有我需要得到迴應,但要我失去了一些東西代碼的約80%。也許你可以幫我:)需要一些幫助的代碼用於獲取從Web服務的響應。你能幫我連接點嗎?

考慮:
指向.wsdl URL時,在方法列表稱爲GetSomething一個Web方法。

這將產生幾類/對象:

  • GetSomethingRequest
  • GetSomethingCompletedEventHandler
  • GetSomethingCompletedEventArgs
  • myComplexType

其中我用它來創建此代碼:

void someMethodToTestResponse() 
{ 
    GetSomethingRequest request = new GetSomethingRequest(); 

    // fill in the request 
    request.myComplexType.Property1 = "Blah"; 
    request.myComplexType.Property2 = "Kachoo"; 

    GetSomethingCompletedEventHandler handler = GetSomethingCompleted_Response; 

    //.... ok now what? 
    //handler.Invoke(???) 
    // at this point I'm supposed to send an object for source (request maybe?) 
    // and a new instance of GetSomethingCompletedEventArgs but that class is 
    // asking for stuff that makes me think that is not the right idea. 

} 

void GetSomethingCompleted_Response(object source, GetSomethingCompletedEventArgs args) 
{ 
    // get the result 
    var result = args.Result; 
} 

我在做什麼錯?我錯過了什麼?提前致謝。

+0

.NET哪個版本您使用的?除非你被困在.NET 2.0中,否則你應該使用服務引用。 – 2011-02-24 19:57:36

+0

好問題,我正在使用3.5。 – DJTripleThreat 2011-02-24 19:59:18

+0

然後請參閱https://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/ – 2011-02-24 20:44:15

回答

0

好吧,我想通了,我需要找到一個Service類型的類。看到這個SO Post它提到:

private com.nowhere.somewebservice ws; 

的問題是,他們提供的類沒有智能檢測,我和我想這是不是我所期待的。

這是我會怎麼解決我的問題:

blah.webservice.SomeMainServiceClass service = new SomeMainServiceClass(); 
GetSomethingRequest request = new GetSomethingRequest(); 

// fill in the request 
request.myComplexType.Property1 = "Blah"; 
request.myComplexType.Property2 = "Kachoo"; 

object myResponse = service.GetSomething(request); 
0

你不需要Web服務的源代碼。 Web服務可以用Java實現。創建服務引用炒作相同,因爲我們真的不知道另一邊是什麼。

因此,請嘗試在VS2008中添加服務引用,並輸入到正常工作的Web服務的URL。 VS將檢查服務器上的wsdl併爲您生成所需的類。

從比起,你只需要調用服務作爲一些普通的方法調用。這意味着你不必提出請求和http以及這些細節。所有對你隱藏的東西。除了可以更改許多WCF設置的app.config。