我可以在 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上使用Docker是获得对IronBarcode性能和稳定性控制能力的一种方法。 要学习如何将IronBarcode与Azure和Docker一起使用,请查看此教程。
5. 官方Azure Function支持
IronBarcode目前支持Azure Functions V3和V4。
工作中的Azure Function代码示例
在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 Function。
- 该功能由HTTP请求触发,并在处理时记录一条消息。
- 我们指定IronBarcode的许可证密钥(将
"Key"替换为您的实际许可证密钥)。 - 使用
BarcodeWriter.CreateBarcode生成一个二维码条形码。 - 条形码图像被转换为JPEG格式,并包含在HTTP响应中。
- 响应内容被设置为可下载附件,文件名为当前日期和时间。
- 响应内容类型被设置为"image/jpeg"以指示图像格式。
常见问题
IronBarcode可以用于Azure上的.NET应用程序吗?
是的,IronBarcode可以用来在Azure服务(如MVC网站和Azure Functions)托管的.NET应用程序中读取和写入QR/条码。
推荐用于IronBarcode的Azure服务计划是什么?
建议在大多数用例中至少使用Azure B1服务计划,对于需要更高吞吐量的系统,需要更高级的服务计划。
哪些.NET框架与Azure上的IronBarcode兼容?
在Azure上运行时,IronBarcode与.NET Standard、Core和Framework项目兼容。
如何在Azure上使用Docker与IronBarcode?
在Azure上使用Docker可以帮助控制IronBarcode部署时的性能和稳定性,并提供指南教程。
Azure Functions是否得到IronBarcode的官方支持?
是的,IronBarcode支持Azure Functions V3和V4,可以在Azure无服务器计算中集成条码功能。
如何在Azure Function中使用IronBarcode生成和使用条码?
在Azure Function中,可以使用BarcodeWriter.CreateBarcode生成条形码,并在HTTP响应中作为图像文件返回。
在Azure Functions中使用IronBarcode时,条码图像设置的内容类型是什么?
使用IronBarcode从Azure Function返回的HTTP响应中的条码图像的内容类型设置为'image/jpeg'。
在Azure Functions中使用IronBarcode是否需要设置许可证密钥?
是的,在Azure Functions中使用IronBarcode需要设置许可证密钥以激活库并去除任何限制。
在Azure Functions中,IronBarcode生成的条码使用何种文件格式?
由IronBarcode在Azure Functions中生成的条码被转换为JPEG格式用于HTTP响应。
在Azure Functions中使用IronBarcode时,如何设置条码图像的内容头?
内容头被设置为将条码图像定义为可下载附件,并将当前日期和时间作为文件名的一部分。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。