定义个帮助类,初始化连接客户端
public class WebServiceMethod
{
public DependencylibServiceClient DependencylibClient { set; get; }
public void InitDependencylibService(string url)
{
DependencylibClient = ServiceLoader.CreateWCFClient<DependencylibServiceClient>(new Uri(url));
}
/// <summary>
/// 动态调用WCF的工具类库
/// </summary>
public class ServiceLoader
{
public static T CreateWCFClient<T>(Uri uri)
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None)
{
MaxReceivedMessageSize = 0x7fffffffL,
MaxBufferSize = 0x7fffffff,
SendTimeout = TimeSpan.FromMinutes(1.0),
ReceiveTimeout = TimeSpan.FromMinutes(1.0),
TransferMode = TransferMode.Buffered
};
CustomBinding binding2 = new CustomBinding(binding);
EndpointAddress address = new EndpointAddress(uri, new AddressHeader[0]);
object obj2 = Activator.CreateInstance(typeof(T), new object[] { binding2, address });
if (obj2 is T)
{
return (T)obj2;
}
return default(T);
}
}
}
使用这个帮助类进行wcf的调取使用工作
WebServiceMethod webservice = new WebServiceMethod();
//初始化wcf服务的公共类
webservice.InitDependencylibService("http://localhost:24666/DependencylibService.svc");
webservice.DependencylibClient.GetB0Completed += (sender, e) => {
string b = e.Result.B0_ASSETCLASS;
string c = "s";
};
webservice.DependencylibClient.GetB0Async(new B0_ASSET());