【发布时间】:2011-03-29 07:59:43
【问题描述】:
我有一个使用 web service software factory 创建的 WCF 服务。我所有的传入请求和返回的响应都作为消息契约实现。我一直在通过在 VS 中添加服务引用来访问该服务,效果很好。然而,我现在被告知这不是标准的公司方式,我需要使用基于 System.ServiceModel.ChannelFactory 的内部 WCF 代理帮助程序类来调用服务。
我以前从未使用过 ChannelFactory,我正在努力找出重写客户端访问的最佳方法。服务契约是由服务工厂生成的,我需要向代理助手类提供服务接口。这是服务合同的一部分——服务上总共有大约 25 个操作:
namespace CAEWCFService.ServiceContracts
{
/// <summary>
/// Service Contract Class - CAEServiceContract
/// </summary>
[WCF::ServiceContract(Namespace = "urn:CAEServiceServiceContracts", Name = "CAEServiceContract", SessionMode = WCF::SessionMode.Allowed, ProtectionLevel = ProtectionLevel.None )]
public partial interface ICAEServiceContract
{
[WCF::OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = "GetAllIncomes", ProtectionLevel = ProtectionLevel.None)]
CAEService.MessageContracts.GetAllIncomesReponse GetIncomeRecords(CAEService.MessageContracts.GetAllIncomesRequest request);
[WCF::OperationContract(IsTerminating = false, IsInitiating = true, IsOneWay = false, AsyncPattern = false, Action = "urn:CAEServiceServiceContracts.CAEServiceContract.GetSecurityRecords", ProtectionLevel = ProtectionLevel.None)]
CAEService.MessageContracts.GetAllSecuritiesResponse GetSecurityRecords(CAEService.MessageContracts.GetAllSecuritiesRequest request);
//... the rest
}
}
这是来自客户端调用 GetIncomeRecords 操作的调用:
CAEServiceContractClient client = new CAEServiceContractClient();
IncomeSearchItem item = new IncomeSearchItem()
{
aCode = aCode.Text,
bCode = bCode.Text,
sCode = sCode.Text,
secDesc = tbSecurityName.Text,
status = statusValue != 0 ? statusValue : 0,
fromDate = fromDate,
toDate = toDate,
IncomeID = 0,
userBank = userBank,
userId = userId,
currentRate = currentSourceVal != 0 ? currentSourceVal : 0,
dateConfirmed = ddDate.SelectedValue,
pageIndex = pageIdx,
pageSize = recordsPerPage
};
GetAllIncomesReponse response = new GetAllIncomesReponse();
try
{
response = client.GetIncomeRecords(item);
totalRecords = response.TotalRecords;
incomelist.DataSource = response.IncomeItemColl;
incomelist.DataBind();
//.. the rest
}
这里有一些来自另一个项目的示例代码,展示了如何使用代理助手。我的服务的问题在于它期望并返回消息契约,而示例传递原始类型并返回原始类型或数据集:
public static class ServiceHelper
{
/// <summary>
/// Services the verification.
/// </summary>
/// <param name="user">The user.</param>
/// <returns></returns>
public static string ServiceVerification(string user)
{
return WCFProxyHelper<IFCAServices>.Use(client =>
client.ServiceVerification(user));
}
}
那么最快/最简单的方法是什么?如何让 ChannelFactory 知道我的 IncomeSearchItem 和我需要传递给服务的其他几个对象?我可以/应该使用 svcutil 生成所有对象并在 ChannelFactory 中使用吗?
【问题讨论】:
标签: wcf wcf-client