问题描述
Hello All
我有以下网络方法。这是一个暂时的模拟服务,因为原始服务正在开发中。
I have the following web method. This is a mock service for time being, as the original one is under development.
TestService.asmx.cs
[WebMethod]
public SearchByContentIDResponse SearchByContentID(SearchByContentIDRequest request)
{
列表与LT; ExternalMetadata> data = new List< ExternalMetadata>
{
new ExternalMetadata {ContentID =" ContentID1",ContentType =" Type1",Definition = 1,HasProxy = false,IsActive = true,Length = 1,SystemName
=" SysName1"},
new ExternalMetadata {ContentID =" ContentID2",ContentType =" Type2",Definition = 2,HasProxy = true,IsActive = true,Length = 3,SystemName =" SysName2"}
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }; b $ b 返回新的SearchByContentIDResponse
{
SearchDataResult = new SearchDataResult
{
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; ExternalMetadata =数据
}
};
}
[WebMethod]
public SearchByContentIDResponse SearchByContentID(SearchByContentIDRequest request)
{
List<ExternalMetadata> data = new List<ExternalMetadata>
{
new ExternalMetadata { ContentID = "ContentID1", ContentType = "Type1", Definition = 1, HasProxy = false, IsActive = true, Length = 1, SystemName ="SysName1"},
new ExternalMetadata {ContentID = "ContentID2", ContentType = "Type2", Definition = 2, HasProxy = true, IsActive = true, Length = 3, SystemName ="SysName2"}
};
return new SearchByContentIDResponse
{
SearchDataResult = new SearchDataResult
{
ExternalMetadata = data
}
};
}
我必须根据集合多次从客户端并行调用它。我已使用"添加服务引用高级"按钮从客户端创建代理。
I have to call this from the client side parallel on several times based on a collection. I have created proxy from client, using the Add Service Reference Advanced button.
以下是代理类中的相关代码
Below is the relevant code from the proxy class
参考的.cs 强>
公共事件SearchByContentIDCompletedEventHandler SearchByContentIDCompleted;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
///< remarks />
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(QUOT; HTTP://tempuri.org/SearchByContentID" ;, RequestNamespace = QUOT; HTTP://tempuri.org/" ;, ResponseNamespace = QUOT; HTTP://tempuri.org /" ;,使用= System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;公共SearchByContentIDResponse SearchByContentID(SearchByContentIDRequest请求){
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;对象[]结果= this.Invoke(QUOT; SearchByContentID" ;,新的对象[] {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;请求});
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; return((SearchByContentIDResponse)(结果[0]));
}
///< remarks />
public void SearchByContentIDAsync(SearchByContentIDRequest request){
this.SearchByContentIDAsync(request,null);
}
///< remarks />
公共无效SearchByContentIDAsync(SearchByContentIDRequest请求,对象userState){
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; if((this.SearchByContentIDOperationCompleted == null)){
this.SearchByContentIDOperationCompleted =新System.Threading.SendOrPostCallback(this.OnSearchByContentIDOperationCompleted);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
this.InvokeAsync(" SearchByContentID",new object [] {
request},this.SearchByContentIDOperationCompleted,userState);
$
}
private void OnSearchByContentIDOperationCompleted(object arg){
if((this.SearchByContentIDCompleted!= null)){
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs =((System.Web.Services.Protocols.InvokeCompletedEventArgs)(ARG));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; this.SearchByContentIDCompleted(这一点,新SearchByContentIDCompletedEventArgs(invokeArgs.Results,invokeArgs.Error,invokeArgs.Cancelled,invokeArgs.UserState));
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; }
}
///< remarks />
public new void CancelAsync(object userState){
base.CancelAsync(userState);
}
public event SearchByContentIDCompletedEventHandler SearchByContentIDCompleted;
/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/SearchByContentID", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public SearchByContentIDResponse SearchByContentID(SearchByContentIDRequest request) {
object[] results = this.Invoke("SearchByContentID", new object[] {
request});
return ((SearchByContentIDResponse)(results[0]));
}
/// <remarks/>
public void SearchByContentIDAsync(SearchByContentIDRequest request) {
this.SearchByContentIDAsync(request, null);
}
/// <remarks/>
public void SearchByContentIDAsync(SearchByContentIDRequest request, object userState) {
if ((this.SearchByContentIDOperationCompleted == null)) {
this.SearchByContentIDOperationCompleted = new System.Threading.SendOrPostCallback(this.OnSearchByContentIDOperationCompleted);
}
this.InvokeAsync("SearchByContentID", new object[] {
request}, this.SearchByContentIDOperationCompleted, userState);
}
private void OnSearchByContentIDOperationCompleted(object arg) {
if ((this.SearchByContentIDCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.SearchByContentIDCompleted(this, new SearchByContentIDCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
public new void CancelAsync(object userState) {
base.CancelAsync(userState);
}
客户端
DbObjectList consolidatedList = new DbObjectList(); //这是一个DbObjects列表,一个cusom类
DbObjectList consolidatedList = new DbObjectList(); // This is a List of DbObjects, a cusom class
var distList = consolidatedList.GroupBy(x => x.CID).Select(y => y.First() );;
列表与LT; DBOBJECT> sortedList = consolidatedList.GroupBy(x => x.CID).Select(y => y.First())。ToList< DbObject>();
var distList = consolidatedList.GroupBy(x => x.CID).Select(y => y.First());
List<DbObject> sortedList = consolidatedList.GroupBy(x => x.CID).Select(y => y.First()).ToList<DbObject>();
我需要调用方法ExecuteDMIServiceAsync parellal for sortedList中的每个DBObject,并传递参数CID作为函数的输入。
I need to call the method ExecuteDMIServiceAsync parellal for each DBObject in the sortedList and pass the parametr CID as an input to the function.
public async Task< DbObjectList> ExecuteDMIServiceAsync(string Cid)
{
DMIService service = new DMIService();
service.SearchByContentIDCompleted + = DMIService_SearchByContentIDCompleted;
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; service.SearchByContentIDAsync(new SearchByContentIDRequest {CID = Cid});
我无法对此功能进行等待,因为SearchByContentIDAsync返回无效。
}
void DMIService_SearchByContentIDCompleted(object sender,SearchByContentIDCompletedEventArgs e)
{
抛出新的NotImplementedException();
$
}
public async Task<DbObjectList> ExecuteDMIServiceAsync(string Cid)
{
DMIService service = new DMIService();
service.SearchByContentIDCompleted += DMIService_SearchByContentIDCompleted;
service.SearchByContentIDAsync(new SearchByContentIDRequest { CID = Cid });
I am not able to do an await on this function as SearchByContentIDAsync is returning void.
}
void DMIService_SearchByContentIDCompleted(object sender, SearchByContentIDCompletedEventArgs e)
{
throw new NotImplementedException();
}
我的意图是使用Async,awit和Task.WhenAll。但现在坚持下去。
My intention was to use Async, awit and Task.WhenAll for this. But now stuck with it.
有人可以帮我完成这项活动吗?
Can some one help me to complete this activity?
谢谢
Tutu
推荐答案
如果您想使用await / async样式操作,那么你需要更改添加服务引用的方式:
If you want to use await/async style operations, you need to change the way Service Reference is added:
在高级页面上选择"生成基于任务的操作"。如果您没有看到此选项,则表示您未使用.NET 4.5或更高版本。
On advanced page choose 'Generate task-based operations'. If you don't see this option, then you not using .NET 4.5 or higher.
这篇关于多次异步调用同一Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!