How can I return bare result back to WCF client [duplicate]
I have code something like the following:
<OperationContract()>
<Description("")>
<WebGet(Bodystyle:=WebMessageBodyStyle.Bare, UriTemplate:="TestConnection")>
Function TestConnection() As String
Public Function TestConnection() As String Implements ITestSvc.TestConnection
WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"
Return "Connection Success"
End Function
But it returns is <string xmlns='...'>Connection Success</string>
How can I have only "Con开发者_开发技巧nection Success" to be returned without XML wrapper. I know that we can do something with MessageEncoder. But, I want to have it available at operation level (certain operations need XML/JSON wrappers and certain operations don't).
Can anyone help me on this?
here is the simplest solution to return plain text. Set response format to xml and set outgoingresponse to text/html. Should do the trick.
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
public string DoWork()
{
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
return "THIS IS PLAIN TEXT";
}
There is one way how to achieve this if you're dealing with HTTP, it's not exactly nice, but I thought I could mention it.
You can set the return type of your method to void and just output your raw string directly into the response.
[OperationContract]
[WebGet(UriTemplate = "foo")]
void Foo()
{
HttpContext.Current.Response.Write("bar");
}
The answer is here WCF ResponseFormat For WebGet (and it worked for me)
精彩评论