1
votes

I'm using VisualStudio2013. Its important to note for readers that the code which this asmx is derived from works perfectly but I do not know how to use the asmx WebService. I downloaded the whole nine yards from here https://sourceforge.net/projects/shorturl-dotnet/

I cannot figure out how to get/set properties of the following CreateUrl() WebMethod. I want to learn how to use the entire WebService but started here.

In the example that follows I send a URL to the CreateURL() method which will shorten the URL and perform other tasks; I do not know how to get properties from the returned ShortUrl.Container Class: I have not been successful accessing the data after the class(es) are returned to my calling method.

// 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 class returned as 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; }
        }
    }
}

In VS2013 I add the Service Reference to point to http://tap.tools.api.asmx as the service endpoint and name the VS2013 reference as ShortenUrl. VS2013 generates the APISoapClient and Container classes.

// 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;

I'm not getting anywhere with either and I think my problem is the instance of the oShortUrl object instantiated within the public ShortUrl.Container CreateUrl(string real_url) method. I do not know how to get any of the properties from that instance of oShortUrl the Container class returns to my methods.

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

Odd as it may sound as old and outdated the use of asmx happens to be I never worked with -any- WebServices yet which explains why I am weak and throw myself to the mercy of the court.

// EDIT: 2016-07-19 ~2:41pm

VS2013 generated several classes from the WSDL two of which appear to be useful as seen in Intellisense...

// class APISoapClient and class Container

When I use a local variable with APISoapClient a shortened URL is generated as I can see using SQL Management Studio and note all of the data is properly generated but I am not able to get/set on any other WebMethods or properties with to get/set data...

// 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");

And...

// 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"); 
2
by any chance when adding the service reference were you able to display the WSDL? - yopez83
Hey yopez I sure did and can read the wsdl. VS2013 generates a Reference.svcmap which maps api.disco, api.wsdl, configuration.svcinfo and another instance I just observed named configuration91.svcinfo all of which are readable in the editor. - ClintonGallagher
You dont need to write all code here just state your approach on overcoming your problem and paste the code where you are facing problems. - ABi

2 Answers

1
votes

If i understood what you're trying to get is the Container returned from the Web Method. If so then just create a variable type of Container and assign the method call to it. Like ShortUrl.Container c = u.CreateUrl(...) then from c you can get the values you're looking for.

1
votes

Think about this @clintongallagher. When you do the following call,

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

I don't know how does GetUrl(shortened_value) is supposed to work but, assuming it will get from the DB the shortened_value passed in, of course the result would not be the same since the shortened value saved was 'A' and your asking for B.