WCF复式合约

发布于 2024-10-22 01:38:46 字数 1230 浏览 4 评论 0原文

假设我有这样的 WCF 服务契约

[ServiceContract(CallbackContract = typeof(ICallback1),
SessionMode = SessionMode.Required)]
public interface IService1
{
  // some methods
}

服务实现为 InstanceContextMode 设置了 InstanceContextMode.Single 和 ICallback1 类似

public interface ICallback1
{
    [OperationContract]
    void Report(int someValue);
}

现在在客户端,我可以有一个实现 ICallback1 的类

class Callback1 : ICallback1
{
    public void Report(int someValue)
    {
        // alert client
    }
}

,并且我创建这样的客户端服务引用

Service1Client serviceClient = new Service1Client(new InstanceContext(new CallBack1())); 

,效果很好。现在的问题是,我有一些客户端对回调不感兴趣,所以我认为我不需要为这些客户端实现回调接口,所以我尝试了这个

 Service1Client serviceClient = new Service1Client(null);

,并且

 Service1Client serviceClient = new Service1Client(new InstanceContext(null));

都报告参数不能为空。我的问题是,如果客户端对回调不感兴趣,如何在不传递回调对象的情况下创建服务引用。唯一的要求是所有客户端都应该与相同的服务通信,但否则我可以重组服务。有什么想法吗?

编辑:

我还尝试了 ServiceContract 的 SessionMode = SessionMode.Allowed 而不是 SessionMode.Required 但这也没有帮助。

Say I have WCF service contract like this

[ServiceContract(CallbackContract = typeof(ICallback1),
SessionMode = SessionMode.Required)]
public interface IService1
{
  // some methods
}

The service implementation has InstanceContextMode.Single set for InstanceContextMode
and ICallback1 is something like

public interface ICallback1
{
    [OperationContract]
    void Report(int someValue);
}

Now on client side, I can have a class the implements ICallback1 like

class Callback1 : ICallback1
{
    public void Report(int someValue)
    {
        // alert client
    }
}

and I create client service reference like this

Service1Client serviceClient = new Service1Client(new InstanceContext(new CallBack1())); 

which works fine. Now the problem is that I have some clients that are not interested in the callback so I figured I do not require to implement the callback interface for such clients so I tried this

 Service1Client serviceClient = new Service1Client(null);

and

 Service1Client serviceClient = new Service1Client(new InstanceContext(null));

both reported that the parameter cannot be null. My question is, how can i create a service reference without passing a callback object if the client is not interested in the callback. The only requirement is that all clients should be talking to the same service but otherwise I can restructure the service however. Any thoughts ?

EDIT:

I have also tried SessionMode = SessionMode.Allowed for ServiceContract instead of SessionMode.Required but that didn't help either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拿命拼未来 2024-10-29 01:38:46

解决方法:从 IService1 中删除 CallbackContract。创建继承 IService1 并包含 CallbackContract 的 IDuplexService1。让 Service1Client 实现 IDuplexService1。实例化主机时,为 IService1 和 IDuplexService1 调用 ServiceHost.AddServiceEndpoint

Workaround: Remove the CallbackContract from IService1. Create IDuplexService1 which inherits IService1 and contains the CallbackContract. Have Service1Client implement IDuplexService1. When instantiating the host, call ServiceHost.AddServiceEndpoint for both IService1 and IDuplexService1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文