`

wcf - Inheritance in callback contract

阅读更多

I have a service decorated with a ServiceContract attribute, and the interface definition is something like this:

    [ServiceContract(CallbackContract = typeof(ITabularPushCallback))]
    public interface ITabularPushService
    {
        [OperationContract(IsOneWay = true)]
        void Subscribe(int tableId);

        [OperationContract(IsOneWay = true)]
        void UnSubscribe(int tableId);

        [OperationContract(IsOneWay = false)]
        bool IsSubscribed(int tableId);

    }

the callback contract of the interface ITabularPushService is ITabularCallback, the definition of the interface is like this:

    public interface ITabularPushCallback
    {
        // this will ensure that the Datagram transport will be used, the Callback does not rquire that the server to block 
        // and this is far more effecient than Bidirectional connection
        [OperationContract(IsOneWay = true)]
        void NotifyMessage(object[][] messages, int tableId);
    }

 

 

Notice it is not necessary to decorate the callback contract with the [ServiceContract] attribute, because 

msdn 写道
you don't need the [ServiceContract] attribute on the callback interface. The callback interface is just "an extra bad of [OperationContract] methods", and not really a contract entity itself. Think of it as merely an auxiliary part of the [ServiceContract] 'forward' interface. Second, all of the inheritance is through the [ServiceContract] ('forward') interfaces. When reflecting on the type, WCF will look through the contract's type hierarchy for parent interfaces that have [ServiceContract], and also pick up those parents' CallbackContracts.

 

This is just an side note, let's get back to our question before.
Now suppose that we have a new requirement to add a new method to the ITabularCallback, something like the Server heartbeat, we don't want to directly extend the interface because we feel this is so general that we want to make a separate interface for the heartbeat. so we go ahead to implement the code as such.

    /// <summary>
    /// Server HeartBeat interface
    /// </summary>
    public interface IHeartbeat
    {
        /// <summary>
        /// Server heartbeat
        /// </summary>
        /// <param name="timestamp">heartbeat timestamp</param>
        [OperationContract(IsOneWay = true)]
        void Heartbeat(long timestamp);
    }

 

 

then we will make our ITabularCallback interface to inherit from the IHeartbeat, quick and easy, shouldn't it be ? however, you might be greeted with a NotSupportedException. The messgae may contains the following.

Callback method Heartbeat is not supported, this can happen if the method is not marked with OperationContractAttribute or if its interface type is not the target of the ServiceContractAttribute's CallbackContract.

 Huh?

 

So we figured out somehow that WCF does not walk the tree of interface and do reflection to get all Operatoin members. (For details, please see the References on topic Inheritance not supported on callback contracts?)


since as we have pointed out, the ServiceContract willl forward interface to WCF system, so we can leverage this point. we can make a marker interface and decorate that with a ServiceContract attribute, which shall binds to Callback we have in mind - IHeartbeat, we caller the new interface IHeartbeatService, it is an empty interface, which serves here only for its side effect of ServiceContract attribute to forward its callback contract. 

    // Check 
    //   http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/ef896836-dec1-4fa6-9956-e3a4958643ce
    [ServiceContract(CallbackContract = typeof(IHeartbeat))]
    public interface IHeartbeatService
    {
    }

 

 

Now we have to refine the Service and its contract together...

 

    [ServiceContract(CallbackContract = typeof(ITabularPushCallback))]
    public interface ITabularPushService : IHeartbeatService
    {
        // same as before
    }

 and the new ITabularCallback interface.

    public interface ITabularPushCallback  : IHeartbeat
    {
        // ... same as before.
    }

 

 

now that both the IHeartbeat and ITabularCallback is forwarded you will not worry about getting the NotSupported excetion.

 

 

Explain on the Design: 

Suppose that we have 

 

 

[ServiceContract(Namespace="foo", CallbackContract=typeof(IBaseCB))] 
interface IBaseC { ... }

interface IBaseCB : IBaseBaseCB { ... }

interface IBaseBaseCB { [OperationContract] void F(); }

 

 

If we have reflection which goes up the tree, then it will have a F method in the namespace foo.

Now, if we add more interface, such as 

 

[ServiceContract(Namespace="bar", CallbackContract=typeof(IBaseBaseCB))]
interface IOther { ... }

 
then we might have another F in namespace "bar".

 

Now, we refine as such 

interface IDerivedCB : IBaseCB { ...}

[ServiceContract(CallbackContract=typeof(IDerivedCB))]
interface IDerivedC : IBaseC, IOther { ... }

 

 

Then how can wcf know which namespace F now in IDerviedC is?

 

Some other tips:

1.  putting [ServiceContract] attribute on a callback contract

msdn 写道
Yes, [ServiceContract] will be ignored when being referenced from another [SC]'s CallbackContract. But you can indeed still put it there if the CC is also an SC. Thus is it possible to model two servers that talk to each other:

 

To model two servers

[ServiceContract(CallbackContract=typeof(IPong)] interface IPing { ... }
[ServiceContract(CallbackContract=typeof(IPing)] interface IPong { ... }

 

Or even make that to a single contract (Peerchannel does this a bit)

[ServiceContract(CallbackContract=typeof(ISelf)] interface ISelf { ... }

 

 

References:Inheritance not supported on callback contracts?

 

分享到:
评论

相关推荐

    HelloWCF--我的WCF第一个练习

    "HelloWCF--我的WCF第一个练习"是一个针对初学者的教程,旨在帮助首次接触WCF的开发者快速理解并掌握这项技术的基础应用。 在WCF项目创建中,通常会经历以下步骤: 1. **定义服务合同**:首先,我们需要定义服务...

    wpf-wcf-wf

    **WPF-WCF-WF整合开发实例** WPF(Windows Presentation Foundation)、WCF(Windows Communication Foundation)和WF(Windows Workflow Foundation)是微软.NET框架中三个关键的技术组件,它们各自负责不同的领域...

    WCF--DEMO_ GLD

    【标题】"WCF--DEMO_ GLD"是一个关于Windows Communication Foundation(WCF)的示例项目,其中“GLD”可能是项目或团队名称的一部分。这个DEMO着重展示了如何在GLD团队环境下创建和使用一个简单的WCF客户端代理类。...

    WCF-MessageContract-XmlSerializable DEMO

    总结来说,`WCF-MessageContract-XmlSerializable DEMO`是一个关于如何在WCF服务中利用`MessageContract`自定义消息结构,以及如何通过`IXmlSerializable`实现更复杂XML序列化的实例。这两个特性结合使用,可以帮助...

    WCF-Communication-data.rar_wcf

    在这个“WCF-Communication-data.rar_wcf”压缩包中,我们可以期待找到一系列关于WCF学习的资源。 WCF的核心概念包括服务、终结点、绑定和合同。服务是提供功能的实体,终结点是服务与外界交互的接口,绑定定义了...

    WCF--system.serviceModel配置属性说明[借鉴].pdf

    WCF--system.serviceModel配置属性说明 WCF(Windows Communication Foundation)是 Microsoft .NET Framework 中的一种技术,提供了一个统一的编程模型,用于构建分布式应用程序。WCF 配置文件是 WCF 应用程序的...

    WCF-CaterSystem.zip

    **WCF-CaterSystem.zip** 是一个包含使用Windows Communication Foundation(WCF)技术与WinForms集成的示例项目。这个项目旨在演示如何在Windows桌面应用(WinForm)中使用WCF服务来处理登录验证和获取人员信息。让...

    WCF-学习资料汇总

    Windows Communication Foundation(WCF)是微软.NET Framework的一部分,它提供了一种统一的编程模型来创建分布式应用程序,使得服务和客户端之间的通信变得更加简单。WCF旨在解决.NET Framework早期版本中的多种...

    Spring.net整合WCF-精简版

    **Spring.NET整合WCF在Windows Forms应用程序中的应用详解** 在.NET框架中,Spring.NET是一个流行的轻量级依赖注入(DI)容器,它提供了一种强大的方式来管理对象生命周期和实现松耦合。同时,Windows ...

    WCF-JSCallService跨域请求windows服务

    总的来说,"WCF-JSCallService跨域请求windows服务"这个主题涵盖了.NET WCF服务的配置、跨域策略的实施以及JavaScript客户端的AJAX请求,这些都是Web开发中实现跨域数据交互的重要技术。通过理解和实践这些知识,...

    WCF - LWCF + LServiceHost

    WCF 项目应用连载[4] - 自定义配置 扩展ServiceHost - LServiceHost WCF 项目应用连载[5] - 自定义配置 扩展ChannelFactory&lt;T&gt; - LDuplex ———————————————————————————————— WCF...

    WCF-Winform项目

    开发工具VS2013 一个简单的Demo解决方案实现以下需求: 多台Host根据XML配置指定某一地址为主服务地址 客户端优先连接主服务Host,当主服务机断连自动检测并连接分服务机,当主服务机恢复服务,自动连回主服务机

    gRPC-for-WCF-Developers_grpc_asp.netC#_

    **gRPC for WCF Developers: 从Web服务到现代微服务架构的跃迁** 随着技术的不断演进,Web服务的开发方式也在发生变化。Windows Communication Foundation (WCF)曾是.NET框架中的主流服务开发工具,但随着时间推移...

    Silverlight+WCF-图片上传-SLUploadPICDemo.zi

    《Silverlight结合WCF实现图片上传下载技术详解》 在当今的Web开发中,交互性和用户体验成为了关键要素,Microsoft的Silverlight技术凭借其丰富的图形渲染和媒体播放能力,为开发者提供了构建富互联网应用(RIA)的...

    NHibernate+WCF项目实战(二)使用NHibernate实现数据访问并进行单元测试 - WCF - 拼吾爱程序人生.mht

    NHibernate+WCF项目实战

    什么是WCF-WFC详解

    Windows通信基础(Windows Communication Foundation,WCF)是基于Windows平台下开发和部署服务的软件开发包(Software Development Kit,SDK)。

    Wcf-cors-behavior:WCF处理Cors的示例行为

    本篇将深入探讨如何在WCF中实现CORS行为,并提供一个示例项目——"Wcf-cors-behavior"的解析。 首先,理解CORS的基本原理是至关重要的。CORS通过在HTTP头部添加`Access-Control-Allow-Origin`字段,允许服务器指定...

    WCF学习资料_DOC文档

    WCF学习资料_DOC文档,从基础到复杂的介绍WCF知识;wcf学习之端点绑定;wcf学习之服务契约;wcf学习之异步调用;wcf学习之异常处理;WCF - ChannelFactory;WCF - Callback等等

    C# WCF CallBack Demo

    **C# WCF CallBack Demo** Windows Communication Foundation (WCF) 是.NET Framework中用于构建分布式应用程序的服务模型。在WCF中,回调(Callback)是一种高级通信模式,它允许服务主动向客户端发送数据,而不...

Global site tag (gtag.js) - Google Analytics