gpt4 book ai didi

WCF:是否可以在双工 channel 中使用流模式?

转载 作者:行者123 更新时间:2023-12-05 00:09:22 24 4
gpt4 key购买 nike

在 WCF 中,合约可以切换到流模式,以传输大消息。

在阅读和测试之后,在我看来,流模式不能用于双工 channel (带有 OneWay 调用和回调接口(interface)的 channel )。

是这样吗?双工和流媒体不能互相使用吗?或者有什么办法吗?

(我正在尝试将一个大文件上传到服务并使用回调报告进度)

最佳答案

要将文件从客户端加载到服务器,可以使用以下代码:服务

 [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();
}
}}

客户:
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>

传递字节数组总是更好。我想,不需要描述回调函数吗?

关于WCF:是否可以在双工 channel 中使用流模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/420569/

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