.NET, BrokeredComponent, UWP, XAML

Part 1: Brokered Component for UWP App

Why use brokered component?
I am currently working on migrating a legacy application built in .net and some c++ libraries to windows universal application (UWP). Brokered winRT component is one way for us to be able to access legacy functionality on a side loaded application. As of the moment there’s an existing template for creating brokered component for visual studio 2013 targeting windows 8.1 application but not for VS2015 targeting windows 10 universal app. I tried to read the whitepaper  and tried to create a template that can be used for universal windows app.

Brokered component allows side loaded application to access or call functionalities of legacy .net applications. You can download the project templates I created from visual studio gallery. The extension contains three project template, the Brokered Client, Brokered Proxy and the Brokered Server.

Click here to download the project templates installer.

Here are the basic steps to create a brokered component for universal windows apps using the project templates.

First we have to create the projects.

Pre requisite
1. Create a client Project
– Go to File>New>Project then search for Brokered Client.
2. Create a server component project
– Right click on solution, Add>New Project and search for Brokered Server
3. Create a Brokered Proxy project
– Right click on solution, Add>New Project and search for Brokered Proxy

Once you have all projects in place we can start configuring it.

Server Component
Server component is a project where we can reference or use native .net codes such as legacy dlls. This project will generate a .winmd file which we can reference later on in our client application (Universal Windows). This will also generate some files that we are going to add to our proxy project later on.

1. You can start creating your BrokeredComponent class (i.e. BrokeredComponent1). Rename it and write your implementation in this class. You can write native .net code in this class.
2. Right click on the Server project and click properties.
3. Go to the Build Event tab and look at the post build event.
4. Make sure that all the reference to the Proxy project’s path is correct. Change the folder name ‘Proxy’ to the name of your Proxy project.

Note: You can copy the build event to notepad or notepad++ to make it easier to edit. You should be replacing 4 instances of Proxy in the post build event script. You will only do this if your proxy project’s name is not ‘Proxy’.replaceproxy
5. Build the server component project
6. Four files will be generated in the folder where the proxy project is.
– Dlldata.c
– A header file (e.g. Server.h)
– A *_i.c file (e.g. Server_i.c)
– A *_p.c file (e.g. Server_p.c)
7. A new folder in {SolutionFolder}\Debug will be created if it doesn’t exist yet.
8. Metadata files (.winmd) will be generated in the Debug\impl and Debug\reference. The file in the impl is the implementation file which is automatically copied to the {SolutionFolder}\Debug. And the one in the reference folder is the one that the client will use by adding it as reference.

Note: The folder structure is very critical in the post build event. Make sure you follow the steps precisely or you can customize the post build event so that it can work on the folder structure that you desire. 

Proxy
This project will generate a dll which we will register later on using regsvr32.

1. Right click on the Proxy project from the solution explorer
2. Click on Add\Existing items
3. Add the files that was generated by the Server (Dlldata.c, Server.h, Server_i.c, Server_p.c)
4. Build the proxy project
6. It will generate the proxy dll in the {SolutionFolder}\Debug

Register
1. Open the command prompt
2. Go to the {SolutionFolder}\Debug
3. Enter the following command:
icacls . /T /grant “ALL APPLICATION PACKAGES”:RX
4. Register the dll by entering:
Regsvr32 Proxy.dll

Client
This is the side-loaded universal windows app.

1. Right click on the package.appmanifest, then click “View Code”
2. Scroll down to the Extension tag
3. In the ActivatableClass tag, type in the fully qualified name of the server class (i.e Server.BrokeredComponen1) in the ActivatableClassId attribute
4. In the ActivatableClassAttribute tag type in the path to the {SolutionFolder}\Debug or anywhere where the proxy together with the .winmd file is in. By default it will be generated in the {SolutionFolder}\Debug.
5. Right click on the Project then add reference then click browse and then go to {Server Project Folder}\bin\Debug\reference\Server.winmd file.
6. You can now create an instance of the class (BrokeredComponent1) in the client project.

Conclusion
The idea of the brokered component is to generate a reference file (.winmd) that we can reference in the UWP, an implementation file (.winmd) and a proxy dll that will be registered to the system. The implementation winmd file should be in the folder where the proxy.dll file is and the reference\Server.winmd should be the one that we reference in the client project. I have created some project templates to skip the project files configuration steps that you can see in the white paper.

Brokered component cannot be submitted to the windows store. It won’t even pass the local certification process using the Windows Application Certification Kit. You can install the app by sideloading. I’m trying to figure out a good way to deploy sideloaded apps to other devices. I’ll be discussing it on my next post.

References:
https://msdn.microsoft.com/en-us/library/windows/apps/dn630195.aspx

http://blogs.msdn.com/b/wsdevsol/archive/2014/04/14/cheat-sheet-for-using-brokered-windows-runtime-components-for-side-loaded-windows-store-apps.aspx

Update:

You can now download the latest version of the Templates that includes the Brokered Component Starter Kit.

Advertisements

20 thoughts on “Part 1: Brokered Component for UWP App

  1. Hello I know that this topic is not part of my question I hope you consider it .
    By the way here it is . Is it possible to add parameters in brokered class like this see below

    public void ReadMeWithParams(List itemparams) {}

    And In the app side call the class like this

    List items = new List(){}
    items.add{..}

    var call = new Class();
    call.ReadMeWithParams(items);

    BTW thanks for your blog. It save my day how to set up it . You are awesome 🙂

    Liked by 1 person

  2. I seem to have this issue with the project templates regardless what I try.

    I create everything correctly. So I try to call it in my Application.

    I instantiate:
    var object= new BrokeredClass.BrokeredComponent();

    then I call a function:

    object.SomeFunction();

    The problem is when calling this function regardless of how it’s implemented, I get a InvalidCastException,
    Additional information: Unable to cast object of type ‘BrokeredClass.BrokeredComponent’ to type ‘BrokeredClass.IBrokeredComponentClass’. The interface is not defined in the projects and I haven’t been able to determine where the issue is.

    Any ideas?

    Like

    1. You can try to double check if the proxy.dll is registered (regsvr32) and the icacls command is executed. If you are using the starter kit, you need run Visual Studio as administrator and check the build events if it executed successfully.

      Like

      1. That’s done too. I checked and rechecked every step here. Even did the Brokered Component Starter Kit and just added one of the functions instead of the Hello world it generates. Nothing changed.

        Like

  3. I also encountered the issue you mentioned before. But like what I said, the solution that I did was to make sure that the proxy.dll is registered and the icacls was executed. One way to check if your proxy.dll is registered, you can see if the proxy was running in task manager, check for a process named dllhost.exe (COM Surrogate). Have you tried Hello World function if it worked? You might want to try to kill the dllhost process, unregister (regsvr32-u) the proxy, rebuild everything and register again, then run. I’m really not sure if that’s the correct solution but in my app, I always kill the process when closing the window.

    Like

    1. Hi,i am trying to register brokered component dll in windows 10 tablet device using regsvr32.but i m getting below error
      “The module “xxxxx.dll” failed to load.
      Make sure the binary is stored at the specified path or debug it to check for problems with the binary or dependent .DLL files.

      could you pls help me to sort out this issue.
      Note:I don’t have visual studio in tablet

      Like

      1. most of the issue resolve by installing
        Visual C++ Redistributable Packages for Visual Studio
        and on the Visual Studio editor the build should be on the “release” state

        Like

  4. I’m having problem registering the dll
    —————————
    RegSvr32
    —————————
    The module “ToggleProxy.BrokeredProxy.dll” was loaded but the call to DllRegisterServer failed with error code 0x80070005.

    For more information about this problem, search online using the error code as a search term.
    —————————
    OK
    —————————

    Like

    1. first thing to check to debug in order to register the .dll file is to check if the machine had already installed “Visual C++ Redistributable Packages for Visual Studio” if already installed then run it as administrator as sensie lawrencecontreras told :).

      Liked by 1 person

  5. Hi Lawrence,
    What are the step I should follow to make this Template compatible with x64 apps ? Simply setting the builds to x64 doesn’t work..

    Like

    1. You can create a project in VS2015 and change the post build event, I’ve written my post build event for VS2017 on one of my reply to the other comment. Then you can run that project in VS 2017. I might release a template for VS2017 but it will still require VS2015 installed.

      Like

  6. Hi,Lance. Thanks for your blog.
    According to the whitepaper your mentioned, the App Broker only supports 32-bit and the desktop component must be 32-bit. Side-loaded applications can be 64-bit.

    So my App Broker is 32-bit, but if I changed my UWP app platform to x64 then I got the error This operation failed because the QueryInterface call on the COM component for the interface with IID … failed due to the following error: Class not registered.

    And if my UWP app platform target to x86 it works.

    Can you give me some suggestion how to make it work in x64 UWP app?

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s