The Application of fo-dicom in AspNet WebApi throws an exception: No codec registered for tranfer syntax:, webapifo-dicom
Background: In a Dicom Web Service, the WADO-RS needs to parse TransferSyntax, and then the DicomFile. ChangeTransferSyntax method in fo-dicom is used. Code similar to: var df = DicomFile. Open (samplesDir + @ "\ User Submitted \ overlays. dcm"); df = df. ChangeTransferSyntax (DicomTransferSyntax. 2000lossless );
Problem: The above code runs normally in the Console (exe) and throws an exception in the WebApi service: No codec registered for tranfer syntax:
Cause: After viewing the fo-dicom source code, we found that DicomCodec was initialized with MEF in the static Construction Method of DicomTranscoder. Code similar to: static DicomTranscoder () {LoadCodecs (null, "Dicom. Native *. dll ");}
Public static void LoadCodecs (string path = null, string search = null) {if (path = null) {path = Path. getDirectoryName (new Uri (Assembly. getExecutingAssembly (). codeBase ). localPath );}
Var log = LogManager. Default. GetLogger ("Dicom. Imaging. Codec ");
Var catalog = (search = null )? New DirectoryCatalog (path): new DirectoryCatalog (path, search); var container = new CompositionContainer (catiner); foreach (var lazy in container. getExports <IDicomCodec> () {var codec = lazy. value; log. debug ("Codec: {0}", codec. transferSyntax. UID. name); _ codecs [codec. transferSyntax] = codec ;}}
When it is the Console, the above path is the path of the exe, so the MEF mechanism can find Dicom. Native. DLL, and then you can get the Codecs.
When it is AspNet WebApi, the above path is C: \ Windows \ Microsoft. NET \ Framework \ v4.0.30319 \ Temporary ASP. NET Files \ root \ e298f90d \ bf8a423a \ assembly \ dl3 \ 2196513d \ b06fccd3_jwbd001. Each DLL is in an independent path, and Dicom cannot be found in the WebApi dll path. native. DLL, so Codecs cannot be obtained, and thus the exception No codec registered for tranfer syntax is thrown:
Solution: In WebApiConfig. add the following code to the Register Method: var path = Path. getDirectoryName (new Uri (Assembly. getExecutingAssembly (). codeBase ). localPath); Dicom. imaging. codec. dicomTranscoder. loadCodecs (path, "Dicom. native *. dll ");
Copyright Disclaimer: This article is an original article by the blogger. You are welcome to reprint it. Please sign it only.