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:
[ServiceContract]
public interface IService
{
[OperationContract]
string OperationWithGet(string s);
[OperationContract]
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/")))
host.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "");
host.Open();
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.
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet]
string EchoWithGet(string s);
[OperationContract]
[WebInvoke]
string EchoWithPost(string s);
}
Now, the host should be changed too:
WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost/webmethods:8000/")))
host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
host.Open();
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());
IService channel = cf.CreateChannel();
channel.OperationWithGet("Hello, get");
channel.OperationWithPost("Hello, post");
Posted
Sep 09 2007, 07:07 PM
by
Damir Dobric