3

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 "Connection 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?

user203687
  • 6,875
  • 12
  • 53
  • 85

3 Answers3

12

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";
}
Devin Burke
  • 13,642
  • 12
  • 55
  • 82
LouisC
  • 121
  • 1
  • 3
3

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");
}
Kuba Beránek
  • 458
  • 9
  • 19
2

The answer is here WCF ResponseFormat For WebGet (and it worked for me)

Community
  • 1
  • 1
user203687
  • 6,875
  • 12
  • 53
  • 85