我可以在 Azure 上使用 .NET 執行 IronBarcode 嗎?
是的! IronBarcode 可用於在 Azure 服務上託管的 .NET 應用程式中讀取和寫入 QR/條碼。 IronBarcode 在多個 Azure 平台上進行了全面測試,包括 MVC 網站、Azure Functions 等。
先決條件
1. 安裝 IronBarcode 開始使用
首先,在NuGet網站上安裝NuGet套件。
作為替代方案,可以下載IronBarcode.dll並添加到您的專案中。
操作指南
2. 效能和 Azure 層
我們建議至少使用 Azure B1服務計劃,因為它適合我們大多數使用者的使用情境。 需要更高吞吐量的系統需要更高等級的服務套餐。
3. 框架選擇
.NET Standard、Core 和 Framework 項目都與 IronBarcode 兼容。
4. Azure 上的 Docker
想要在 Azure 上使用 IronBarcode 來控制效能和穩定性,一種方法是使用 Docker。 要了解如何將IronBarcode與Azure和Docker一起使用,請查看此教學。
5. 官方 Azure 函數支援
IronBarcode 目前支援 Azure Functions V3 和 V4。
Azure 函數程式碼範例
已在 Azure Functions v3.3.1.0+ 上測試。 以下是一個程式碼範例:
using System;
using System.Net;
using System.Net.Http;
using IronBarCode;
using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http.Headers;
public static class BarcodeFunction
{
// Azure Function triggered by HTTP request.
[FunctionName("barcode")]
public static HttpResponseMessage Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
// Set the license key for IronBarcode if needed.
IronBarCode.License.LicenseKey = "Key";
// Create a QR barcode from a string.
var myBarCode = BarcodeWriter.CreateBarcode("IronBarcode Test", BarcodeEncoding.QRCode);
// Prepare the HTTP response to return the barcode image.
var response = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(myBarCode.ToJpegBinaryData())
};
// Set content headers for attachment and content type.
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = $"{DateTime.Now:yyyyMMddmm}.jpg"
};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
return response;
}
}Imports System
Imports System.Net
Imports System.Net.Http
Imports IronBarCode
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.Extensions.Logging
Imports System.Net.Http.Headers
Public Module BarcodeFunction
' Azure Function triggered by HTTP request.
<FunctionName("barcode")>
Public Function Run(
<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> req As HttpRequest,
log As ILogger) As HttpResponseMessage
log.LogInformation("VB.NET HTTP trigger function processed a request.")
' Set the license key for IronBarcode if needed.
IronBarCode.License.LicenseKey = "Key"
' Create a QR barcode from a string.
Dim myBarCode = BarcodeWriter.CreateBarcode("IronBarcode Test", BarcodeEncoding.QRCode)
' Prepare the HTTP response to return the barcode image.
Dim response = New HttpResponseMessage(HttpStatusCode.OK) With {
.Content = New ByteArrayContent(myBarCode.ToJpegBinaryData())
}
' Set content headers for attachment and content type.
response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {
.FileName = $"{DateTime.Now:yyyyMMddmm}.jpg"
}
response.Content.Headers.ContentType = New MediaTypeHeaderValue("image/jpeg")
Return response
End Function
End ModuleImports System
Imports System.Net
Imports System.Net.Http
Imports IronBarCode
Imports Microsoft.AspNetCore.Http
Imports Microsoft.Azure.WebJobs
Imports Microsoft.Azure.WebJobs.Extensions.Http
Imports Microsoft.Extensions.Logging
Imports System.Net.Http.Headers
Public Module BarcodeFunction
' Azure Function triggered by HTTP request.
<FunctionName("barcode")>
Public Function Run(
<HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route := Nothing)> req As HttpRequest,
log As ILogger) As HttpResponseMessage
log.LogInformation("VB.NET HTTP trigger function processed a request.")
' Set the license key for IronBarcode if needed.
IronBarCode.License.LicenseKey = "Key"
' Create a QR barcode from a string.
Dim myBarCode = BarcodeWriter.CreateBarcode("IronBarcode Test", BarcodeEncoding.QRCode)
' Prepare the HTTP response to return the barcode image.
Dim response = New HttpResponseMessage(HttpStatusCode.OK) With {
.Content = New ByteArrayContent(myBarCode.ToJpegBinaryData())
}
' Set content headers for attachment and content type.
response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("attachment") With {
.FileName = $"{DateTime.Now:yyyyMMddmm}.jpg"
}
response.Content.Headers.ContentType = New MediaTypeHeaderValue("image/jpeg")
Return response
End Function
End Module在此程式碼中
- 我們定義了一個名為「barcode」的 Azure 函數。
- 此函數由 HTTP 請求觸發,並在處理後記錄一則訊息。
- 我們為IronBarcode指定授權金鑰(用您的實際授權金鑰替換
"Key")。 - 使用
BarcodeWriter.CreateBarcode生成QR碼條碼。 - 條碼圖像被轉換為JPEG格式並包含在HTTP回應中。
- 回覆內容將以可下載附件的形式提供,檔案名稱包含目前日期和時間。
- 回應內容類型設定為“image/jpeg”,以指示圖片格式。
常見問題
IronBarcode可以與Azure上的.NET應用程式一起使用嗎?
是的,IronBarcode可用於在Azure服務上託管的.NET應用程式中讀取和寫入QR/條碼,例如MVC網站和Azure Functions。
推薦的IronBarcode Azure服務計劃是什麼?
建議在大多數使用案例中至少使用Azure B1服務計劃,對於需要更高吞吐量的系統則需要更高級別的服務計劃。
IronBarcode在Azure上與哪些.NET框架兼容?
IronBarcode在Azure上兼容.NET Standard、Core和Framework專案。
Docker如何與Azure上的IronBarcode一起使用?
在Azure上使用Docker可以在部署IronBarcode時幫助控制效能和穩定性,有教程可供指導。
IronBarcode是否正式支持Azure Functions?
是的,IronBarcode支持Azure Functions V3和V4,允許在Azure無伺服器計算中整合條碼功能。
如何在Azure Function中使用IronBarcode生成和使用條碼?
在 Azure Function 中,可以使用 BarcodeWriter.CreateBarcode 生成條碼,並作為影像檔在 HTTP 回應中返回。
在使用IronBarcode的Azure Functions中為條碼影像設定什麼內容類型?
當從使用IronBarcode的Azure Function中的HTTP回應返回時,條碼影像的內容類型被設置為'image/jpeg'。
在Azure Functions中使用IronBarcode是否需要設置授權金鑰?
是的,在Azure Functions中使用IronBarcode需要設置授權金鑰以啟用程式庫並移除任何限制。
IronBarcode在Azure Functions中生成的條碼使用什麼文件格式?
IronBarcode在Azure Functions中生成的條碼被轉換為JPEG格式以供HTTP回應使用。
在使用IronBarcode的Azure Functions中如何設置條碼影像的內容標頭?
內容標頭被設定為將條碼影像定義為可下載附件,檔名中包含當前日期和時間。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。