Mono WCF Advent Day 7: Moonlight

| 1 Comment | No TrackBacks

Our WCF is ready in Moonlight 2.0. It provides Silverlight 2.0 compatibility, and filling 3.0 support should be almost easy (we need to implement things like CookieContainer support etc.). Actually our WCF stack has been improved a lot by the moonlight effort (the team gave several bug reports).

I haven't explained how to prepare Moonlight environment. Theis a wiki page for instructions I linked above (I assume it will be frequently updated, so I'm not going to give the anchor to the build instruction section now).

Also, when creating moonlight apps, make sure you have 'moonlight-web-devel' package (in case it is available in your distro) or some sort of equivalents (e.g. "MoonSDK") installed and set up.

Some notes

  • WCF in .NET and WCF in Moonlight are significantly different. We have almost feature complete WCF in Moonlight, while we lack a lot of features in .NET WCF. In WCF only BasicHttpBinding is supported in System.ServiceModel.dll (as Silverlight does).
  • As I mentioned on Day 5, Moonlight is the land of "async everywhere". So there isn't sync service calls. Operation methods are called with BeginFoo() and EndFoo() instead of Foo(). If you use them, EndFoo() is expected to be called inside AsyncCallback of the BeginFoo() argument. FooAsync() pattern is easier.
  • Silverlight applications may contain Microsoft's SDK assemblies. They are not what we implemented. For example, System.Xml.Linq.dll, System.Json.dll and System.ServiceModel.PollingDuplex.dll are SDK assemblies. I have never tried the last assembly, so it may or may not work.
  • Silverlight client can access the services only when it is from allowed domain by the website. "clientaccesspolicy.xml" and "crossdomain.xml" play the key role on the permission (I don't explain their details here, MSDN would give you hints).

Example

Now, let's build a simple app and see it's working. (NOTE: Here I use the latest version of svcutil which includes support for event-based async call I explained on Day 5.)

While it is possible to create a Moonlight application from console using "smcs" and "mxap" tools, I take the easiest way to create Moonlight project.

  • Start MonoDevelop (2.2 would be the best; earlier versions that support Moonlight project is fine).
  • Create a "Moonlight Application project", say, moonlight1. There are App.xaml, App.xaml.cs (folded), Page.xaml and Page.xaml.cs (folded) as the sources.
  • Just build and run to make sure it works (a web page with moonlight xap, printing "Hello Moon").
  • Prepare a WCF service which is hosted under xsp2 as explained on Day 6. It is important to host a service on ASP.NET since you have to provide access to "clientaccesspolicy.xml" (explained above). You cannot do it with a standalone ServiceHost at least in simple manner.
  • Create "clientaccesspolicy.xml" file on the hosted root directory. Moonlight WCF client will try to httpget the file. An example file content is pasted below, which states that it accepts SOAP requests from anywhere.
  • Create a client proxy explained on Day 5, by running: svcutil -moonlight http://localhost:8080/test.svc/wsdl
  • Open Page.xaml.cs on MonoDevelop, and add the code below for service invocation. The example means that the result from the SOAP response will be shown as a Javascript alert message when the application is loaded.
  • Run the Moonlight application project (F5 or F8). Moonlight addin will open your browser and show the test page.
<!-- clientaccesspolicy.xml -->
<access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-methods="*" http-request-headers="*">
        <domain uri="*">
        </domain>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true">
        </resource>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>
// Page.xaml.cs
this.Loaded += delegate {
  var binding = new BasicHttpBinding ();
  var address = new EndpointAddress ("http://localhost:8080/test.svc");
  var client = new HelloServiceClient (binding, address);
  client.GreetCompleted += delegate (object o, GreetCompletedEventArgs e) {
    System.Windows.Browser.HtmlPage.Window.Dispatcher.BeginInvoke (delegate {
        System.Windows.Browser.HtmlPage.Window.Alert (e.Result);
        tb.Text = e.Result;
    });
  };
  client.GreetAsync ("moonlight");
};

moonlight2-wcf-md.png

No TrackBacks

TrackBack URL: http://veritas-vos-liberabit.com/monogatari/mt-tb.cgi/90

1 Comment

nice articles about Moonlight 2.0. It provides Silverlight 2.0 compatibility, and filling 3.0

Leave a comment