How can a Metro app in Windows 8 communicate with a backend desktop app on the same machine?

40,035

Solution 1

I'm porting my existing project to Win8 right now. It consists of windows service and tray application which are talking to each other via NamedPipes WCF. As you may already know Metro doesn't support named pipes. I ended up using TcpBinding for full duplex connection.

This post describes what functionality is supported.

Sample of my WCF server that Metro client can consume is here.

Also keep in mind that you can't use synchronous WCF in Metro. You'll have to use Task-based wrapper which is only asynchronous.

And thank you for you question. I was good starting point for me :)

Solution 2

There were a number of questions like this at the end of a //build/ session I attended. Aleš Holeček, the exec who did one of the big picture sessions, came up out of the audience to handle them. Even if you're not a C++ developer, download that session and watch the Q & A. http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-789C

Metro apps can't count on desktop apps or services being installed on the machine. And desktop apps can't count on Metro apps running since they can be suspended any time. You need to start thinking differently. Listen to Aleš on this one.

Solution 3

Take note that with Windows 8.1 Update, communication between Windows Store apps and desktop components written in C# for .NET 4.5+ is now officially supported for side-loaded applications in Enterprise scenarios:

Brokered Windows Runtime Components for side-loaded Windows Store apps

To quote:

Recognizing that critical business functions and rules are embodied in existing software assets and that enterprises have a wide variety of scenarios for which the new application style will be highly productive, the Windows 8.1 Update includes a new feature called Brokered Windows Runtime Components for side-loaded applications. We use the term IPC (inter-process communication) to describe the ability to run existing desktop software assets in one process (desktop component) while interacting with this code in a Windows Store app. This is a familiar model to enterprise developers as data base applications and applications utilizing NT Services in Windows share a similar multi-process architecture.

Although implementing this approach is a bit on the complicated side initially, it allows for deep integration across Windows Store and desktop components. Just keep in mind that for the time being, it won't pass public Windows Store certification.

Solution 4

There is an article on InfoQ about how to build loosely coupled Metro apps with protocol handlers. This is something which has been supported by Windows for a long time and one could foresee an desktop application register itself as a protocol handler and maybe the metro application can communicate through this mechanism.

I have no idea if this is possible, but it might be interesting to check out.

Solution 5

Christophe Nasarre has blogged about a rather hacky way to do it using local files. The result is communication between desktop app/windows store app (referred to as DA/WSA in the blog), without having to switch between the UI of the two apps. He also blogged about another less hacky technique involving protocol handlers.

Note that having a WSA which communicates with a DA is explicitly forbidden by the store App certification requirements

Windows Store apps must not communicate with local desktop applications or services via local mechanisms, including via files and registry keys.

... but it restricts "local mechanisms" only. So I guess one can build a web service for routing the communications.

Share:
40,035
dodgy_coder
Author by

dodgy_coder

I'm an Australian software developer. Programming in C#, WPF, Android and Java. Big Raspberry Pi fan. I'm on twitter @dodgy_coder and write a blog at dodgycoder.net

Updated on November 26, 2020

Comments

  • dodgy_coder
    dodgy_coder over 3 years

    In a situation where you have the UI frontend built using the new Metro style of apps for windows 8, and would like it to communicate with a .NET application running on the desktop on the same local machine (e.g. a windows service app).

    What forms of interprocess communication are available between the metro app and the desktop app?

    Thanks to Pavel Minaev of the Visual Studio team, who has provided some initial info here in a comment, quoted:

    According to Martyn Lovell, there isn't any deliberate mechanism for that, and some that could be used for it are intentionally restricted. Named pipes aren't there, for example, nor are memory mapped files. There are sockets (including server sockets), but when connecting to localhost, you can only connect to the same app. You could use normal files in one of the shared "known folders" (Documents, Pictures etc), but that is a fairly crude hack that necessitates polling and is visible to the user. -- Pavel Minaev commenting on this issue

    So failing normal approaches I was thinking of using web services or reading/writing to a database in order to get some form of communication happening, both of which seem like overkill when the processes are running on the same machine.

    Is what I'm attempting here making sense? I can see a need for a metro app to be the frontend UI for an existing service which is running on the desktop. Or is it better to just use WPF for the frontend UI running on the desktop (i.e. a non-metro app).

  • Pavel Minaev
    Pavel Minaev over 12 years
    This exact question seems to be asked at 47:20 in the video.
  • Pavel Minaev
    Pavel Minaev over 12 years
    ... and one more at 55:00. The answer, generally, speaking, seems to be "nope you can't do that".
  • dodgy_coder
    dodgy_coder over 12 years
    Thanks for the markers, yeah the speaker is saying that due to the strict sandboxing, its not really possible, but may be achieved by using the shared files workaround. Probably the best way then is to just assume they may not be on the same machine and use web services or WCF/tcp binding.
  • Pavel Minaev
    Pavel Minaev over 12 years
    @dodgy_coder I'm not sure WCF/TCP (or HTTP) would work on the same machine. If the sandbox doesn't let you connect to localhost directly via a TCP socket, why would it let you do the same via WCF?
  • dodgy_coder
    dodgy_coder over 12 years
    @Pavel I was thinking of communicating via web services over a valid/live domain, not localhost... so in effect the comms go out to the internet (or intranet) and back.
  • dodgy_coder
    dodgy_coder over 12 years
    Interestingly, there is built in support for communicating between two metro apps via share contracts but this seems to be similar in usage to the clipboard, and for transferring one-way from a source app to a target app, rather than for implementing say a two-way communication protocol.
  • Lumi
    Lumi over 12 years
    The article says: "The way you can do this in Metro [jump over to another workflow in a different application - because your app is meant to be small and highly focussed] is by leveraging protocols. For our example above the protocol may look like “acme-stock-purchase://client=123&stock=XYZ”." - Just what does it mean technically, "leveraging protocols"?
  • expert
    expert about 12 years
    The problem with this approach is that it's only one way communication.
  • dodgy_coder
    dodgy_coder about 12 years
    Thanks for this ... that's a great help. Its good to see a practical answer instead of just being told that it shouldn't / can't be done.
  • lightw8
    lightw8 about 12 years
    Seems to me that side-loaded LOB Metro apps would have no problem depending on an installed desktop app or service. I have a hard time believing this very practical scenario won't be supported. With Silverlight, we saw a gradual increase in desktop/native interop capabilities...I'm pretty sure something along these scenarios (named pipes, or memory-mapped files, or something...) will be supported (with guidance docs) in the future.
  • dzendras
    dzendras over 11 years
    It may be a stupid question... but you may connect to localhost using your sample or not? The question you link to shows the internals of Visual Studio (I deduced it from the path, but if I'm wrong, please correct me). Does WCF (combined with localhost) work outside of WCF?
  • expert
    expert over 11 years
    @dzendras What do you mean by "outside" ?
  • dzendras
    dzendras over 11 years
    when the app is run without VS's assistance (deployed)
  • expert
    expert over 11 years
    @dzendras Sure, it will work. It will work with localhost as well.
  • Ani
    Ani over 11 years
    I doubt an app like this would pass Store certification, though.
  • expert
    expert over 11 years
    @ananthonline What part of license agreement does it violate?
  • Ani
    Ani over 11 years
    If anything this is the rule I would think that might be quoted "3.9 All app logic must originate from, and reside in, your app package You app must not attempt to change or extend the packaged content through any form of dynamic inclusion of code or data that changes how the application interacts with the Windows Runtime, or behaves with regard to Store policy. It is not permissible, for example, to download a remote script and subsequently execute that script in the local context of your app package."
  • dodgy_coder
    dodgy_coder over 11 years
    Not sure why you need to mention about social engineering... since the original question is talking about a desktop app/service talking to a metro app, it is expected that the user will need to install the desktop app/service separately. So what method of communication did you use between the desktop service and the metro app?
  • ehdv
    ehdv almost 11 years
    This answer should include a caveat that it will very likely cause an application to fail Windows Store certification.
  • expert
    expert almost 11 years
    @ehdv It's been a while now but as far as I remember Dell didn't have problems with certification.
  • Hjulle
    Hjulle over 10 years
    @ananthonline As far as I understand none of the apps internal logic comes from an external source. This is just simple server-client software where both runs on the same computer.
  • adosaiguas
    adosaiguas almost 10 years
    I wanted to be able to communicate with localhost for an internal application and I could only make it happen on the computers not running VS using this command.