将 PushStreamContent C# 代码转换为 VB.Net WebAPI 2 时出错
Error converting PushStreamContent C# code to VB.Net WebAPI 2
我在将 this C# code 转换为 WebAPI 2 VB.Net 项目时遇到一些困难。
这是原始的 C# 代码
[HttpGet]
public HttpResponseMessage FromImages() {
var imageStream = new ImageStream();
Func<Stream, HttpContent, TransportContext, Task> func = imageStream.WriteToStream;
var response = Request.CreateResponse();
response.Content = new PushStreamContent(func);
response.Content.Headers.Remove("Content-Type");
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" + imageStream.Boundary);
return response;
}
internal class ImageStream {
public object Boundary { get; private set; } = "HintDesk";
public async Task WriteToStream(Stream outputStream, HttpContent content, TransportContext context) {
byte[] newLine = Encoding.UTF8.GetBytes("\r\n");
foreach (var file in Directory.GetFiles(@"TestData\Images", "*.jpg")) {
var fileInfo = new FileInfo(file);
var header = $"--{Boundary}\r\nContent-Type: image/jpeg\r\nContent-Length: {fileInfo.Length}\r\n\r\n";
var headerData = Encoding.UTF8.GetBytes(header);
await outputStream.WriteAsync(headerData, 0, headerData.Length);
await fileInfo.OpenRead().CopyToAsync(outputStream);
await outputStream.WriteAsync(newLine, 0, newLine.Length);
await Task.Delay(1000 / 30);
}
}
}
这是VB.Net翻译
<HttpGet> _
Public Function FromImages() As HttpResponseMessage
Dim imageStream = New ImageStream()
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = imageStream.WriteToStream
Dim response = Request.CreateResponse()
response.Content = New PushStreamContent(func)
response.Content.Headers.Remove("Content-Type")
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" + imageStream.Boundary)
Return response
End Function
Friend Class ImageStream
Public Function WriteToStream(outputStream As Stream, content As HttpContent, context As TransportContext) As Task
Dim newLine As Byte() = Encoding.UTF8.GetBytes(vbCr & vbLf)
For Each file As var In Directory.GetFiles("TestData\Images", "*.jpg")
Dim fileInfo = New FileInfo(file)
Dim header = "--{Boundary}" & vbCr & vbLf & "Content-Type: image/jpeg" & vbCr & vbLf & "Content-Length: {fileInfo.Length}" & vbCr & vbLf & vbCr & vbLf
Dim headerData = Encoding.UTF8.GetBytes(header)
Await outputStream.WriteAsync(headerData, 0, headerData.Length)
Await fileInfo.OpenRead().CopyToAsync(outputStream)
Await outputStream.WriteAsync(newLine, 0, newLine.Length)
Await Task.Delay(1000 / 30)
Next
End Function
End Class
我发现了两个问题:
这一行是一个'Argument not specified for parameter 'context' of 'Public Function WriteToStream...'
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = imageStream.WriteToStream
重载解析失败,因为无法使用这些参数调用可访问的 'New'
response.Content = 新 PushStreamContent(func)
我想我没有正确翻译这个,但我很难在 VB.Net 中找到任何我想要完成的例子。有人可以告诉我我做错了什么吗?
主要问题是您没有使用 'AddressOf' - 这是 VB 等价物:
Option Infer On
Imports Microsoft.VisualBasic
<HttpGet>
Public Function FromImages() As HttpResponseMessage
Dim imageStream = New ImageStream()
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = AddressOf imageStream.WriteToStream
Dim response = Request.CreateResponse()
response.Content = New PushStreamContent(func)
response.Content.Headers.Remove("Content-Type")
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" & imageStream.Boundary)
Return response
End Function
Friend Class ImageStream
Private privateBoundary As Object = "HintDesk"
Public Property Boundary() As Object
Get
Return privateBoundary
End Get
Private Set(ByVal value As Object)
privateBoundary = value
End Set
End Property
Public Async Function WriteToStream(ByVal outputStream As Stream, ByVal content As HttpContent, ByVal context As TransportContext) As Task
Dim newLine() As Byte = Encoding.UTF8.GetBytes(ControlChars.CrLf)
For Each file In Directory.GetFiles("TestData\Images", "*.jpg")
Dim fileInfo = New FileInfo(file)
Dim header = $"--{Boundary}" & ControlChars.CrLf & "Content-Type: image/jpeg" & ControlChars.CrLf & "Content-Length: {1}" & ControlChars.CrLf & ControlChars.CrLf
Dim headerData = Encoding.UTF8.GetBytes(header)
Await outputStream.WriteAsync(headerData, 0, headerData.Length)
Await fileInfo.OpenRead().CopyToAsync(outputStream)
Await outputStream.WriteAsync(newLine, 0, newLine.Length)
Await Task.Delay(1000 \ 30)
Next file
End Function
End Class
我在将 this C# code 转换为 WebAPI 2 VB.Net 项目时遇到一些困难。
这是原始的 C# 代码
[HttpGet]
public HttpResponseMessage FromImages() {
var imageStream = new ImageStream();
Func<Stream, HttpContent, TransportContext, Task> func = imageStream.WriteToStream;
var response = Request.CreateResponse();
response.Content = new PushStreamContent(func);
response.Content.Headers.Remove("Content-Type");
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" + imageStream.Boundary);
return response;
}
internal class ImageStream {
public object Boundary { get; private set; } = "HintDesk";
public async Task WriteToStream(Stream outputStream, HttpContent content, TransportContext context) {
byte[] newLine = Encoding.UTF8.GetBytes("\r\n");
foreach (var file in Directory.GetFiles(@"TestData\Images", "*.jpg")) {
var fileInfo = new FileInfo(file);
var header = $"--{Boundary}\r\nContent-Type: image/jpeg\r\nContent-Length: {fileInfo.Length}\r\n\r\n";
var headerData = Encoding.UTF8.GetBytes(header);
await outputStream.WriteAsync(headerData, 0, headerData.Length);
await fileInfo.OpenRead().CopyToAsync(outputStream);
await outputStream.WriteAsync(newLine, 0, newLine.Length);
await Task.Delay(1000 / 30);
}
}
}
这是VB.Net翻译
<HttpGet> _
Public Function FromImages() As HttpResponseMessage
Dim imageStream = New ImageStream()
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = imageStream.WriteToStream
Dim response = Request.CreateResponse()
response.Content = New PushStreamContent(func)
response.Content.Headers.Remove("Content-Type")
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" + imageStream.Boundary)
Return response
End Function
Friend Class ImageStream
Public Function WriteToStream(outputStream As Stream, content As HttpContent, context As TransportContext) As Task
Dim newLine As Byte() = Encoding.UTF8.GetBytes(vbCr & vbLf)
For Each file As var In Directory.GetFiles("TestData\Images", "*.jpg")
Dim fileInfo = New FileInfo(file)
Dim header = "--{Boundary}" & vbCr & vbLf & "Content-Type: image/jpeg" & vbCr & vbLf & "Content-Length: {fileInfo.Length}" & vbCr & vbLf & vbCr & vbLf
Dim headerData = Encoding.UTF8.GetBytes(header)
Await outputStream.WriteAsync(headerData, 0, headerData.Length)
Await fileInfo.OpenRead().CopyToAsync(outputStream)
Await outputStream.WriteAsync(newLine, 0, newLine.Length)
Await Task.Delay(1000 / 30)
Next
End Function
End Class
我发现了两个问题:
这一行是一个'Argument not specified for parameter 'context' of 'Public Function WriteToStream...'
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = imageStream.WriteToStream
重载解析失败,因为无法使用这些参数调用可访问的 'New'
response.Content = 新 PushStreamContent(func)
我想我没有正确翻译这个,但我很难在 VB.Net 中找到任何我想要完成的例子。有人可以告诉我我做错了什么吗?
主要问题是您没有使用 'AddressOf' - 这是 VB 等价物:
Option Infer On
Imports Microsoft.VisualBasic
<HttpGet>
Public Function FromImages() As HttpResponseMessage
Dim imageStream = New ImageStream()
Dim func As Func(Of Stream, HttpContent, TransportContext, Task) = AddressOf imageStream.WriteToStream
Dim response = Request.CreateResponse()
response.Content = New PushStreamContent(func)
response.Content.Headers.Remove("Content-Type")
response.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/x-mixed-replace;boundary=" & imageStream.Boundary)
Return response
End Function
Friend Class ImageStream
Private privateBoundary As Object = "HintDesk"
Public Property Boundary() As Object
Get
Return privateBoundary
End Get
Private Set(ByVal value As Object)
privateBoundary = value
End Set
End Property
Public Async Function WriteToStream(ByVal outputStream As Stream, ByVal content As HttpContent, ByVal context As TransportContext) As Task
Dim newLine() As Byte = Encoding.UTF8.GetBytes(ControlChars.CrLf)
For Each file In Directory.GetFiles("TestData\Images", "*.jpg")
Dim fileInfo = New FileInfo(file)
Dim header = $"--{Boundary}" & ControlChars.CrLf & "Content-Type: image/jpeg" & ControlChars.CrLf & "Content-Length: {1}" & ControlChars.CrLf & ControlChars.CrLf
Dim headerData = Encoding.UTF8.GetBytes(header)
Await outputStream.WriteAsync(headerData, 0, headerData.Length)
Await fileInfo.OpenRead().CopyToAsync(outputStream)
Await outputStream.WriteAsync(newLine, 0, newLine.Length)
Await Task.Delay(1000 \ 30)
Next file
End Function
End Class