2016-07-28 121 views
1

我正在使用VisualStudio2013。對讀者來說,重要的是要注意,這個asmx源自的代碼完美工作,但我不知道如何使用asmx WebService。我從這裏下載了整個9碼https://sourceforge.net/projects/shorturl-dotnet/C#如何從asmx Web Service獲取/設置數據

我不知道如何獲取/設置以下CreateUrl()WebMethod的屬性。我想學習如何使用整個WebService,但是從這裏開始。

在下面的示例中,我將URL發送到CreateURL()方法,該方法將縮短URL並執行其他任務;我不知道如何從返回的ShortUrl.Container類獲取屬性:在類返回給我的調用方法後,我沒有成功訪問數據。

//的WebMethod

public class API : System.Web.Services.WebService { 

[WebMethod] 
public ShortUrl.Container CreateUrl(string real_url) 
{ 
    ShortUrl.Container oShortUrl = new ShortUrl.Container(); 

    oShortUrl.RealUrl = real_url; 
    oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl(); 
    oShortUrl.CreateDate = DateTime.Now; 
    oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress; 

    ShortUrl.Utils.AddUrlToDatabase(oShortUrl); 

    oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl); 

    return oShortUrl; 
    } 
} 

// ShortUrl.Container類返回oShortUrl

namespace ShortUrl 
{ 
    /// <summary> 
    /// Container for the ShortURL object 
    /// </summary> 
    public class Container 
    { 
     private string _real_url; 
     private string _short_url; 
     private DateTime _create_date; 
     private string _created_by; 

     public Container() 
     { 
      this.CreateDate = DateTime.Now; 
      this.CreatedBy = "tap"; 
      this.RealUrl = null; 
      this.ShortenedUrl = "Unknown"; 
     } 

     public string RealUrl 
     { 
      get { return _real_url; } 
      set { _real_url = value; } 
     } 

     public string ShortenedUrl 
     { 
      get { return _short_url; } 
      set { _short_url = value; } 
     } 

     public DateTime CreateDate 
     { 
      get { return _create_date; } 
      set { _create_date = value; } 
     } 

     public string CreatedBy 
     { 
      get { return _created_by; } 
      set { _created_by = value; } 
     } 
    } 
} 

在VS2013我添加服務引用指向http://tap.tools.api.asmx作爲服務端點並將其命名爲VS2013參考作爲ShortenUrl。 VS2013生成APISoapClient和Container類。

// get/set properties of the ShortUrl.Container class 
// by means of APISoapClient 
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient(); 
u.CreateUrl("http://clintongallagher.com/tag-target-url.html"); 


// get/set properties of the ShortUrl.Container class 
// by means of Container class 
ShortenUrl.Container c = new ShortenUrl.Container(); 
string url = c.RealUrl; 

我不會跟任何地方任何讓我覺得我的問題是公共ShortUrl.Container CreateUrl(字符串real_url)方法中實例化的oShortUrl對象的實例。我不知道如何從oShortUrl的Container實例返回我的方法的任何屬性。

// oShortUrl 
ShortUrl.Container oShortUrl = new ShortUrl.Container(); 

奇怪,因爲它可能聽起來陳舊過時的使用ASMX的恰好是我從來沒有與-any- WebServices的工作還沒有這解釋了爲什麼我軟弱,並拋出自己的法院的擺佈。

//編輯:2016年7月19日〜2:41pm

VS2013產生幾類從WSDL其中兩個表面上是有用如智能感知看到...

//類APISoapClient和類容器

當我與APISoapClient一起使用本地變量時,會生成縮短的URL,因爲我可以看到使用SQL Management Studio並注意所有數據都已正確生成,但我無法在任何其他WebMethods上獲取/設置或與獲取/設置數據的屬性...

// Exposes two WebMethods: CreateUrl and GetUrl  
ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient(); 

// Does generate the shortened URL 
u.CreateUrl("http://clintongallagher.com/tag-target-url.html"); 
// Should return the URL that was shortened but doesn't 
u.GetUrl("i2Z5H"); 

而且......

// Exposes the properties in Intellisense but does not return data 
ShortenUrl.Container c = new ShortenUrl.Container(); 

// returns 1/1/0001 12:00:00 AM 
lblCreateDate.Text = "CreateDate: " + c.CreateDate.ToString(); 
// returns nothing 
lblCreatedBy.Text = "CreatedBy: " + c.CreatedBy; 
// returns nothing 
lblRealUrl.Text = "RealUrl: " + c.RealUrl; 
// returns ShortenUrl.Container 
lblShortenedUrl.Text = "ShortenedUrl: " + u.GetUrl("i2Z5H"); 
+1

當你添加服務引用時,你能夠顯示WSDL嗎? – yopez83

+0

嘿yopez我確實做過,可以閱讀wsdl。 VS2013生成一個Reference.svcmap,它映射api.disco,api.wsdl,configuration.svcinfo和我剛纔觀察到的名爲configuration91.svcinfo的另一個實例,所有這些實例都可以在編輯器中讀取。 – ClintonGallagher

+0

你不需要在這裏編寫所有的代碼,只是說明你的方法克服你的問題,並粘貼代碼在你面臨問題的地方。 – ABi

回答

1

如果我理解你正在試圖獲得什麼是容器從Web方法返回。如果是這樣,那麼只需創建一個變量類型的Container並將方法調用分配給它。像ShortUrl.Container c = u.CreateUrl(...)然後從c你可以得到你正在尋找的值。

+0

是的,我也這麼認爲,但它沒有產生預期的結果。參考我原來的問題編輯:2016-07-19〜2:41pm – ClintonGallagher

+0

你有沒有得到這個工作?如果是這樣,請回答這個問題很好。謝謝 – yopez83

+0

我的歉意yopez和謝謝你,我開始處理你的評論,但不知何故,我的UPS吹熄了電池留下的7mos,所以我一直在努力迴應保護我的資產。我將你的評論標記爲答案,如果我不丟失機器,可能會回到這個問題... – ClintonGallagher

1

想想這個@clintongallagher。當你做下面的調用,

ShortenUrl.APISoapClient u = new ShortenUrl.APISoapClient(); 
u.CreateUrl("http://clintongallagher.com/tag-target-url.html"); 


[WebMethod] 
public ShortUrl.Container CreateUrl(string real_url) 
{ 
    ShortUrl.Container oShortUrl = new ShortUrl.Container(); 

    oShortUrl.RealUrl = real_url; 

    //here you're assigning a value to this object, let's say 'A' 
    oShortUrl.ShortenedUrl = ShortUrl.Utils.UniqueShortUrl(); 

    oShortUrl.CreateDate = DateTime.Now; 
    oShortUrl.CreatedBy = HttpContext.Current.Request.UserHostAddress; 

    //then here you're saving the object with the Shortened value 'A' you just got 
    ShortUrl.Utils.AddUrlToDatabase(oShortUrl); 

    /* 
    *finally you're replacing the Shortened value with another value, 
    *let's say 'B', which is the object you're going to return*/ 
    oShortUrl.ShortenedUrl = ShortUrl.Utils.PublicShortUrl(oShortUrl.ShortenedUrl); 

    return oShortUrl; 
} 

我不知道如何GetUrl(shortened_value)應該工作,但是,假設它會從數據庫中獲得shortened_value傳遞,當然結果不會是因爲同樣的保存的縮短值是'A'並且您要求B

+0

你有沒有得到它的工作克林頓? – yopez83