【发布时间】:2026-02-13 17:10:01
【问题描述】:
我有一个 WFC(4.0) 服务 dll,我必须为其创建一个主机应用程序。该dll非常简单,具有以下接口:
[ServiceContract(CallbackContract=typeof(IChatServiceCallback))]
public interface IService {
[OperationContract]
Guid Subscribe();
}
public interface IServiceCallback {
void NotifyClient(strign message);
}
当我尝试创建服务时,我的问题就出现了。当我创建从客户端到主机的通道时,出现以下异常:“提供给 ChannelFactory 的 InstanceContext 包含一个未实现 CallbackContractType 'Client.MyServiceReference.IServiceCallback' 的 UserObjecct。”
我发现在ServiceReference对象浏览器中ClientObject不包含IServiceCallback接口。这是我对应的主机代码:
ServiceHost host = new ServiceHost(typeof(ChatService));
try
{
host.BeginOpen(new AsyncCallback(OnOpen), host);
mre.WaitOne();
if (host.State == CommunicationState.Opened)
{
Console.WriteLine("Server is running!\nServer listens on the following endpoints:");
foreach (var endp in host.Description.Endpoints)
{
Console.WriteLine("\t{0}", endp.Address);
}
Console.WriteLine("Press <Enter> to stop the server...");
Console.ReadLine();
host.Close();
}
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
如何为包含在客户端实现的回调接口的服务 dll 创建主机应用程序?
客户端: 尝试 { clientID = client.Subscribe();
【问题讨论】: