我在SO,MSDN和Code Project上阅读过很多文章。我的答案仍然没有。
我创建了一个具有回调契约的WCF服务,以及一个实现回调接口的Windows窗体应用程序(测试工具)。
以下是接口:
[ServiceContract]
public interface ICacheService
{
[OperationContract]
bool AddToCache(string type, string key, string source, object item, int duration);
[OperationContract]
bool Remove(string key);
}
[ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(ISearchCallback))]
public interface ICacheSearch
{
[OperationContract(IsOneWay=true)]
void SearchCache(string source);
}
public interface ISearchCallback
{
[OperationContract(IsOneWay=true)]
void OnCallback(object data);
}
我的服务(省略了添加和删除以节省空间 - 它们按预期工作):
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
public class CachingService : ICacheService, ICacheSearch, IDisposable
{
private List<ICache> _cacheList = new List<ICache>();
private ISearchCallback _callback = null;
public void SearchCache(string source)
{
try
{
_callback = OperationContext.Current.GetCallbackChannel<ISearchCallback>();
List<object> items = new List<object>();
foreach (ICache cache in this._cacheList)
{
foreach (CacheItem item in cache.GetCacheItemsBySource(source))
{
items.Add(item.Value);
}
}
ReadOnlyCollection<object> outList = new ReadOnlyCollection<object>(items);
_callback.OnCallback(outList);
}
catch { }
}
}
我的服务托管在Windows服务中。这是app.config中的system.serviceModel部分:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding
name="WSHttpBinding_ICacheService"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:01:00"
sendTimeout="00:01:00"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</wsHttpBinding>
<wsDualHttpBinding>
<binding
name="WSDualHttpBinding_ICacheSearch"
closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="00:01:00"
sendTimeout="00:01:00"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"
clientBaseAddress="http://localhost:5057/wsDualHttpBinding"
useDefaultWebProxy="true"
transactionFlow="false">
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</wsDualHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="CacheService.CachingService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:5055/CachingService/"/>
</baseAddresses>
</host>
<endpoint address="Search" binding="wsDualHttpBinding" contract="CacheService.ICacheSearch" />
<endpoint address="" binding="wsHttpBinding" contract="CacheService.ICacheService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<client>
<endpoint address="http://localhost:5055/CachingService/Search" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_ICacheSearch" contract="CacheService.ICacheSearch"
name="WSDualHttpBinding_ICacheSearch" />
<endpoint address="http://localhost:5055/CachingService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICacheService" contract="CacheService.ICacheService"
name="WSHttpBinding_ICacheService" />
</client>
</system.serviceModel>
最后,我创建了一个简单的Windows窗体应用程序进行测试。它被声明为:
[CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Single)]
public partial class Form1 : Form, cacheService.ICacheSearchCallback
{
InstanceContext context = null;
cacheService.CacheServiceClient _service = null;
cacheService.CacheSearchClient _proxy = null;
private void Form1_Load(object sender, EventArgs e)
{
_syncContext = System.Threading.SynchronizationContext.Current;
_service = new cacheService.CacheServiceClient();
this.dataGridView1.AutoGenerateColumns = true;
context = new InstanceContext(this);
_proxy = new cacheService.CacheSearchClient(context);
RefreshList();
}
private void RefreshList()
{
_proxy.SearchCache("mySource");
}
public void OnCallback(object data)
{
this._data = data;
this.dataGridView1.DataSource = this._data;
}
对服务的调用(_proxy.SearchCache(“mySource”))成功执行服务和服务调用_callback.OnCallback(outList)上的方法。但是我的测试应用程序中实现的回调方法永远不会被调用,因此数据永远不会传递给测试应用程序。
同样,我查看了有关SO和许多其他博客和代码网站的最新WCF文章。我觉得我需要另外一双眼睛指出明显的东西,以便我可以重新开始工作。
非常感谢,
罗伯特