How can I reference nuget Hl7.Fhir.DSTU2 and Hl7.Fhir.STU3 in the same c# Web application?

172 views
Skip to first unread message

bobpat...@gmail.com

unread,
Aug 26, 2019, 10:44:18 PM8/26/19
to FHIR DOTNET
Is there a way to reference Nuget Package Hl7.Fhir.DSTU2 and Hl7.Fhir.STU3 in the same c# Web application without getting the error?  

I'm getting "Error CS0433 The type 'FhirJsonParser' exists in both 'Hl7.Fhir.DSTU2.Core, Version=1.2.0.0, Culture=neutral, PublicKeyToken=d706911480550fc3' and 'Hl7.Fhir.STU3.Core, Version=1.3.0.0, Culture=neutral, PublicKeyToken=d706911480550fc3'

I have a .Net Core Web application that is using Hl7.Fhir.DSTU2 Version="1.2.0" to connect to Apis from Epic and Cerner using OAuth2 and maps to Hl7.fhir.Model.Conformance(DSTU2)
I now need to access STU3 Apis and I want to be able to use the STU3 HL7.Fhir.Models in addition to DSTU2 HL7.Fhir.Models

Brian Postlethwaite

unread,
Aug 26, 2019, 10:51:22 PM8/26/19
to FHIR DOTNET
The details on how to do this are on this page where I use this same approach.
https://github.com/brianpos/fhirpathtester


Need to hand edit the csproj file to perform the assembly aliasing
ChangeAliasesOfStrongNameAssemblies

Then you can use the extern alias code as required as shown in this file.
https://github.com/brianpos/FhirPathTester/blob/feature/uwp-cleanups/WPF/FhirPathProcessor.cs
If you are only working with 1 of the versions in the source file, only extern alias and add a using for the version you're interested in.
This also works for the Specification project too.

The assembly files have been named so that they won't cause you any problems, however the specification.zip file hasn't had that treatment, so you'll need to be careful with that if you need to.

p.s. If you want to try out the app, its in the Microsoft Windows Store

Pål Øystein Carlsen

unread,
Aug 27, 2019, 3:36:12 AM8/27/19
to FHIR DOTNET
Here is what you add to your csproj file.

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Hl7.Fhir.DSTU2.Core'">
        <Aliases>dstu2</Aliases>
      </ReferencePath>
    </ItemGroup>
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Hl7.Fhir.STU3.Core'">
        <Aliases>stu3</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

In your code you put this at the very top of your source file(above other "includes") 
extern alias stu3;
//Declare a dstu2 and stu3 resource Bundles
var 
dstu2Response =  new dstu2::Hl7.Fhir.Model.Bundle(); 
var stu3Response =  new stu3::Hl7.Fhir.Model.Bundle();

This has worked like a charm for me, but last week Visual Studio 2019 started to behave like the resources does not exist. It marks HL7 resources referenced with alias in red as unknown resource. Building works like before, but the editor does not look good. I assumes htis has something to do with the latest update to Visual Studio 2019, or perhaps it could be ReSharper creating this trouble

Pål Øystein Carlsen

unread,
Aug 27, 2019, 3:54:16 AM8/27/19
to FHIR DOTNET
I found out that my editor issue was related to ReSharper. I dissabled and enabled this extension, and this resolved my issue :)

bobpat...@gmail.com

unread,
Aug 27, 2019, 8:04:01 PM8/27/19
to FHIR DOTNET
Great. Thanks.   I was able to get it working by using all of your examples.


Added this to Project File: .csproj

<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Hl7.Fhir.DSTU2.Core'">
        <Aliases>dstu2</Aliases>
      </ReferencePath>
    </ItemGroup>
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'Hl7.Fhir.STU3.Core'">
        <Aliases>stu3</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

Add this to the top of the file that will use the dstu2 Models, Rest, Serialization etc... 


extern alias dstu2; 
using dstu2.Hl7.Fhir.Model; 
using dstu2.Hl7.Fhir.Rest; 

namespace Foo.Services
{
public class PatientService
{...
...
var client = new FhirClient(baseUrl)
{
PreferredFormat = ResourceFormat.Json
};

client.OnBeforeRequest += (object sender, BeforeRequestEventArgs e) =>
{
e.RawRequest.Headers.Add("Authorization", "Bearer " + accesstoken);
};



Reply all
Reply to author
Forward
0 new messages