ChannelFactory<T>
有一個虛擬方法CreateChannel()
。如果這不被覆蓋,它會使用動態代碼生成,這在MonoTouch上會失敗。
解決方案是覆蓋它並提供您自己的編譯時實現。
下面是我的舊服務實現,至少用於在MonoTouch上工作。我將它分成2個部分類 - 第一個在所有構建中鏈接,第二個在iOS構建中(允許動態生成機制仍然可以在Windows上工作)。
我已經剝離它只包含1服務電話。
TransactionService.cs:
public partial class TransactionService : ClientBase<IConsumerService>, IConsumerService
{
public TransactionService()
{
}
public TransactionService(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public TransactionService(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public TransactionService(string endpointConfigurationName, EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public TransactionService(Binding binding, EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public AccountBalanceResponse GetAccountBalance(AccountBalanceQuery query)
{
return Channel.GetAccountBalance(query);
}
}
TransactionService.iOS.cs: ConsumerServiceClientChannel
,其執行通過反射調用)
public partial class TransactionService
{
protected override IConsumerService CreateChannel()
{
return new ConsumerServiceClientChannel(this);
}
private class ConsumerServiceClientChannel : ChannelBase<IConsumerService>, IConsumerService
{
public ConsumerServiceClientChannel(System.ServiceModel.ClientBase<IConsumerService> client) :
base(client)
{
}
// Sync version
public AccountBalanceResponse GetAccountBalance(AccountBalanceQuery query)
{
object[] _args = new object[1];
_args[0] = query;
return (AccountBalanceResponse)base.Invoke("GetAccountBalance", _args);
}
// Async version
public IAsyncResult BeginGetAccountBalance(AccountBalanceQuery query, AsyncCallback callback, object asyncState)
{
object[] _args = new object[1];
_args[0] = query;
return (IAsyncResult)base.BeginInvoke("GetAccountBalance", _args, callback, asyncState);
}
public AccountBalanceResponse EndGetAccountBalance(IAsyncResult asyncResult)
{
object[] _args = new object[0];
return (AccountBalanceResponse)base.EndInvoke("GetAccountBalance", _args, asyncResult);
}
}
}
編輯:我只是用最新的測試此MT(5.2) - 它不再需要以前的那些額外的鍋爐板,只需要CreateChannel()覆蓋。我已經清理了示例代碼來匹配。
EDIT2:我添加了一個異步方法實現。
感謝您的分享,我現在嘗試。 – BlueSky 2012-04-08 05:26:25
您的解決方案已成功完成,謝謝。 – BlueSky 2012-04-08 07:51:37
這裏說它無法找到類ChannelBase。我不明白我可以如何在異步方法中使用它。謝謝! – cheeesus 2012-04-16 11:36:14