In WCF, contracts can be switched to streaming mode, to transfer large messages.
After reading and testing it seems to me, that streaming mode can not be used with duplex channels (channels with OneWay-calls and a callback interface).
Is this so? Do duplex and streaming can not be used with each other? Or is there a way?
(I'm trying to upload a large file to the service and use callback to report progress on this)
to load the file from the client to the server, you can use the following code: service
[ServiceContract(SessionMode=SessionMode.Required, CallbackContract=typeof(IFreeBoxServiceCallBack))]
public interface IFreeBoxService
{
[OperationContract(IsOneWay = true)]
void sendFile(byte[] bytes, int offset, int count);
[OperationContract(IsOneWay = true)]
void sendFileName(string fileName);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode=ConcurrencyMode.Multiple)]
public class FreeBoxService:IFreeBoxService
{
string path = "D:\\repository\\";
string fileName = "";
IFreeBoxServiceCallBack callBack = null;
public FreeBoxService()
{
callBack = OperationContext.Current.GetCallbackChannel<IFreeBoxServiceCallBack>();
}
public void sendFileName(string fileName)
{
this.fileName = fileName;
}
public void sendFile(byte[] bytes, int offset, int count)
{
using (FileStream fileStream = new FileStream(path + fileName, FileMode.Append, FileAccess.Write))
{
fileStream.Write(bytes, offset, count);
fileStream.Close();
fileStream.Dispose();
}
}}
client:
private void sendFileToServer()
{
FileStream fs = new FileStream(fullName,FileMode.Open,FileAccess.Read);
int bytesRead = 0;
bytes = new byte[15000];
proxy.sendFileName("someFile.xml");
while ((bytesRead = fs.Read(bytes, 0, 15000))>0)
{
proxy.sendFile(bytes, 0, bytesRead);
}
fs.Close();
fs.Dispose();
}
.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="FreeBoxServiceLib.FreeBoxService" behaviorConfiguration="MyServiceBehevior">
<endpoint address="" contract="FreeBoxServiceLib.IFreeBoxService"
binding="netTcpBinding">
</endpoint>
<endpoint address="MEX" binding="mexTcpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8080/FreeBoxService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehevior">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
is always better to pass an array of bytes. I think, not need describe the callback functions?