Why WCF service is getting old values of parameters - wcf

I have setup two projects. One is WCF service project with following contract:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "xml/{filterName}/{filterValue}")]
XmlElement XMLDataWithFilter(string filterName, string filterValue);
Other project uses this WCF service. The values for the parameters filterName and filterValue sent with first call remain same when some I make call to WCF service with some other parameters.
Why WCF service is retaining the values of parameters?

Related

How to do versioning in WCF services

I am using WCF services for the Mobile and Web platform and I want to apply the versions to them included Database version as well.
[OperationContract]
[FaultContract(typeof(CommonFaultContract))]
[WebInvoke(Method = "GET", UriTemplate = "/login/{uname},{password},{uuid},{deviceName},{deviceOS}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
UserDataContract login(string uname, string password, string uuid, string deviceName, string deviceOS);
using the above code to call the operation contact
These links may help you-
msdn data contract versioning
wcf handle versioning

REST API Post request - Request Error

I have a REST API call that uses post method. The WCF Service inturn saves the input string to the database. Here is my WCF Contract
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "/xxxx")]
int xxxx(string username);
When I use DHC client on chrome to test , it displays a bad request error. The json data send to the server is of the form {"username": test123}
Pls find the image below
Is the format of the data sent to server incorrect ? Please suggest.
The contract should be marked with following WebInvoke attributes -
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/xxxx")]

Make all methods in a WCF service all be GET's with the same body wrap style

I would like to make all the methods I am adding to my WCF service have the following behaviour:
[WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
Instead of having to add these attributes to every single [OperationContract] method is there a way I can configure this service wide?

Creating a WCF JSON (non RESTful) Service

I am trying to create a simple non-RESTful JSON service using WCF and .NET 4.
I'd like my service to be able to parse a JSON request message with a specific format, something like this:
{ "MethodNameRequest": { "MethodParam1Name": "ParamValue1", "MethodParam2Name": "ParamValue2" } }
The endpoint for this service should reside in a single constant URI ("http://myserver/myservice/") so that all methods could be invoked using a POST request to it.
The problem is that whenever I try to declare two (or more) methods using the same "UriTemplate" and the same HTTP verb "POST" (using WebInvokeAttribute), like this:
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "")]
public string Method1()
{
return "Method1";
}
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "")]
public string Method2()
{
return "Method2";
}
I get the following exception:
In contract '', there are multiple
operations with Method 'POST' and a
UriTemplate that is equivalent to ''.
Each operation requires a unique
combination of UriTemplate and Method
to unambiguously dispatch messages.
Use WebGetAttribute or
WebInvokeAttribute to alter the
UriTemplate and Method values of an
operation.
Any ideas on how I can configure WCF to allow this?
I don't see how WCF could figure out which method to call if it somehow allowed the identical UriTemplate for the different methods. Seems you need to implement logic inside the method to handle content based processing.
Try to ommit the UriTemplate property, use instead the <enableWebScript/> element in web.config. This will allow wcf to automatically handle the requests for you.

WCF WebInvoke ResponseFormat

I have a WCF restul service and I want to allow the user to choose what request format they want, i have the decorations
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=xml")]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
First of, is there a way to specify the ResponseFormat at runtime and take the format in as an argument to the method? From reading around i dont think so... OK next thing
The above code is ok and works, but im having a problem, i want to be able to specify a default, so when no format arguement is passed then i just default but if i decorate like so
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch})]
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "getstreamurl?ch={ch}&format=json")]
Where the XML is the default, if i try to call the service method through the browser it tells me that:
UriTemplateTable does not support multiple templates that have equivalent path as template 'getstreamurl?ch={ch}' but have different query strings, where the query strings cannot all be disambiguated via literal values. See the documentation for UriTemplateTable for more detail
They obviously can be distinguished but it seems that WCF is only reading up to the argument and thats it...Any suggestions?
No, I don't think you can do that programmatically at runtime. What you can do of course if to expose two distinct endpoints from your service - one returning XML, another returning JSON, and then programmatically pick which one to call from your client app.
Marc
Update: as Steve Michelotti correctly points out, this automatic switching between JSON and XML can now be achieved in WCF 4.0. WCF 4.0 has an improved REST support which also includes an Format Message Selection feature, based on HTTP accept headers.
For more info on WCF 4.0's new features, see: A Developer's Introduction to WCF 4.0
You can do this if your rest service is configured automatically select response type.
Then on client request simply add needed header Accept: application/json