1

This is my first attempt at a service via WCF which is hosted in a Windows Service. I have noticed that if I do something incorrectly in the UriTemplate it completely breaks everything and I don't know why.

Example:

In the first code example everything works fine. The service waits on my defined base address and returns the information I expect.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetDetail?id={id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    MyDetail GetDetail(int id);
}

In this example where I have changed the UriTemplate = "/GetDetail?id={id}" to UriTemplate = "/GetDetail/{id}" everything breaks. The service doesn't even wait on my configured base address.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetDetail/{id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    MyDetail GetDetail(int id);
}

I don't understand how this change can cause everything to fail? Shouldn't it just fail to work on that GetDetail call and not break the whole system?

Also to expand on this how can I add logging to my service.

CathalMF
  • 9,705
  • 6
  • 70
  • 106

1 Answers1

0

When using WebGet or WebInvoke, UriTemplate variables in the path have to be strings. You can only bind UriTemplate variables to int, long, etc. when they are in the query portion of the UriTemplate as in your first example.

So, a very basic way to solve your problem could be be

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "/GetDetail/{id}", BodyStyle = WebMessageBodyStyle.WrappedResponse, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    MyDetail  GetDetail(string id);
}
Cybermaxs
  • 24,378
  • 8
  • 83
  • 112
  • Yes I understand the fact that they need to be strings but I don't know why having one of these incorrectly defined causes the whole webservice to fail? – CathalMF Jul 03 '13 at 13:23
  • 1
    At startup, WCF activation validates service declaration & configuration. You can't partially run a WCF service. – Cybermaxs Jul 03 '13 at 19:03