Closed HiraveBapu closed 4 years ago
I know this is late... however, you can use the Hapi suggested spec for HL7 over HTTP with the correct header. I am including my solution - it's generic, so you'll need to have the correct handlers in the API server for each version, but it gets the IMessages into your controllers:
First you will need a MediaTypeFormatter:
public class Hl7MediaTypeFormatter : MediaTypeFormatter
{
private static readonly Type _supportedType = typeof(IMessage);
public Hl7MediaTypeFormatter()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("x-application/hl7-v2+er7"));
}
public override async Task<object> ReadFromStreamAsync(Type type, Stream readStream, HttpContent content, IFormatterLogger formatterLogger)
{
if (readStream.Length == 0)
return null;
var parser = new PipeParser();
var streamReader = new StreamReader(readStream);
var messageContent = await streamReader.ReadToEndAsync();
return parser.Parse(messageContent);
}
public override async Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)
{
var parser = new PipeParser();
var encodedHl7 = parser.Encode((IMessage)value).Trim();
var streamWriter = new StreamWriter(writeStream);
await streamWriter.WriteAsync(encodedHl7);
await streamWriter.FlushAsync();
}
public override bool CanReadType(Type type)
{
return _supportedType.IsAssignableFrom(type);
}
public override bool CanWriteType(Type type)
{
return _supportedType.IsAssignableFrom(type);
}
}
If you are using the WebApiConfig template, you add the MediaTypeFormatter for the config:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Formatters.Add(new Hl7MediaTypeFormatter());
}
Then you can have your controller accept IMessage parameters in its actions:
public class MyController : ApiController
{
public IMessage Post([FromBody] IMessage message)
{
//... do something with your message
}
}
All you need to do is ensure that your HTTP headers in the request have the Content Type of x-application/hl7-v2+er7.
We used the approach suggested in hapi-hl7overhttp in our asp.net core project and works great. take a look at their specification.
i am looking to accept HL7 messages over HTTP (WebAPI server) in .NET. any suggestions ? currently i am testing with sending HL7 over HTTP from hapiTestPanel to my .NET webApi application. Currently webapi application failed to accept messages or test panel failed to send over HTTP. Any suggestions ?