2016-07-28 17 views
1

私はVisualStudio2013を使用しています。このasmxが由来するコードは完全には動作するが、asmx WebServiceの使用方法はわかりません。ここから9ヤード全体をダウンロードしましたhttps://sourceforge.net/projects/shorturl-dotnet/C#asmx Webサービスからデータを取得/設定する方法

次の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クラスIは、サービスのエンドポイントとしてhttp://tap.tools.api.asmxを指すとVS2013の参照に名前を付けるためにサービス参照を追加oShortUrl

VS2013で
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; } 
     } 
    } 
} 

として返さ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- Webサービスで働いはまだありませんこれは私が苦手と裁判所の慈悲に身を投げる理由を説明しないことを起こります。

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

VS2013は...インテリセンスに見られるように有用であると思われるそのうちの2つがWSDLから

//クラスAPISoapClientをいくつかのクラスを生成しましたクラスコンテナ

APISoapClientでローカル変数を使用すると、SQL Management Studioを使用して見ることができ、すべてのデータが正しく生成されているが、他のWebメソッドまたはデータを取得/設定するためのプロパティ...

// 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は、api.disco、api.wsdl、configuration.svcinfoをマッピングするReference.svcmapを生成し、私はちょうど、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; 
} 

私はshortened_valueが渡され、それはDBから取得すると仮定すると、GetUrl(shortened_value)が動作するようになっているんのか分からないが、もちろん結果は、以来、同じではないでしょう短縮された値は 'A'で、あなたにはBを求めています。

+0

あなたはクリントンを働かせてくれましたか? – yopez83

関連する問題