C# WCF未发送流(net.tcp绑定)
我无法使用net.tcp绑定发送流。我有MessageContract来发送数据,WCF正在传递文件名,但流FileByTestStream是空的C# WCF未发送流(net.tcp绑定),c#,wcf,service,stream,net.tcp,C#,Wcf,Service,Stream,Net.tcp,我无法使用net.tcp绑定发送流。我有MessageContract来发送数据,WCF正在传递文件名,但流FileByTestStream是空的 [MessageContract] public class FileUploadMessage { [MessageHeader(MustUnderstand = true)] public string Filename; [MessageBodyMember(Order = 1)] public Stream F
[MessageContract]
public class FileUploadMessage
{
[MessageHeader(MustUnderstand = true)]
public string Filename;
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
}
我的服务:
[ServiceContract]
public interface ISlaveService
{
[OperationContract]
FileUploadMessage SendPattern(FileUploadMessage image);
}
服务实现(发送和返回流都不工作):
启动服务:
public static void StartService()
{
try
{
Uri tcpUrl = new Uri("net.tcp://localhost:" + Config.Instance().LocalPort + "/SlaveService");
host = new ServiceHost(typeof(SlaveService), tcpUrl);
NetTcpBinding tcpbinding = new NetTcpBinding();
tcpbinding.Security.Mode = SecurityMode.None;
tcpbinding.TransferMode = TransferMode.Streamed;
tcpbinding.MaxReceivedMessageSize = 2147483647;
tcpbinding.ReaderQuotas.MaxArrayLength = 2147483647;
tcpbinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
tcpbinding.ReaderQuotas.MaxStringContentLength = 2147483647;
tcpbinding.ReaderQuotas.MaxDepth = 2147483647;
host.AddServiceEndpoint(typeof(ISlaveService), tcpbinding, "net.tcp://localhost:" + Config.Instance().LocalPort + "/SlaveService");
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(),
"net.tcp://localhost:" + Config.Instance().LocalPort + "/SlaveService/mex");
host.Open();
}
catch (Exception ex)
{
//TO DO: Logger.Add("Failed to start Slave service. " + ex.Message);
}
}
客户端连接:
public bool Connect(string address)
{
NetTcpBinding tcpbinding = new NetTcpBinding();
tcpbinding.Security.Mode = SecurityMode.None;
tcpbinding.TransferMode = TransferMode.Streamed;
tcpbinding.MaxReceivedMessageSize = 2147483647;
tcpbinding.ReaderQuotas.MaxArrayLength = 2147483647;
tcpbinding.ReaderQuotas.MaxBytesPerRead = 2147483647;
tcpbinding.ReaderQuotas.MaxStringContentLength = 2147483647;
tcpbinding.ReaderQuotas.MaxDepth = 2147483647;
EndpointAddress endpoint = new EndpointAddress("net.tcp://" + address + "/SlaveService");
var channelFactory = new ChannelFactory<ISlaveService>(tcpbinding, endpoint);
client = null;
try
{
client = channelFactory.CreateChannel();
}
catch (Exception e)
{
Logger.Add("Exception occured while trying to connect. " + e.Message);
if (client != null)
{
((ICommunicationObject)client).Abort();
}
return false;
}
return true;
}
public bool Connect(字符串地址)
{
NetTcpBinding=新的NetTcpBinding();
tcpbinding.Security.Mode=SecurityMode.None;
tcpbinding.TransferMode=传输模式。流式传输;
tcpbinding.MaxReceivedMessageSize=2147483647;
tcpbinding.ReaderQuotas.MaxArrayLength=2147483647;
tcpbinding.ReaderQuotas.MaxBytesPerRead=2147483647;
tcpbinding.ReaderQuotas.MaxStringContentLength=2147483647;
tcpbinding.ReaderQuotas.MaxDepth=2147483647;
EndpointAddress endpoint=新的EndpointAddress(“net.tcp://“+address+”/SlaveService”);
var channelFactory=新的channelFactory(tcpbinding,端点);
client=null;
尝试
{
client=channelFactory.CreateChannel();
}
捕获(例外e)
{
添加(“尝试连接时发生异常。”+e.Message);
如果(客户端!=null)
{
((ICommunicationObject)client.Abort();
}
返回false;
}
返回true;
}
我可以发送文本和数字,但当我试图发送流时,它发送的是一个空流。我在论坛上说MaxReceivedMessageSize应该是一个解决方案,但它并不能解决我的问题。
请帮助合同应为:
[OperationContract]
FileUploadMessage SendPattern(Stream image);
我尝试了这个,在使用流之后添加了FileUploadMessage。当我将文件作为字节数组而不是流发送时,这段代码可以正常工作。我不知道为什么
[OperationContract]
FileUploadMessage SendPattern(Stream image);