CoreWCF: NetCoreClient throws exception "type is not a ServiceContract"
I add a small service to NetCoreClient and NetCoreService, when the service factory is created from the NetCoreClient
var factory = new ChannelFactory<ISchedulerService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/basichttp"));
an exception is thrown with the message:
Attempted to get contract type for ISchedulerService, but that type is not a ServiceContract, nor does it inherit a ServiceContract.
Source=System.Private.ServiceModel
StackTrace:
at System.ServiceModel.Description.ServiceReflector.GetContractTypeAndAttribute(Type interfaceType, ServiceContractAttribute& contractAttribute)
at System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(Type contractType, Type serviceType, Object serviceImplementation)
at System.ServiceModel.ChannelFactory1.CreateDescription() at System.ServiceModel.ChannelFactory.InitializeEndpoint(Binding binding, EndpointAddress address) at System.ServiceModel.ChannelFactory
1…ctor(Binding binding, EndpointAddress remoteAddress)
at NetCoreClient.Program.Main(String[] args) in C:\Github\CoreWCF\src\Samples\NetCoreClient\Program.cs:line 23
To do this I added a netstandard 2.0 library TaskLibrary to CoreWCFWithSamples.sln. Then I added a definition for two types:
ISchedulerService
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using CoreWCF;
namespace TaskLibrary
{
[ServiceContract]
[ServiceKnownType ( typeof ( TaskTypes ) )]
public interface ISchedulerService
{
#region Methods
[OperationContract]
List<TaskTypes> GetSupportedTaskTypes ();
#endregion
}
}
TaskTypes
using System;
using System.Collections.Generic;
using System.Text;
namespace TaskLibrary
{
public enum TaskTypes
{
ManualTask,
AutoTask,
RepetitiveTask
}
}
Next I added TaskLibrary references to NetCoreClient and NetCoreServer.
To NetCoreServer startup.cs the Configure code was modified to:
public void Configure ( IApplicationBuilder app, IHostingEnvironment env )
{
app.UseServiceModel ( builder =>
{
builder.AddService<SchedulerService> ();
builder.AddServiceEndpoint<SchedulerService, TaskLibrary.ISchedulerService> ( new BasicHttpBinding (), "/basichttp" );
} );
}
To NetCoreClient program.cs the Main code was modified as shown below. After NetCoreServer is started up, then NetCoreClient is started and the exception at the start of the post is generated.
What am I doing wrong?
static void Main(string[] args)
{
/*
var factory = new ChannelFactory<Contract.IEchoService>(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8808/nettcp"));
factory.Open();
var channel = factory.CreateChannel();
((IClientChannel)channel).Open();
Console.WriteLine("net.tcp Echo(\"Hello\") => " + channel.Echo("Hello"));
((IClientChannel)channel).Close();
factory.Close();
*/
var factory = new ChannelFactory<ISchedulerService>(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/basichttp"));
factory.Open();
var channel = factory.CreateChannel();
((IClientChannel)channel).Open();
var taskTypes = channel.GetSupportedTaskTypes ();
//Console.WriteLine("http Echo(\"Hello\") => " + channel.Echo("Hello"));
((IClientChannel)channel).Close();
factory.Close();
Console.WriteLine("Hit enter to exit");
Console.ReadLine();
}
About this issue
- Original URL
- State: open
- Created 4 years ago
- Comments: 33 (13 by maintainers)
Yes, that is the problem. ChannelFactory needs to use System.ServiceModel.ServiceContractAttribute and CoreWCF needs to use CoreWCF.ServiceContractAttribute. I actually have a PR right now to allow CoreWCF to use the S.SM attributes so you can use a single common contract if you want (#87). I’m just waiting on a code review. So you can either do the juggling with the two namespaces or you can wait until someone has reviewed my changes and then pick up the latest packages which allows you to just use S.SM.ServiceContract if that’s what you would prefer.