私はこれがうまくいかない理由を理解するために私の一日のほとんどを費やしました。オブジェクトをクライアントにストリーミングするWCFサービスがあります。次に、クライアントはファイルをディスクに書き込むことになっています。しかし、呼び出すstream.Read(buffer, 0, bufferLength)
と常に0が返されます。コードは次のとおりです。
namespace StreamServiceNS
{
[ServiceContract]
public interface IStreamService
{
[OperationContract]
Stream downloadStreamFile();
}
}
class StreamService : IStreamService
{
public Stream downloadStreamFile()
{
ISSSteamFile sFile = getStreamFile();
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bf.Serialize(stream, sFile);
return stream;
}
}
サービス構成ファイル:
<system.serviceModel>
<services>
<service name="StreamServiceNS.StreamService">
<endpoint address="stream" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IStreamService"
name="BasicHttpEndpoint_IStreamService" contract="SWUpdaterService.ISWUService" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IStreamService" transferMode="StreamedResponse"
maxReceivedMessageSize="209715200"></binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceThrottling maxConcurrentCalls ="100" maxConcurrentSessions="400"/>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
クライアント:
TestApp.StreamServiceRef.StreamServiceClient client = new StreamServiceRef.StreamServiceClient();
try
{
Stream stream = client.downloadStreamFile();
int bufferLength = 8 * 1024;
byte[] buffer = new byte[bufferLength];
FileStream fs = new FileStream(@"C:\test\testFile.exe", FileMode.Create, FileAccess.Write);
int bytesRead;
while ((bytesRead = stream.Read(buffer, 0, bufferLength)) > 0)
{
fs.Write(buffer, 0, bytesRead);
}
stream.Close();
fs.Close();
}
catch (Exception e) { Console.WriteLine("Error: " + e.Message); }
クライアントapp.config:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpEndpoint_IStreamService" maxReceivedMessageSize="209715200" transferMode="StreamedResponse">
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://[server]/StreamServices/streamservice.svc/stream"
binding="basicHttpBinding" bindingConfiguration="BasicHttpEndpoint_IStreamService"
contract="StreamServiceRef.IStreamService" name="BasicHttpEndpoint_IStreamService" />
</client>
</system.serviceModel>
(簡潔にするために一部のコードを切り取った)
WCFストリーミングサービスの作成で見つけたものをすべて読みましたが、私のコードはそれらのコードと何ら変わりはありません。ストリーミングをバッファリングに置き換えて、オブジェクトをそのようにうまく送信することはできますが、ストリーミングしようとすると、クライアントは常にストリームを「空」と見なします。が作成されますtestFile.exe
が、サイズは0KBです。
私は何が欠けていますか?
Hrm
を更新
します。デバッグできません。デバッグしようとすると、WCFテストクライアントがストリームをサポートしていないことがわかります。しかし、whileループを配置せず、fs.Write(...)が1つしかない場合、ストリームが閉じると8KBが書き込まれるため、ストリーム内で何かが発生していることはわかっています。だから何かが伝わってきています。
私はあなたの投稿を見て、次のように追加しました。
[MessageContract]
public class FileDownloadMessage
{
[MessageBodyMember(Order = 1)]
public MemoryStream FileByteStream;
}
私のIStreamServiceに。そしてdownloadStreamFile()
今、オブジェクトを返しFileDownloadMessage
ます。
しかし、クライアントのサービス参照を更新した後、オブジェクトdownloadStreamFile()
を返すと考えています。
それはまた、私が作成しなかったクラスを作成し、それがどこから来ているのかわかりません。TestApp.StreamServiceRef.MemoryStream
downloadStreamFileRequest()