When using "ChannelFactory" to try to consume WCF Restful Service, e.g.:
Service
[ServiceContract]
public interface ISocialStatus
{
[OperationContract]
[WebGet(UriTemplate =
"/statuses/update/{text}")]
void UpdateStatus(string text);
}
Client:
using (ChannelFactory<ISocialStatus> factory = new ChannelFactory<ISocialStatus>("SocialClient"))
{
ISocialStatus socialChannel = factory.CreateChannel();
socialChannel.UpdateStatus("aaa");
}
Solution:
Way 1: Add WebHttpBehavior
using (ChannelFactory<ISocialStatus> factory = new ChannelFactory<ISocialStatus>("SocialClient"))
{
factory.Endpoint.Behaviors.Add(new WebHttpBehavior());
ISocialStatus socialChannel = factory.CreateChannel();
socialChannel.UpdateStatus("aaa");
}
Way 2: Use WebChannelFactory
using (System.ServiceModel.Web.WebChannelFactory<ISocialStatus> factory = new WebChannelFactory<ISocialStatus>("SocialClient"))
{
ISocialStatus socialChannel = factory.CreateChannel();
socialChannel.UpdateStatus("aaa");
}