private static void CreateDefaultStreamFromVirtualDirectory(NonClosingStream nonClosingStream) { var imagePath = HostingEnvironment.MapPath("~/bin/ODataLogo.jpg"); using (var stream = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { stream.CopyTo(nonClosingStream); } }
private static void CreateDefaultStreamFromAssemblyResource(NonClosingStream nonClosingStream) { using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("ODataSamples.Services.TripPin.ODataLogo.jpg")) { stream.CopyTo(nonClosingStream); } }
private static Stream CreateDefaultStream() { var result = new NonClosingStream(); try { if (HostingEnvironment.IsHosted) { try { // On IIS based deployment, e.g. Azure, we read image from virtual directory CreateDefaultStreamFromVirtualDirectory(result); } catch (Exception) { // to prevent the failure of web service, we read image from assembly resource CreateDefaultStreamFromAssemblyResource(result); } } else { // the code here is for unit test // when unit test, we are unable to get file from a directory CreateDefaultStreamFromAssemblyResource(result); } result.Seek(0, SeekOrigin.Begin); } catch (Exception ex) { throw new ODataServiceException(HttpStatusCode.InternalServerError, "The data source initialization of TripPin service failed.", ex); } return result; }