0
votes

I'm trying to upgrade an existing WCF service from VS2015 to VS2019, but there seems a problem with interface/class service attributes in VS2019.

I've created the following short code example to demonstrate the problem here:

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

namespace ConsoleApp25
{
    [ServiceContract]
    public interface ISOAAdd
    {
        [OperationContract]
        double Add(double a, double b);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class SOAAddImpl: ISOAAdd
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
    }

    class Program
    {
        private static ServiceHost Host;
        private const ushort Port = 23456;

        static void Main(string[] args)
        {
            Type ServiceType = typeof(SOAAddImpl);            
            Host = new ServiceHost(ServiceType, new Uri[] { new Uri(string.Format("http://localhost:{0}/", Port)) });
            Host.AddServiceEndpoint(ServiceType, new BasicHttpBinding(), "SOAAddImpl");
            ServiceMetadataBehavior Behavior = new ServiceMetadataBehavior();
            Behavior.HttpGetEnabled = true;
            Host.Description.Behaviors.Add(Behavior);
            Host.Open();
            Console.ReadKey();
        }
    }
}

When this code is run, I get the following exception:

The contract type ConsoleApp25.SOAAddImpl is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute.

If I add the attribute to the class, I get the following exception:

The service class of type ConsoleApp25.SOAAddImpl both defines a ServiceContract and inherits a ServiceContract from type ConsoleApp25.ISOAAdd. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Consider moving the ServiceContractAttribute on type ConsoleApp25.ISOAAdd to a separate interface that type ConsoleApp25.ISOAAdd implements.

I. e. for some reason the app cannot see that the interface is already decorated with ServiceContract, but when I decorate both the class and the interface it CAN see the decoration (both), which results in the error above. How do I solve this?

1
obviously it should be new ServiceHost(ServiceType, ... and host.AddServiceEndpoint(typeof(ISOAAdd), ... - Selvin
Thank you so much! You saved my day. - Jurijus Zaksas

1 Answers

0
votes

As Selvin pointed in the comment section, you need to send parameter service interface(typeof(ISOAAdd)) instead of service object(typeof(SOAAddImpl)) to ServiceHost.AddServiceEndpoint method.

Host.AddServiceEndpoint(typeof(ISOAAdd), new BasicHttpBinding(), "SOAAddImpl");