我们可以在抽象类而不是接口(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 一起使用。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

C# Excel 自动化仅在 Excel 可见时起作用