Search code examples
c#visual-studio-2013background-task

How to avoid circular dependency while adding references to windows runtimecomponent?


I added a windows store Blank App,named app, and a windows RTComponent, named backgroundtask, to my project solution. So, I had to add to add references to backgroundtask in app.

But I also needed to use the data input in the app in Backgroundtask. So, I created a class in app and tried to add references to app in Backgroundtask.

It displayed an error stating circular dependency. How can I use the data fed to app through Backgroundtask and add references to Backgroundtask through app all at the same time?


Solution

  • I'm not sure why Backgroundtask needs to reference app, unless

    • you're trying to access public static variables from the other project, or
    • you're passing an instance of a class from the app project, so Backgroundtask can't access it

    Instead, pass the values from app as parameters to whatever method you're calling in Backgroundtask or, if there are a lot of values to pass, create a third project which contains the classes that need to be shared between both projects.

    • Create a project called Shared and reference it from both of the other projects.

    • Add a new class called AppData (or name it something more specific that seems appropriate).

    • Instantiate the class in app, populate it with the data you need, and pass it to a method in Backgroundtask that accepts a parameter of that class type.

    Either one of those options should eliminate the need for a circular dependency.