C#如何从asmx Web服务获取/设置数据
我正在使用VisualStudio2013。读者需要注意的是,这个asmx源代码工作得非常完美,但我不知道如何使用ASMXWebService。我从这里下载了整个九码 我不知道如何获取/设置以下CreateUrl()WebMethod的属性。我想学习如何使用整个Web服务,但从这里开始 在下面的示例中,我向CreateURL()方法发送一个URL,该方法将缩短URL并执行其他任务;我不知道如何从返回的ShortUrl获取属性。容器类:在类返回到调用方法后,我没有成功访问数据 //网络方法C#如何从asmx Web服务获取/设置数据,c#,web-services,soap,asmx,C#,Web Services,Soap,Asmx,我正在使用VisualStudio2013。读者需要注意的是,这个asmx源代码工作得非常完美,但我不知道如何使用ASMXWebService。我从这里下载了整个九码 我不知道如何获取/设置以下CreateUrl()WebMethod的属性。我想学习如何使用整个Web服务,但从这里开始 在下面的示例中,我向CreateURL()方法发送一个URL,该方法将缩短URL并执行其他任务;我不知道如何从返回的ShortUrl获取属性。容器类:在类返回到调用方法后,我没有成功访问数据 //网络方法 pub
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;
}
}
//容器类作为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; }
}
}
}
这两种方法我都做不到,我认为我的问题是在public ShortUrl.Container CreateUrl(string real_url)方法中实例化的oShortUrl对象的实例。我不知道如何从容器类返回给我的方法的oShortUrl实例中获取任何属性
// oShortUrl
ShortUrl.Container oShortUrl = new ShortUrl.Container();
虽然听起来很奇怪,但asmx的使用却恰巧是我从未使用过任何Web服务,这就解释了为什么我很软弱,只能听命于法庭的摆布
//编辑:2016-07-19~2:41
VS2013从WSDL生成了几个类,从Intellisense中可以看出,其中两个似乎很有用
//类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");
如果我明白了,您想要得到的是从Web方法返回的容器。如果是这样,那么只需创建一个变量类型的容器,并将方法调用分配给它。像
ShortUrl.Container c=u.CreateUrl(…)
那么从c
你可以得到你想要的值。想想这个@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(shorted\u value)
应该如何工作,但是,假设它将从数据库获得传入的shorted\u值,当然,结果会不一样,因为保存的缩短值是“A”,并且您在添加服务引用时请求B
,您是否能够显示WSDL?嘿,yopez,我当然可以,并且可以阅读WSDL。VS2013生成一个Reference.svcmap,它映射api.disco、api.wsdl、,configuration.svcinfo和我刚刚观察到的另一个名为configuration91.svcinfo的实例,所有这些都可以在编辑器中读取。您不需要在此处编写所有代码,只需说明您解决问题的方法,并将代码粘贴到您面临问题的位置。是的,我也这么认为,但它不会产生预期的结果。请参阅我的原始问题编辑:2016-07-19~2:41pmDid你能让它工作吗?如果是的话,请检查这个问题的答案。谢谢我的道歉,尤佩兹,谢谢你,我开始处理你的评论,但不知怎的,我的UPS在电池上剩下7个OS时爆炸了,所以我一直在努力做出回应,以保护我的资产。我将您的评论标记为答案,如果我没有丢失机器,我可能会回到这个问题上来……听到@ClintonGallagher,我很抱歉。如果你还有其他问题我可以帮你的话,请告诉我。谢天谢地,7mo旧UPS电池上的电源浪涌仍然切断了我的以太网连接。我会在几天后再回到这个话题上来
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;
}