无法写入内部事件流

时间:2015-10-21 04:37:50

标签: c# powershell outputstream streamwriter httpserver

我可以尝试运行NHTTP(用于.NET框架的C#编写的简单异步HTTP服务器。您可以在nuget下载此dll,或查看github上的源代码)但我是无法从我的服务器写出我自己对客户端的回复,因为我无法写入OutputStream。

我的powershell脚本:

Add-Type -Path "NHttp.dll"

$server = New-Object NHttp.HttpServer
$server.EndPoint =  new-object System.Net.IPEndPoint ([System.Net.IPAddress]::Parse("127.0.0.1"), 8080)


$RequestReceived = {

    $request = $EventArgs.Request
    $response = $EventArgs.Response

    $global:response = $response.OutputStream.CanWrite

    $writer = New-Object System.IO.StreamWriter($response.OutputStream)
    $writer.AutoFlush = $true
    $writer.WriteLine("Hello World")    


}

Register-ObjectEvent -InputObject $server -EventName RequestReceived -SourceIdentifier RequestReceived -Action $RequestReceived

$server.Start()

始终当我的服务器收到来自客户端的请求时$ global:response = False

事件中发生的错误,在服务器收到请求时发生:

writeErrorStream      : True
PSMessageDetails      : 
Exception             : System.Management.Automation.MethodInvocationException: Exce
                        ption when calling the ".ctor" with "1" argument: "Stream wa
                        s not writable" ---> System.ArgumentException: Stream was no
                        t writable.
                          at System.IO.StreamWriter..ctor(Stream stream, Encoding en
                        coding, Int32 bufferSize, Boolean leaveOpen)
                          at System.IO.StreamWriter..ctor(Stream stream)
                           --- End of inner exception stack trace ---
                          at System.Management.Automation.DotNetAdapter.AuxiliaryCon
                        structorInvoke(MethodInformation methodInformation, Object[]
                         arguments, Object[] originalArguments)
                          at Microsoft.PowerShell.Commands.NewObjectCommand.CallCons
                        tructor(Type type, ConstructorInfo[] constructors, Object[] 
                        args)
TargetObject          : 
CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationExceptio
                        n
FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Comman
                        ds.NewObjectCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, C:\powershell_scripts\tmp\nhttp.ps1: line 18
PipelineIterationInfo : {}

服务器响应清理白页和StatusCode 200到客户端。

但C#的例子非常完美:

using (var server = new HttpServer())
{
    server.RequestReceived += (s, e) =>
    {
        using (var writer = new StreamWriter(e.Response.OutputStream))
        {
            writer.Write("Hello world!");
        }
    };

    server.Start();

    Process.Start(String.Format("http://{0}/", server.EndPoint));

    Console.WriteLine("Press any key to continue...");
    Console.ReadKey();
}

当调用由Register-ObjectEvent创建的事件时,Action块中的这种行为发生在我身上并不是第一次。我试着解决它,改变&#34;公寓&#34;模型(STA和MTA),但它没有解决问题。如果您需要我有更多的例子。

0 个答案:

没有答案