.NET, BrokeredComponent, UWP, XAML

Part 2: Brokered Component for UWP App Starter Kit

My previous blog about brokered components demonstrate a procedure on how to consume a brokered component in a windows store app (Universal windows). It described some details and a lot of steps to get started with brokered components. I’m assuming that you have already read the previous blog post about brokered component or you have enough knowledge about it. Now to make it easier, I created a brokered component starter kit inside the VSIX installer together with the previous brokered component project templates. You can download the templates in visual studio gallery. There will be a new project template called Brokered Component Solution. This will create a solution that has three projects, the client, the server and the proxy.

Here are the steps on how to use the brokered component starter kit:
1. Download the Brokered Component Templates from visual studio gallery or update the extension if you have already downloaded it.
2. Run Visual Studio 2015 as an Administrator and create a new project.
3. In the new project dialog, try to search for “Brokered Component Solution”

bcsolution
4. Type in the Solution name and click ok.
5. A Setup dialog box will appear. Type in the desired names for the client, proxy and the server project then click OK.

Setup Dialog
6. It will generate a new solution with the three projects in it.

Projects
7. Build the server project (right click on the project then build).
8. Right click on the proxy project and add existing item.
9. Add the files generated by the server (dlldata.c, server_i.c, server_p.c, server.h)

Addexistingcfiles

Add existing
10. Build the Proxy project.
11. Right click on the Client project then add a new reference.
12. Click browse and then locate the reference folder of the server, then select server.winmd (i.e. Server\Bin\Debug\Reference\Server.winmd).

addref

ref

refwinmd.PNG
13. Set the Client project as the Start up project.
14. Hit F5 to run the client project.

app.PNG

The new template takes care of a bunch of things such as the registration of proxy.dll, the setup of app manifest, the copying of dlls to the debug folder. If you want to know more details about the brokered component, you can check my previous post or read about the brokered component white paper.

One last tweak, this is optional by the way. We don’t want to build each project one by one everytime there’s a change in the server. To do that, we need to configure the project dependencies. In the solution explorer, right click on the solution then click Project Dependencies. We want to make sure that the Client is dependent on both Proxy and Server and the Proxy is dependent on the Server. This will give us a build order of Server->Proxy->Client.

You can download the sourcecode from GitHub

 

Advertisements

49 thoughts on “Part 2: Brokered Component for UWP App Starter Kit

  1. I have a weird behavior when I try to play IAsyncOperation, for example if I replace GetValue in your Hello World example by this :
    public IAsyncOperation GetValueAsync()
    {
    return Task.Run(() =>
    {
    Thread.Sleep(3000);
    return “Hello .NET world”;
    }).AsAsyncOperation();
    }

    and then replace Button_Click in MainPage by :
    private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
    //Just an exmple of invoking brokered component.
    Server.TestAsyncComponent bc = new Server.TestAsyncComponent();
    string brokeredComponentValue = await bc.GetValueAsync();
    this.Display.Text = brokeredComponentValue;

    Debug.WriteLine(brokeredComponentValue);
    }

    It works perfectly in Debug Mode, but in Release Mode it seems to crash.

    Do you have any idea ?

    Like

  2. I found that UseDotNetNativeToolchain was default set to true in csproj in Realease mode (false in Debug mode). You should update your template.

    Like

  3. Hi lawrencecontreras
    i followed the step by step as mention in part 2(uwp)
    its run successfully but when i click on button its shows me error

    An exception of type ‘System.InvalidCastException’ occurred in Client.exe but was not handled in user code

    Additional information: Unable to cast object of type ‘Server.BrokeredComponent1’ to type ‘Server.IBrokeredComponent1Class’.

    i try lot but i can’t solve it.
    i need it solve urgent. plz help

    Like

    1. The reason you’re getting that is that you might not be running as an administrator. Another thing is that you need to make sure that theproxy is registered using regsvr32. Try to run visual studio as administrator ifthat didnt work, try to manually register the generated proxy and lastly check the appxpackage file, view the xml and try to double check if the path to the proxy is correct.

      Like

      1. yeeeppp… it works.. i have to run cmd as administrator and register the proxy.dll using regsvr32. Thank you very much sir..you help me

        Like

      1. i want a log off button or shutdown button in uwp with brokered-component. when user click on button system get log off or shut down.

        Like

      2. You want to shut down your device from your app? I really think that it’s beyond this topic. The idea is to be able to call native .NET codes from UWP/WinRT app. Now since you can do that via brokered component, you should be able to write your own logic of shutting down or logging off. You might wanna do that in your server project.

        Like

  4. Thank you sir for replay… i am facing another problem is that when i follow same steps in my friends laptop.the server does not generate (dlldata.c, server_i.c, server_p.c, server.h) its shows build succeeded but not generated files. now i cant able add these file from proxy. I can’t figure out why this happen.

    Like

  5. How can i run server as administrator? My problem is that i’m doing System.IO.StreamWriter to “c:\” on BrokeredComponent1 but it does not have permissons. Any other folder does not have any problem. Thanks in Advance

    Like

  6. That was my first thought, dind’t work 😦 I have tried this type solution so i can write a file in c:\ , but because of this administartor problem i can now write to any folder in my computer but not c:\ 😦

    Like

  7. how to implement background process with timer using brokered component?how can we add db layer(class Library) in client brokered component? problem is that can’t able add reference file(dll) in the project. please help me sort out this problem.

    Like

    1. I answered this on my post below. The key is to build in RELEASE configuration, install the Visual Studio Redistributable to the client, copy the brokered component libraries in the path that you defined in the appxmanifest.

      Like

  8. i have created a package with brokered component its work fine when i install it my system. when same package i install in my friend system its installed smoothly . But When I execute the uwp app it crash every time.I think server.Winmd file or proxy.dll not get registered .Please help me how i install the sideloaded uwp app with brokered component in other system or tablets.

    Like

    1. Remember to build it in RELEASE configuration. Probably the reason it worked on both you and you’re friend’s system is because both of you has visual studio installed that has all the debug utilities on those machines. If you want to deploy to a fresh machine, there’s a requirement to install Visual Studio redistributable on that machine.

      Here’s the link on where to download the redistributable installer.
      https://www.microsoft.com/en-us/download/details.aspx?id=48145

      I used InnoSetup to install my app. The setup script includes the copying of the files needed by the brokered component, the installation of redistributable and the registration of the proxy. You can create a single file setup that will do everything for you using the InnoSetup. The only problem with this is that you can’t uninstall the Windows App, you can only uninstall the .NET libraries of the brokered component.

      Like

      1. Thank you for reply..I follow all the steps as above comments
        1.build it in RELEASE configuration
        2.install redistributable installer
        3.copy the brokered component libraries in the path that defined in the appxmanifest.
        but still facing problem please help.. my app install successfully in others system but when i run the app and click the button it crash…. i track the error and found the following error

        System.InvalidCastException: InvalidCast_WinRT
        at SharedLibrary!+0x2fbd0c
        at SharedLibrary!+0x340967
        at SharedLibrary!+0xed8de
        at SharedLibrary!+0x105065
        at SharedLibrary!+0x12b61e
        at Server.BrokeredComponent1.GetValue()
        at Client.MainPage.Button_Click(Object sender, UI.Xaml.RoutedEventArgs e)

        app having no problem to instantiated the brokered component
        Server.BrokeredComponent1 bc = new Server.BrokeredComponent1();

        but when the function called(bc.GetValue();) its gives error as above .
        need your help urgent..

        Like

  9. Looks like the proxy.dll is not registered. Make sure you registered the correct proxy and the winmd file is in the path where the proxy is. Have you registered the proxy using regsvr32 in other machines?

    Like

    1. Now its work fine.. I registered the winmd file using
      icacls . /T /grant “ALL APPLICATION PACKAGES”:RX
      and proxy.dll using regsvr32.. after that magic starts…its working..
      Thanks for your quick reply..Thank you.

      Like

  10. I got Assets\Store Logo.png not found during installing the uwp app package. I know it is not brokored component problem. Please help me out solve this problem.

    Like

  11. First off, great instructions, I actually got the sample project to work. The problem arises when I try to apply the technique to an existing project. I worked through a lot of the errors and I’m getting this now:

    Unable to cast COM object of type ‘WebPointOfSaleConduit.BrokeredComponent1’ to interface type ‘WebPointOfSaleConduit.IBrokeredComponent1Class’. This operation failed because the QueryInterface call on the COM component for the interface with IID ‘{B9E18572-0CCC-53FD-4909-BB5B8BC2BB6B}’ failed due to the following error: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

    regsvr32 works on the proxy object, it’s targeting x86, and visual studio is running in administrator mode.

    Have you see this message before?

    Like

    1. This is a common error when the brokered component proxy has not been updated. If you have changes in your project, you need to kill the running proxy at the background by killing the dllhost.exe process or the COM Surrogate process in your task manager. Then build the server, build the proxy, register the proxy and start the app. Remember that the proxy and the server generated winmd file should be in the exact location specified in the appx manifest. To understand more about brokered component, try to read the first part of this blog.

      Like

  12. Hi Lawrence,
    Thanks for this template. Is there any way i can make it compatible with x64 apps? I’ve only managed to get it running with x86 so far.

    Like

  13. Hi, I have a bit of an issue with adding certain nuget packages to the server component. To replicate this problem I have started with a new project, and followed the tutorial above to make it work. It does work, but if I download the “PdfSharp” or “iTextSharp” nuget packages and then reference them in the provided “GetValue()” method, it throws the following exception:

    “An exception of type ‘System.IO.FileNotFoundException’ occurred in Client.exe and wasn’t handled before a managed/native boundary
    WinRT information: System.IO.FileNotFoundException: Could not load file or assembly ‘PdfSharp, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb’ or one of its dependencies. The system cannot find the file specified.”

    The dlls are is copied into the bin folder, so they do exist. I also started a new console application that’s setting were the same as the server component’s setting (other than the output type) and it could run the packages. I’ve been struggling with these for a few days now and any help would be appreciated.

    Thanks,
    Mike

    Like

  14. Hi Lawrence,
    When I tried to register Proxy.dll in a fresh machine running x64 windows 10 that has no VS, I’ve got this error “The module “Proxy.dll” failed to load.” remember that I have installed Visual C++ Redistributable for Visual Studio 2015, build in Release mode and Solution Configuration on x86 ( I can’t build in x64 configuration).
    Thank you so much.

    Like

  15. Hello Lawrence,

    have you tried to implement this kind of thing on your brokered before

    if so any tips that could help me .

    Thanks in advance.

    Like

  16. I am attempting to use the template you gave, but it fails to build the proxy project. Gives this error
    A problem was encountered creating the sub project ‘Proxy’. Exception from HRESULT: 0x90041FE2

    When I attempt to add or create a proxy project manually it says “The method or operation is not implemented”

    Any chance I could get some guidance on this?

    Like

  17. I’m running this on VS2015 and it works like a CHARM! 5* ! However – I need to open the project in VS2017 – not the template – but the projects created. All works like a charm, but I run into a problem with the PostBuild event on the server part, failing with err 9009. I will happily pay you to help out with this one…

    Like

    1. Hi Tommy, the reason you’re getting an error in VS 2017 is because 2017 does not have its vcvarsall.bat file. It’s one of the batch file being called in the post build event. That means you can still run brokered component on VS2017 but you are still dependent on the vcvarsall.bat from the VS2015 folder.

      To cut the long story short here’s the post build event I use in VS2017 project.

      call “C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat” x86

      md “$(TargetDir)”\impl
      md “$(TargetDir)”\reference

      erase “$(TargetDir)\impl\*.winmd”
      erase “$(TargetDir)\impl\*.pdb”
      rem erase “$(TargetDir)\reference\*.winmd”

      xcopy /y “$(TargetPath)” “$(TargetDir)impl”
      xcopy /y “$(TargetDir)*.pdb” “$(TargetDir)impl”

      md “$(SolutionDir)$(ConfigurationName)”

      xcopy /y “$(TargetDir)*.winmd” “$(SolutionDir)..\BuildAll\$(Configuration)”
      xcopy /y “$(TargetDir)*.pdb” “$(SolutionDir)..\BuildAll\$(Configuration)”
      xcopy /y “$(TargetDir)*.config” “$(SolutionDir)..\BuildAll\$(Configuration)”

      winmdidl /nosystemdeclares /metadata_dir:”C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.15063.0″ “$(TargetPath)”

      midl /metadata_dir “C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.15063.0” /iid “$(ProjectDir)..\Raven.ViewModel.Interface.BC.Proxy\$(TargetName)_i.c” /env win32 /x86 /h “$(ProjectDir)..\Raven.ViewModel.Interface.BC.Proxy\$(TargetName).h” /winmd “$(TargetName).winmd” /W1 /char signed /nologo /winrt /dlldata “$(ProjectDir)..\Raven.ViewModel.Interface.BC.Proxy\dlldata.c” /proxy “$(ProjectDir)..\Raven.ViewModel.Interface.BC.Proxy\$(TargetName)_p.c” “$(TargetName).idl”

      mdmerge -n 1 -i “$(ProjectDir)bin\$(ConfigurationName)” -o “$(TargetDir)reference” -metadata_dir “C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.15063.0” -partial

      echo ‘mdmerge -n 1 -i “$(ProjectDir)bin\$(ConfigurationName)” -o “$(TargetDir)reference” -metadata_dir “C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.15063.0” -partial’
      rem erase “$(TargetPath)”

      Like

  18. Hi, i followed the step by step as mention in part 2(uwp), but when i start client project i get this error:The command “echo “Registering the proxy using Regsvr32”
    call “%windir%\System32\regsvr32” “c:\users\m-kurenkov\documents\visual studio 2015\Projects\BrokeredComponentApp2\Debug\Proxy.dll” /s

    echo “Allowing access to the proxy assemblies using icacls”
    call “%windir%\System32\icacls” “c:\users\m-kurenkov\documents\visual studio 2015\Projects\BrokeredComponentApp2\Debug\*.dll” /T /grant “ALL APPLICATION PACKAGES”:RX” exited with code 1332

    How can i solve this problem? I’m registering Proxy.dll, but it has not solved the problem

    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