WebGet and WebInvoke

Damir Dobric Posts

Next talks:



Follow me on Twitter: #ddobric




WCF 3.5 introduces two new operation behaviors: WebGet and WebPut. I wouldn't declare this as revolutionary feature, but definitely as an important one.
To explain new behaviors Imagine you have a contract like this one:

  public interface IService 
    string OperationWithGet(string s);

string OperationWithPost(string s);

To start a service implementing this interface following example could be used:
ServiceHost host = new erviceHost(typeof(Service), new Uri("http://localhost/webmethods:8000/")))
typeof(IService), new BasicHttpBinding(), "");


Imagine now, you have a requirement to access service operations as web-methods, by entering following in your browser:
http://localhost/webmethods:8000/OperationWithGet?s=Hello, world!"

The ASP.NET based services can do this, but WCF not, at least not until now. To make this possible WCF3.5 introduces the new assembly System.ServiceModel.Web.dll, which provides two new operation behaviors, one new binding WebHttpBinding, one ServiceHost WebServiceHost and one new factory WebChannelFactory To achieve this requirement (invoke service operations as web-methods) the example above has to be slightly changed. The target operation should be declared with attributes WebGet or WebPut, depending of wanted HttpMethod.

public interface IService

string EchoWithGet(string s);

string EchoWithPost(string s); 

Now, the host should be changed too:

WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost/webmethods:8000/")))
typeof(IService), new WebHttpBinding(), "");


The client is as simple as this:

WebChannelFactory<IService> cf = new WebChannelFactory<IService>();
cf.Endpoint.Binding = new WebHttpBinding();

cf.Endpoint.Address = new EndpointAddress(new Uri("localhost/webmethods:8000"));

cf.Endpoint.Behaviors.Add(new WebHttpBehavior());

channel = cf.CreateChannel();
channel.OperationWithGet("Hello, get");
channel.OperationWithPost("Hello, post");

Posted Sep 09 2007, 07:07 PM by Damir Dobric
Filed under:


Damir Dobric Posts wrote Syndication API for .NET Compact Framework
on 09-09-2007 23:17

Few weeks ago Microsoft announced the APLHA version of “ BizTalk Services ”. Small part of the package

Damir Dobric Posts wrote Mobility Day Zagreb 2007
on 09-10-2007 9:02

At Sept. 11. 2007 I will talk at Mobility Day in Zagreb about new communication possibilities around

Matt wrote re: WebGet and WebInvoke
on 11-03-2007 6:59

I'm trying to make the most simple service that consumes and output xml.  I followed forums.microsoft.com/.../ShowPost.aspx

and am able to output xml properly, but after almost a day, I am not any closer to consuming xml.

Here is what my contract looks like.


public interface ITestService


   [OperationContract, WebInvoke(

           BodyStyle = WebMessageBodyStyle.Bare,

           RequestFormat = WebMessageFormat.Xml,

           ResponseFormat = WebMessageFormat.Xml)]

   XmlElement GetAsXml(Stream input);


I am using fiddler to simulate a client.  Here is what I am sending using fiddler:


User-Agent: Fiddler


Content-Length: 204

<TextBlock x:Name="text" IsHitTestVisible="false" Text="Hello" Foreground="black" xmlns="schemas.microsoft.com/.../presentation" xmlns:x="schemas.microsoft.com/.../>

The response I get it is HTTP/1.1 415 Unsupported Media Type

If I add Content-Type: application/xml; charset=utf-8 to the request headers, I get the following response:

HTTP/1.1 400 Bad Request

Any ideas.  All I want to do is parse the xml that is in the post request on the server

The_Ghost wrote re: WebGet and WebInvoke
on 06-08-2010 23:21

You have to add that line to the request:

Content-Type: application/xml; charset=utf-8

I had the same problem.

developers.de is a .Net Community Blog powered by daenet GmbH.