c# - 我们可以在抽象类上使用服务契约吗

标签 c# wcf abstract-class serviceknowntype

我们可以在抽象类而不是接口(interface)上使用服务契约属性吗?

    public class Service1 : AbstractService
    {
        public override string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }

    [ServiceContract]
    public abstract class AbstractService
    {
        [OperationContract]
        public abstract string GetData(int value);

        [OperationContract]
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }

配置:

    <system.serviceModel>
        <services>
            <service name="WcfService1.Service1" behaviorConfiguration="WcfService1.Service1Behavior">
                <!-- Service Endpoints -->
                <endpoint address="" binding="wsHttpBinding" contract="WcfService1.AbstractService">
                    <!--
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          -->
                    <identity>
                        <dns value="localhost"/>
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WcfService1.Service1Behavior">
                    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
                    <serviceMetadata httpGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

如果不可能,那么我正在寻找原因。我得到了答案,但我不清楚答案。

第一个答案

您可以在抽象类上使用服务契约,但在添加引用时它会给您一个错误,显示“如果一个类被标记为 ServiceContractAttribute,则另一个服务类不能从它派生”。这是非常合乎逻辑的,就好像它允许调用哪个派生类方法一样。

这个语句不清楚那么另一个服务类不能从它派生。这很符合逻辑,好像它允许那么它应该调用哪个派生类方法。

第二个答案

如果我们在数据契约(Contract)中包含 [ServiceKnownType](在服务契约(Contract)中)或 [KnownType],它将起作用。基本上我们必须告诉 WCF 要序列化/反序列化什么。

另一个人说如果我们使用[ServiceKnownType](在服务契约上)或[KnownType]在数据契约上是可能的

寻找更多的见解,如果不可能,那为什么不可能。如果可能的话,用示例场景和示例代码进行解释。谢谢

最佳答案

如果你使用抽象类,它会编译,但在运行时会抛出异常

Inheritance can only be used among interface types.If a class is marked with ServiceContractAttribute, then another service class cannot derive from it

您可以将普通类与 ServiceContractAttribute 一起使用。

关于c# - 我们可以在抽象类上使用服务契约吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22917251/

相关文章:

c# - WP7 : Styling ListBox items

c# - XAML 绑定(bind)到复杂值对象

c# - 使用 MVC3 和 EF4 CTP5w 进行 JSON 序列化的循环引用异常

c# - 作为 WebService 使用的 WCF 添加了一个 bool 参数?

c# - 有没有办法在没有泛型约束的情况下强制执行无参数构造函数

c# - ExecuteNonQueryAsync 并在 SQL 事务中提交

java - 轴2 : Avoid "Unexpected subelement" error when non-required property added to WSDL

c# - 如何更改 WCF 服务的默认显示

generics - 泛型类型与抽象类/接口(interface)

c++ - 如何在 C++ 中实现内部抽象成员类?