Interface (s) inheriting other interfaces (interfaces) in WCF services

There are several WCF services in my solution, each of which implements its own callback interface. Let them say what they are called Subscribe1, with ISubscribe1and ICallback1etc.

It happens that there are several methods between ICallbacks, so I made the following interface:

interface ICallback
{
    [OperationContract]
    void CommonlyUsedMethod();
}

I inherited it all: ICallback1 : ICallback, ICallback2 : ICallbacketc. And removed CommonlyUsedMethod()from all callback interfaces.

Now, on the service code, everything compiles fine, and the services can start as usual. But when I updated the service links for the client, CommonlyUsedMethod () disappeared from the reference.cs file (part of ISubscribeCallback) and can no longer be used to send data back to the client.

+3
source share
4 answers

Well, this is the exact code, I condensed it as much as possible. Just launch a new console application and copy / paste it. Run it and add a link to the service. CommonlyUsedMethod () is missing from the link, while other methods. Could this be frame 4?

using System;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace TestService
{
    class Program
    {
        static void Main()
        {
            var serviceHost=new ServiceHost(typeof(Subscribe1), new Uri("net.tcp://localhost:8888"));
            serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
            serviceHost.AddServiceEndpoint(typeof(ISubscribe1), new NetTcpBinding(SecurityMode.None), string.Empty);
            serviceHost.AddServiceEndpoint("IMetadataExchange", MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
            serviceHost.Open();

            Console.WriteLine("Working!");
            while(Console.ReadKey(true).Key!=ConsoleKey.Escape) { }
        }
    }

    [ServiceContract]
    interface ICallbackBase
    {
        [OperationContract]
        void CommonlyUsedMethod();
    }

    [ServiceContract]
    interface ICallback1 : ICallbackBase
    {
        [OperationContract]
        void SpecificMethod();
    }

    [ServiceContract(CallbackContract=typeof(ICallback1))]
    interface ISubscribe1
    {
        [OperationContract]
        void TestMethod();
    }

    [ServiceBehavior]
    class Subscribe1 : ISubscribe1
    {
        [OperationBehavior]
        public void TestMethod()
        {
        }
    }
}
+1
source

try also setting the ServiceContract attribute in the base interface.

+4
source

, ?

[ServiceContract]
public interface ICallbackBase
{
    [OperationContract]
    void CommonlyUsedMethod();
}

[ServiceContract]
public interface ICallback1 : ICallbackBase
{
    [OperationContract]
    void SpecificMethod();
}

, , -, , .

, :

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="MyNamespace.ICallback1")]
public interface ICallback1 {

    [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ICallbackBase/CommonlyUsedMethod", ReplyAction="http://tempuri.org/ICallbackBase/CommonlyUsedMethodResponse")]
    void CommonlyUsedMethod();

}

"ICallbackBase" OperationContractAttribute - , .

0

I'm not sure what you are trying to do with WCF is possible. When you use inheritance in WCF, you need to apply the KnownType attribute to the DataContract so that the DataContractSerializer knows what it serializes and makes available on the other end. Since you cannot put the KnownType attribute on interfaces, there is no way to tell the serializer that this is necessary on the other end. Thus, it does not appear when you implement it on the client.

0
source

Source: https://habr.com/ru/post/1747967/


All Articles