【问题标题】:Can we use Service contract on abstract class我们可以在抽象类上使用服务契约吗
【发布时间】:2014-04-07 15:54:05
【问题描述】:

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

    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 标记,那么另一个服务类不能从它派生”。这很合乎逻辑,好像它允许它应该调用哪个派生类方法。

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

第二个答案

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

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

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

【问题讨论】:

  • 这是一个有趣的问题。您是否尝试过在 Visual Studio 中设置一个小项目来看看会发生什么?
  • 嘿@mateuszmcz 你给的链接是关于抽象类和数据合同的讨论,但我正在寻找抽象类和服务合同。告诉我为什么不能使用抽象类而不是接口作为服务合同,因为我搜索谷歌没有正确解释。

标签: c# wcf abstract-class serviceknowntype


【解决方案1】:

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

继承只能在接口类型之间使用。如果一个类被标记了ServiceContractAttribute,那么另一个服务类不能从它派生

您可以使用带有 ServiceContractAttribute 的普通类。

【讨论】:

    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 2012-01-23
    • 2020-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多