Search code examples
c#wcfrestwcf-client

WCF REST Starter Kit - A property with the name 'UriTemplateMatchResults' already exists


I just started with the WCF REST Starter Kit.

I created a simple service that return an array of an object.

Using the browser, everything works fine but when I use a WCF client, I get an ArgumentException.

I'm not using IIS and here is the code:

The contract:

[ServiceContract]
    public interface IGiftService {

        [WebGet(UriTemplate="gifts")]
        [OperationContract]
        List<Gift> GetGifts();

    }

    public class GiftService : IGiftService {

        public List<Gift> GetGifts() {
            return new List<Gift>() {
                new Gift() { Name = "1", Price = 1.0 },
                new Gift() { Name = "2", Price = 1.0 },
                new Gift() { Name = "3", Price = 1.0 }
            };
        }

    }

    [DataContract]
    public class Gift {

        [DataMember]
        public string Name { get; set; }
        [DataMember]        
        public double Price { get; set; }
    }

To start the service:

WebServiceHost2 host = new WebServiceHost2(
                typeof(GiftService), 
                true, 
                new Uri("http://localhost:8099/tserverservice"));
            host.Open();

            Console.WriteLine("Running");
            Console.ReadLine();
            host.Close();

To start the client:

WebChannelFactory<IGiftService> factory = new WebChannelFactory<IGiftService>(
                new Uri("http://localhost:8099/tserverservice"));

            IGiftService service = factory.CreateChannel();
            List<Gift> list = service.GetGifts();

            Console.WriteLine("-> " + list.Count);
            foreach (var item in list) {
                Console.WriteLine("-> " + item.Name);
            }

The server and the client are in the same solution and I'm using the same interface in both (to describe the service contract).

The exception says: "A property with the name 'UriTemplateMatchResults' already exists." and that is the stack trace:

Class firing the exception -> Microsoft.ServiceModel.Web.WrappedOperationSelector

Stack trace:

  at System.ServiceModel.Channels.MessageProperties.UpdateProperty(String name, Object value, Boolean mustNotExist)
   at System.ServiceModel.Channels.MessageProperties.Add(String name, Object property)
   at System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector.SelectOperation(Message& message, Boolean& uriMatched)
   at System.ServiceModel.Dispatcher.WebHttpDispatchOperationSelector.SelectOperation(Message& message)
   at Microsoft.ServiceModel.Web.WrappedOperationSelector.SelectOperation(Message& message) in C:\Program Files\WCF REST Starter Kit\Microsoft.ServiceModel.Web\WrappedOperationSelector.cs:line 42
   at Microsoft.VisualStudio.Diagnostics.ServiceModelSink.ServiceMethodResolver.GetOperation()
   at Microsoft.VisualStudio.Diagnostics.ServiceModelSink.ServiceMethodResolver..ctor(ContractDescription contract, DispatchRuntime runtime, Message request, InstanceContext instanceContext)

What am I doing wrong?

UPDATE: I disabled the help page and the service is working now. Is it a bug?

host.EnableAutomaticHelpPage = false;

Thank you!

André Carlucci


Solution

  • Had the same problem, disabled the help page and it fixed it. The exception was being thrown if some REST urls were called in a sequence very quickly. It was fine when waiting between the calls.

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
            {
                return new WebServiceHost2(serviceType, true, baseAddresses) {EnableAutomaticHelpPage = false};
            }