我有一個使用web service software factory創建的WCF服務。我所有的傳入請求和返回的響應都是作爲消息合約實現的。我一直通過在VS中添加服務引用來訪問該服務,該服務工作正常。不過,我現在被告知,這不是標準的公司方式,我需要使用基於System.ServiceModel.ChannelFactory的內部WCF代理輔助類來調用該服務。如何重寫WCF客戶端使用ChannelFactory而不是服務引用
我以前從未使用過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中使用它?
嗨@Ian,公司的方式是使用服務助手,使用預先構建的WCF代理幫助對象。擴展方法可以工作,但我如何重新創建或使客戶端意識到GetAllIncomesRequest和其他對象?當我刪除服務參考時,它們將消失。 – 2011-03-29 09:19:56