views:

1555

answers:

2

Hello,

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

+1  A: 

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};
        }
Khash
A: 

hi, I had the same probem, but I wanted to see the help page so disabling it wasn't a solution for me. I found out that URITemplating in the WCF REST Toolkit is causing those problem, when it sees that it already have this template in the template tables. Basically you will only need a template when the URL to your method is different according to the requested data, after all, that's what the templates are for. I had the same URITemplates for my POST operations, so the URLs did not differ between separate queries which causes this error. then I found out that I actually didn't need any templating at all, at least for the POST operations, moreover youvannot make a POST query though the URL if your method requires a complex object to be passed as a parameter. So I removed the URITemplate named parameter from the WebInvoke attribute in the service interface, I think that solved the problem. Of course if you make GET queries to the server and rely on URITemplating you'll still have to either put up with or leave away the help page.

slighter