Hi, I'm trying to get a Server application to expose some status information using WCF. In particular I'm after using WCF services with RESTful "API". I'm hitting somewhat of a wall when it comes to consuming the REST api from a silverlight app/page that I want to have as an additional type of client...
So far I've been successful in defining a status interface:
public static class StatusUriTemplates
{
public const string Status = "/current-status";
public const string StatusJson = "/current-status/json";
public const string StatusXml = "/current-status/xml";
}
[ServiceContract]
public interface IStatusService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = StatusUriTemplates.StatusJson)]
StatusResultSet GetProgressAsJson();
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = StatusUriTemplates.StatusXml)]
StatusResultSet GetProgressAsXml();
[OperationContract]
[WebGet(UriTemplate = StatusUriTemplates.Status)]
StatusResultSet GetProgress();
}
Implementing it in the server:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class ServerStatusService : IStatusService
{
public StatusResultSet GetProgressAsJson()
{ return GetProgress(); }
public StatusResultSet GetProgressAsXml()
{ return GetProgress(); }
public StatusResultSet GetProgress()
{
return StatusResultSet.Empty;
}
}
Exposing it from my code at runtime:
var service = new ServerStatusService();
var binding = new WebHttpBinding();
var behavior = new WebHttpBehavior();
var host = new WebServiceHost(service, new Uri("http://localhost:8000/server"));
host.AddServiceEndpoint(typeof(IStatusService), binding, "status");
host.Open();
I've even been successful with consuming the service from a .NET console/winfoems/WPF application using something along the line of this:
var cf = new WebChannelFactory<IStatusService>(new Uri("http://localhost:8000/server/status"));
var ss = cf.CreateChannel();
Console.WriteLine(ss.GetProgress().TimeStamp);
The "wall" I'm hitting is that there is NO WebChannelFactory for SliverLight.
Period.
This means that when it comes to silverlight code, my options are:
- Write ugly code using WebClient, which ultimately means I will have to update two sets of code whenever I have a change to my API
- Use SOAP/WS for the WebService and keep updating the service reference from Visual Studio
Is there a way to keep the "clean" implementation with WebChannelFactory in SilverLight? Perhaps a public domain / open source WebChannelFactory for SilverLight?
Any help with this will be greatly appreciated!