如何将泛型接口中的方法传递到泛型类中



我下面有这段代码(可能实现错误(,我一直在尝试创建一种使用多个web服务的通用方法(以减少代码冗余(。到目前为止一切都很好,直到最后一行的"ClaimGetAsync(("。

public static class Test {
public static void TestWebServiceA() {
var ws = new WebService<ARMDevelopment.WebServiceAWI>();
ws.GetClaim(new ARMDevelopment.WebServiceAWIClient(), new ARMDevelopment.User(), 
"https://trustonline.delawarecpf.com/tows/webserviceawi.svc", "userName", "password");  
}
public static void TestWebServiceB() {
var ws = new WebService<BWDevelopment.WebServiceBW>();
ws.GetClaim(new BWDevelopment.WebServiceBWClient(), new BWDevelopment.User(), 
"https://trustonline.delawarecpf.com/tows/webservicebw.svc", "userName", "password");  
}
}
public class WebService<T> {             
public async void GetClaim<TOne, TTwo>(TOne entity1, TTwo entity2, string url, string userName, string password) 
where TOne : IWebServiceClient, new() // class, new()
where TTwo : IUser, new()  // limits the TTwo class to implement IUser -> IUser created by me   
{
TOne webServiceClient = new TOne(); // entity1;
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
EndpointAddress endpoint = new EndpointAddress(url);
ChannelFactory<T> channelFactory = new ChannelFactory<T>(binding, endpoint);
TTwo user = new TTwo(); // entity2;
user.UserName = await entity1.EncryptValueAsync(userName);
user.Password = await entity1.EncryptValueAsync(password);
T wcfClient = channelFactory.CreateChannel();
var response = wcfClient.ClaimGetAsync(user, 12345); 
}
}

我创建的IUser和IWebServiceClient以及web服务文件中的User和WebServiceClient作为部分继承它们。

public interface IUser {
string UserName { get; set; }
string Password { get; set; }
}
public interface IWebServiceClient
{
Task<string> EncryptValueAsync(string text);
}

但现在方法中的最后一行无法发现"ClaimGetAsync",因为wcfClient是一个通用的T

var response = wcfClient.ClaimGetAsync(user, 12345);

如何让泛型查找/发现ClaimGetAsync((以便使用它?

UPDATE-一位成员要求我发布一些从web服务util svcutil.exe生成的接口代码:这是一个来自AWI网络服务的,我还有大约20个来自我们从这个提供商使用的所有其他服务

namespace WebServices_Development_TrustOnline_ARM
{
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.1")]
[System.ServiceModel.ServiceContractAttribute(Namespace="urn:trustonline", ConfigurationName="WebServices_Development_TrustOnline_ARM.WebServiceAWI")]
public interface WebServiceAWI
{
[System.ServiceModel.OperationContractAttribute(Action="urn:trustonline/EncryptValue", ReplyAction="urn:trustonline/EncryptValue")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
System.Threading.Tasks.Task<string> EncryptValueAsync(string text);
}
[System.ServiceModel.OperationContractAttribute(Action="urn:trustonline/ClaimGet", ReplyAction="urn:trustonline/ClaimGet")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults=true)]
[return: System.ServiceModel.MessageParameterAttribute(Name="webserviceresult")]
System.Threading.Tasks.Task<WebServices_Development_TrustOnline_ARM.WebServiceResult> ClaimGetAsync(WebServices_Development_TrustOnline_ARM.User user, int claimnumber);
}
public interface IClaimsService
{
Task<IEnumerable<Claim>> ClaimGetAsync<TUser>(TUser user, int claimsNumber);
}
public class WebService<T> where T : IClaimsService
{
}

相关内容

  • 没有找到相关文章

最新更新