gpt4 book ai didi

c# - 流式 WCF 服务失败

转载 作者:行者123 更新时间:2023-11-30 15:49:31 25 4
gpt4 key购买 nike

尝试从服务下载文件时,我的 wcf 客户端出现以下错误:“接收对 http://mypc-pc/xmlLoadService/Service.svc 的 HTTP 响应时发生错误。这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol)。这也可能是由于 HTTP 请求上下文被服务器中止(可能是由于到服务关闭)。查看服务器日志了解更多详细信息。”

正在执行的代码是这样的:

 System.IO.FileStream fs;
try
{
fs = (System.IO.FileStream)client.Download(@"C:\UploadedFiles\Test.txt");
byte[] arr = new byte[fs.Length];
int read;
do
{
read = fs.Read(arr, 0, arr.Length);

} while (read != arr.Length);

Console.WriteLine(ASCIIEncoding.ASCII.GetString(arr));
Console.ReadLine();
}

这是我的 web.config 配置:

<bindings>
<basicHttpBinding>
<binding name="basicHTTP" receiveTimeout="00:10:00"
sendTimeout="00:03:00" closeTimeout="00:10:00"
openTimeout="00:03:00" messageEncoding="Mtom"
maxBufferSize="100000" maxReceivedMessageSize="100000"
transferMode="Buffered">
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="wsHttpBinding" contract="IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
<service behaviorConfiguration="mexBehavior"
name="LoadXMLService.XMLOperations">
<endpoint address=""
binding="basicHttpBinding"
bindingConfiguration="basicHTTP"
behaviorConfiguration=""
contract="LoadXMLService.IxmlLoad" />
</service>
</services>

在服务器上调用“下载”函数时失败。

更新:据我从跟踪日志中了解到,文件未在服务端打开。

这是应该打开文件的代码,在逐步执行时不会抛出任何异常:

  public Stream Download( string path )
{
try
{
using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{

return stream;
}
}
catch (Exception ex)
{
string error = ex.Message;

return null;
}
}

该文件是一个文本文件,其中只有一行。这是跟踪日志:

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>524340</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2009-10-04T15:20:22.7770000Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{cb13d023-8122-4199-896c-78684b2461db}" />
<Execution ProcessName="WebDev.WebServer" ProcessID="8104" ThreadID="8" />
<Channel />
<Computer>TONY-PC</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-GB/library/System.ServiceModel.ServiceOperationExceptionOnReply.aspx</TraceIdentifier>
<Description>Replying to an operation threw a exception.</Description>
<AppDomain>b986c3da-11-128991431145070000</AppDomain>
<Source>System.ServiceModel.Dispatcher.ImmutableDispatchRuntime/48562646</Source>
<Exception>
<ExceptionType>System.ObjectDisposedException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Cannot access a closed file.</Message>
<StackTrace>
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.Xml.XmlMtomWriter.WriteXOPBinaryParts()
at System.Xml.XmlMtomWriter.WriteEndElement()
at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.MtomMessageEncoder.WriteMessage(Message message, Stream stream, String startInfo, String boundary, String startUri, Boolean writeMessageHeaders)
at System.ServiceModel.Channels.MtomMessageEncoder.WriteMessage(Message message, Stream stream, String boundary)
at System.ServiceModel.Channels.HttpOutput.WriteStreamedMessage(TimeSpan timeout)
at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
at System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout)
at System.ServiceModel.Activation.HostedHttpContext.OnReply(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Reply(Message message)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc&amp; rpc)
</StackTrace>
<ExceptionString>System.ObjectDisposedException: Cannot access a closed file.
at System.IO.__Error.FileNotOpen()
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.Xml.XmlMtomWriter.WriteXOPBinaryParts()
at System.Xml.XmlMtomWriter.WriteEndElement()
at System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
at System.ServiceModel.Channels.MtomMessageEncoder.WriteMessage(Message message, Stream stream, String startInfo, String boundary, String startUri, Boolean writeMessageHeaders)
at System.ServiceModel.Channels.MtomMessageEncoder.WriteMessage(Message message, Stream stream, String boundary)
at System.ServiceModel.Channels.HttpOutput.WriteStreamedMessage(TimeSpan timeout)
at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout)
at System.ServiceModel.Channels.HttpRequestContext.OnReply(Message message, TimeSpan timeout)
at System.ServiceModel.Activation.HostedHttpContext.OnReply(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Reply(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.RequestContextBase.Reply(Message message)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc&amp; rpc)</ExceptionString>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
<System.Diagnostics xmlns="http://schemas.microsoft.com/2004/08/System.Diagnostics">
<LogicalOperationStack></LogicalOperationStack>
<Timestamp>921505170062</Timestamp>
<Callstack>
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Environment.get_StackTrace()
at System.Diagnostics.TraceEventCache.get_Callstack()
at System.Diagnostics.XmlWriterTraceListener.WriteFooter(TraceEventCache eventCache)
at System.Diagnostics.XmlWriterTraceListener.TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, Object data)
at System.Diagnostics.TraceSource.TraceData(TraceEventType eventType, Int32 id, Object data)
at System.ServiceModel.Diagnostics.DiagnosticTrace.TraceEvent(TraceEventType type, TraceCode code, String description, TraceRecord trace, Exception exception, Object source)
at System.ServiceModel.Diagnostics.TraceUtility.TraceEvent(TraceEventType severity, TraceCode traceCode, TraceRecord extendedData, Object source, Exception exception)
at System.ServiceModel.Diagnostics.TraceUtility.TraceEvent(TraceEventType severity, TraceCode traceCode, Object source, Exception exception)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Reply(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessageCleanup(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.Dispatch(MessageRpc& rpc, Boolean isOperationContextSet)
at System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)
at System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)
at System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)
at System.ServiceModel.Diagnostics.Utility.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)
at System.ServiceModel.AsyncResult.Complete(Boolean completedSynchronously)
at System.ServiceModel.Channels.InputQueue`1.AsyncQueueReader.Set(Item item)
at System.ServiceModel.Channels.InputQueue`1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.InputQueue`1.EnqueueAndDispatch(T item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.InputQueueChannel`1.EnqueueAndDispatch(TDisposable item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.SingletonChannelAcceptor`3.Enqueue(QueueItemType item, ItemDequeuedCallback dequeuedCallback, Boolean canDispatchOnThisThread)
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke2()
at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.WorkItem.Invoke()
at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ProcessCallbacks()
at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.CompletionCallback(Object state)
at System.ServiceModel.Channels.IOThreadScheduler.CriticalHelper.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.ServiceModel.Diagnostics.Utility.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</Callstack>
</System.Diagnostics>
</ApplicationData>
</E2ETraceEvent>

最佳答案

由于您似乎在使用流式响应 - 您的配置如何?你的客户端配置中有这样的东西吗??

  <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="FileTransferBinding"
transferMode="StreamedResponse" />
</basicHttpBinding>
</bindings>
<client>
<endpoint name="DefaultEndpoint"
address="....."
binding="basicHttpBinding"
bindingConfiguration="FileTransferBinding"
contract="IYourContract" />
</client>
</system.serviceModel>
  • 一个 BindingConfiguration,它有一个 transferMode=StreamedtransferMode=StreamResponse
  • 一个客户端,它有一个binding=basicHttpBinding 和一个bindingConfiguration=FileTransferBinding(其中绑定(bind)配置的名称与以上在绑定(bind)配置的声明中)

这是您应该拥有的绝对最低限度 - 但如果没有提供这些关键信息,这只是猜测......

更新:您的问题最可能是您的绑定(bind)配置中没有 transferMode="StreamResponse"。仅当您有此设置时,您才能读取 Stream 作为服务调用的响应!

更新 2:如果您仍然遇到问题,我强烈建议您配置并打开 WCF 跟踪 - 将此片段添加到您的客户端配置中:

<configuration>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="traceListener"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>

它会将跟踪信息写入文件 C:\log\traces.svclog(确保该目录存在),您可以使用服务跟踪查看器 (SvcTraceViewer. exe 在你的 C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\)

查看 MSDN docs有关如何配置和设置 WCF 跟踪的详细信息。

更新 3: 因为您想真正返回流(而不是在服务器上关闭它),所以您应该将您的流代码放入 using() {...} block !

试试这个:

public Stream Download( string path )
{
try
{
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
return stream;
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
}

您需要在服务器上打开流,并保持打开状态并返回它。客户的工作是确保在客户阅读完后将其关闭。

将该代码放在 using() {....} block 中通常是最佳实践,但在这种“服务器打开流并将其返回给客户端”的情况下不起作用。

马克

关于c# - 流式 WCF 服务失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1516503/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com