C# WCF回调不回拨
我在这里读了很多关于SO、MSDN和代码项目的文章。我仍然找不到答案 我创建了一个具有回调契约的WCF服务,以及一个实现回调接口的Windows窗体应用程序(测试工具) 以下是接口:C# WCF回调不回拨,c#,wcf,callback,C#,Wcf,Callback,我在这里读了很多关于SO、MSDN和代码项目的文章。我仍然找不到答案 我创建了一个具有回调契约的WCF服务,以及一个实现回调接口的Windows窗体应用程序(测试工具) 以下是接口: [ServiceContract] public interface ICacheService { [OperationContract] bool AddToCache(string type, string key, string source, object item, int durati
[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);
}
我的服务(省略添加和删除以节省空间-它们按预期工作):
对服务的调用(_proxy.SearchCache(“mySource”))成功地对服务执行了方法,并对服务调用了_callback.OnCallback(outList)。但是,在我的测试应用程序中实现的回调方法从未被调用,因此数据从未传输到测试应用程序
再一次,我查看了SO和许多其他博客和代码站点上最近的每一篇WCF文章。我觉得我需要另一双眼睛来指出显而易见的事情,这样我才能回去工作
非常感谢,
罗伯特只是为了测试一下。你能运行这个代码吗代码>在刷新列表中?我怀疑你的线程已被阻止,无法处理你的回调。我仍在使用.NET 4,但使用System.Threading.Tasks.Task.Factory.StartNew(()=>\u proxy.SearchCache(“mySource”);毫无例外地运行。然后呢?您的回调方法现在被调用了吗?在向缓存中添加一个项(该项调用RefreshList()方法)之后,我得到了以下结果:没有端点侦听可以接受消息。这通常是由不正确的地址或SOAP操作引起的。有关更多详细信息,请参阅InnerException(如果存在)。内部异常:无法建立连接,因为目标计算机主动拒绝了它127.0.0.1:5055。舞台调度?
[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 { }
}
}
<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>
[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;
}