Create and consume an app service

这个文章举了一个进程间应用服务的例子。方案里面涉及三个项目:AppServiceProvider、MyAppService和ClientApp。

一个幕后任务(Background Task)被创建的时候可以存续大概30秒。如果在这30秒内接受到后继的调用请求,幕后任务的存续时间会得到延长。但是,可以通过GetDeferral方法来获得一个延时令牌,并将其保存到变量中。这样,幕后任务的存续期就得到延长了(即便客户端主动退出也不会立即中止)。

但是文档中又有另一种说法:

The lifetime of the app service depends on the caller:

If the caller is in the foreground, the app service lifetime is the same as the caller. If the caller is in the background, the app service gets 30 seconds to run. Taking out a deferral provides an additional one time 5 seconds.

可能上面是针对进程内应用服务而言的。

在Package.appxmanifest中添加对uap3和uap4的命名空间的定义,并申明AppService:

...
  xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
  xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
...
      <Extensions>
        <uap:Extension Category="windows.appService" EntryPoint="MyAppService.Inventory">
          <uap3:AppService Name="com.microsoft.inventory" uap4:SupportsMultipleInstances="true"/>
        </uap:Extension>
      </Extensions>

跨进程的服务需要使用AppServiceName和PackageFamilyName指定,并且需要部署以后才能调用。

调试和排查有很多事项需要注意。比如调试AppServiceProvider的时候:

In the Solution Explorer, right-click the AppServiceProvider project and choose Properties. From the Debug tab, change the Start action to Do not launch, but debug my code when it starts. (Note, if you were using C++ to implement your app service provider, from the Debugging tab you would change Launch Application to No).

Convert an app service to run in the same process as its host app

在Package.appxmanifest申明AppService

          <Extensions>
              <uap:Extension Category="windows.appService">
                  <uap:AppService Name="InProcessAppService" />
              </uap:Extension>
          </Extensions>

App-to-App Communication: Building a Web of Apps

BackgroundTask is about running code without UI. AppService is based on BackGroundTask.

  • Session 1: Use PackageFamilyName in LaunchUir Scheme, also to get result.
  • Session 2: App Services [25:30]

参考

(完)

2019-11-19更新

In-Process尚不能使用于Create a multi-instance Universal Windows App

布署APP的时候会出错:

DEP0700: Registration of the app failed. [0x80073CF6] AppxManifest.xml(29,10): error 0x80080204: Cannot register the cdcb7227-8423-4f7a-9570-abd83cb255e0_1.0.0.0_x64__8vkwcx8tjcnay package because EntryPoint must be specified when SupportsMultipleInstances is true.

参考How to use an In-Process AppService in a Multi-Instance UWP app

(更新完)

2019-11-23更新

在生成的AppX包中的AppXPackage.xml里面,AppService转化为了下面这种形式:

  <Extensions>
    <Extension Category="windows.activatableClass.inProcessServer">
      <InProcessServer>
        <Path>CLRHost.dll</Path>
        <ActivatableClass ActivatableClassId="MyAppService.Inventory" ThreadingModel="both" />
        <ActivatableClass ActivatableClassId="Microsoft.UI.Xaml.Markup.ReflectionXamlMetadataProvider" ThreadingModel="both" />
      </InProcessServer>
    </Extension>
  </Extensions>

(更新完)

2020-10-20更新

Develop Apps > Communication

App-to-app communication

Solving UWP App-to-App Communication in IoT Apps

Using cross-app communication to make apps work together (10 by 10)

几种UWP App间共享数据的办法:

(更新完)