Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 381
andrecarlucci Avatar asked Dec 04 '25 08:12

andrecarlucci


2 Answers

In my case, the problem only occurred when accessing the endpoint using a WCF channel with Visual Studio debugger integration enabled.

I worked around the issue by adding some code to remove the VS behaviour from the ChannelFactory:

var vsBehaviour = channelFactory.Endpoint.EndpointBehaviors
    .FirstOrDefault(i =>
        i.GetType().Namespace == "Microsoft.VisualStudio.Diagnostics.ServiceModelSink");
if (vsBehaviour != null)
{
    channelFactory.Endpoint.Behaviors.Remove(vsBehaviour);
}

Apparently, there are other ways to disable WCF Visual Studio debugger integration, but they seem to be system-wide, while this solution is local.

like image 194
nickguletskii Avatar answered Dec 06 '25 20:12

nickguletskii


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};
        }
like image 33
Khash Avatar answered Dec 06 '25 22:12

Khash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!