我试图用netttcpbinding创建一个WCF回调服务。当我尝试调用服务的方法时,我得到以下异常:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll
Additional information: The InstanceContext provided to the ChannelFactory contains a UserObject that does not implement the CallbackContractType 'Client.WCFService.IHostFunctionsCallback'.
我添加了一个服务参考,而不是使用SvcUtil.exe我已经在网上搜索解决这个问题,但我还没有找到一个解决方案。
这是我的实现:
IHostFunctions.cs(部分的HostLibrary)
using System.ServiceModel;
namespace HostLibrary
{
[ServiceContract(CallbackContract = typeof(ICallback))]
public interface IHostFunctions
{
[OperationContract]
void OpenSession();
}
}
ICallback.cs(部分的HostLibrary)
using System.ServiceModel;
namespace HostLibrary
{
public interface ICallback
{
[OperationContract]
void OnCallback();
}
}
HostFunctions.cs(部分的HostLibrary)
using System;
using System.ServiceModel;
using System.Timers;
namespace HostLibrary
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class HostFunctions : IHostFunctions
{
#region Implementation of IHostFunctions
public static ICallback Callback;
public static Timer Timer;
public void OpenSession()
{
Console.WriteLine("> Session opened at {0}", DateTime.Now);
Callback = OperationContext.Current.GetCallbackChannel<ICallback>();
Timer = new Timer(1000);
Timer.Elapsed += OnTimerElapsed;
Timer.Enabled = true;
}
void OnTimerElapsed(object sender, ElapsedEventArgs e)
{
Callback.OnCallback();
}
#endregion
}
}
Callback.cs (Part of Client)
using System;
using HostLibrary;
namespace Client
{
public class Callback : ICallback
{
#region Implementation of ICallback
public void OnCallback()
{
Console.WriteLine("> Received callback at {0}", DateTime.Now);
}
#endregion
}
}
服务的Program.cs
using System;
using System.ServiceModel;
using HostLibrary;
namespace WCF_TCP_Callbacks
{
internal static class Program
{
private static void Main(string[] args)
{
using (var sh = new ServiceHost(typeof (HostFunctions)))
{
sh.Open();
Console.WriteLine("Service started.");
Console.ReadLine();
Console.WriteLine("Stopping service...");
sh.Close();
}
}
}
}
客户端的Program.cs
using System;
using System.Globalization;
using System.ServiceModel;
using System.Threading;
using Client.WCFService;
namespace Client
{
internal class Program
{
private static void Main(string[] args)
{
var callback = new Callback();
using (var proxy = new HostFunctionsClient(new InstanceContext(callback)))
{
proxy.OpenSession();
}
Console.ReadLine();
}
}
}
代码来自http://adamprescott.net/2012/08/15/a-simple-wcf-service-callback-example/,但是使用了netttcpbinding。
默认情况下,WCF将尝试使用可用的SynchronizationContext进行分派。这个回调的问题是UI线程已经在出站调用中被阻塞了。因此,对于调度调用,我们需要告诉WCF不要使用SynchronizationContext——同样使用CallbackBehavior属性:
[CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant, UseSynchronizationContext=false)]
public class Callback : ICallback
{
....
}
要了解更多细节,请查看此链接http://www.dotnetconsult.co.uk/weblog2/PermaLink,guid,b891610a-6b78-4b54-b9a6-4ec81c82b7c0.aspx还有一篇文章进一步描述了它http://stefanoricciardi.com/2009/08/28/file-transfer-with-wcp/
我通过简单地将类ICallback
重命名为IHostFunctionsCallback
来解决这个问题。
我仍然不知道为什么现在这个工作,因为我以前没有使用IHostFunctionsCallback
。
我知道帖子是旧的:你的回调类的真正名字是什么?
你发布的代码是这样写的:
Callback : ICallback
错误信息是这样说的:CallbackContractType Client.WCFService.IHostFunctionsCallback"
所以Callback是按照你上面的代码,或者它真的定义为:
Client.WCFService.IHostFunctionsCallback
我会说你不正确地修饰了回调通道的属性引用,或者继承了错误的回调。搜索你的项目,确保你正确地命名了所有的东西。
编辑至于为什么修复工作和发生了什么:我为别人的情况作了回答。如果你在一个团队环境中,有人将Callback类接口的名称从通用改为更容易理解的名称——在WCF中,这就是你的userobject——这就是契约。您可能曾经使用Visual Studio生成过客户端或服务。这也会把事情搞砸,这就是我所看到的,因为命名约定遵循IService[Callback]。