gpt4 book ai didi

WCF-大数据

转载 作者:行者123 更新时间:2023-12-02 10:53:25 24 4
gpt4 key购买 nike

我有一个带有 basicHTTPBinding 的 WCF Web 服务,服务器端的传输模式是 StreamedRequest,因为客户端将以内存流的形式向我们发送大文件。

但是在客户端,当我使用传输模式作为streamedRequest时,它给了我一个这个错误“远程服务器返回错误:(400) 错误请求”当我查看跟踪信息时,我将其视为错误消息异常:从网络接收的 XML 存在问题。有关更多详细信息,请参阅内部异常。

InnerException:无法读取消息正文,因为它是空的。

我能够使用 trasfermode 作为缓冲发送最多 5MB 的数据,但从长远来看,如果有许多客户端尝试以缓冲传输模式访问服务,这会影响我的 Web 服务的性能。

SmartConnect.Service1Client Serv = new SmartConnectClient.SmartConnect.Service1Client();
SmartConnect.OrderCertMailResponse OrderCert = new SmartConnectClient.SmartConnect.OrderCertMailResponse();
OrderCert.UserID = "abcd";
OrderCert.Password = "7a80f6623";
OrderCert.SoftwareKey = "90af1";
string applicationDirectory = @"\\inid\utty\Bran";
byte[] CertMail = File.ReadAllBytes(applicationDirectory + @"\5mb_test.zip");
MemoryStream str = new MemoryStream(CertMail);

//OrderCert.Color = true;
//OrderCert.Duplex = false;
//OrderCert.FirstClass = true;
//OrderCert.File = str;
//OrderCert.ReturnAddress1 = "Test123";
//OrderCert.ReturnAddress2 = "Test123";
//OrderCert.ReturnAddress3 = "Test123";
//OrderCert.ReturnAddress4 = "Test123";
OrderCert.File = str;

//string OrderNumber = "";
//string Password = OrderCert.Password;
//int ReturnCode = 0;
//string ReturnMessage = "";
//string SoftwareKey = OrderCert.SoftwareKey;
//string UserID = OrderCert.UserID;
//OrderCert.File = str;

MemoryStream FileStr = str;
Serv.OrderCertMail(OrderCert);

// Serv.OrderCertMail(ref OrderNumber, ref Password, ref ReturnCode, ref ReturnMessage, ref SoftwareKey, ref UserID, ref FileStr );
lblON.Text = OrderCert.OrderNumber;

Serv.Close();


// My Web Service - Service Contract
[OperationContract]
OrderCertMailResponse OrderCertMail(OrderCertMailResponse OrderCertMail);

[MessageContract]
public class OrderCertMailResponse
{
string userID = "";
string password = "";
string softwareID = "";
MemoryStream file = null;
//MemoryStream str = null;

[MessageHeader]
//[DataMember]
public string UserID
{
get { return userID; }
set { userID = value; }
}

[MessageHeader]
//[DataMember]
public string Password
{
get { return password; }
set { password = value; }
}

[MessageHeader]
//[DataMember]
public string SoftwareKey
{
get { return softwareID; }
set { softwareID = value; }
}

[MessageBodyMember]
// [DataMember]
public MemoryStream File
{
get { return file; }
set { file = value; }
}

[MessageHeader]
//[DataMember]
public string ReturnMessage;

[MessageHeader]
//[DataMember]
public int ReturnCode;

[MessageHeader]
public string OrderNumber;
}


[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class Service1 : IService1
{
public OrderCertMailResponse OrderCertMail(OrderCertMailResponse OrderCertMail)
{
OrderService CertOrder = new OrderService();
ClientUserInfo Info = new ClientUserInfo();
ControlFileInfo Control = new ControlFileInfo();

//Info.Password = "f2496623"; // hard coded password for development testing purposes
//Info.SoftwareKey = "6dbb71"; // hard coded software key this is a developement software key
//Info.UserName = "sdfs"; // hard coded UserID - for testing
Info.UserName = OrderCertMail.UserID.ToString();
Info.Password = OrderCertMail.Password.ToString();
Info.SoftwareKey = OrderCertMail.SoftwareKey.ToString();

//Control.ReturnAddress1 = OrderCertMail.ReturnAddress1;
//Control.ReturnAddress2 = OrderCertMail.ReturnAddress2;
//Control.ReturnAddress3 = OrderCertMail.ReturnAddress3;
//Control.ReturnAddress4 = OrderCertMail.ReturnAddress4;
//Control.CertMailFirstClass = OrderCertMail.FirstClass;
//Control.CertMailColor = OrderCertMail.Color;
//Control.CertMailDuplex = OrderCertMail.Duplex;

//byte[] CertFile = new byte[0];
//byte[] CertFile = null;

//string applicationDirectory = @"\\inid\utility\Bryan";
// byte[] CertMailFile = File.ReadAllBytes(applicationDirectory + @"\3mb_test.zip");
//MemoryStream str = new MemoryStream(CertMailFile);
OrderCertMailResponseClass OrderCertResponse = CertOrder.OrderCertmail(Info,Control,OrderCertMail.File);
OrderCertMail.ReturnMessage = OrderCertResponse.ReturnMessage.ToString();
OrderCertMail.ReturnCode = Convert.ToInt32(OrderCertResponse.ReturnCode.ToString());
OrderCertMail.OrderNumber = OrderCertResponse.OrderNumber;

return OrderCertMail;
}
<小时/>

下面是我的新操作合约,它仅以数据流作为参数

[OperationContract]
SmartStream SendStream(MemoryStream DataStream);

public SmartStream SendStream(MemoryStream DataStream)
{
OrderService CertOrder = new OrderService();
ClientUserInfo Info = new ClientUserInfo();
ControlFileInfo Control = new ControlFileInfo();
MemoryStream serverStream = null;

Info.Password = "78f24dsfsdf96623";
Info.SoftwareKey = "dfs6dbb71";
Info.UserName = "ssfsdf";

using (serverStream = new MemoryStream(100))
{
int count = 0;
const int buffLen = 4096;
byte[] buf = new byte[buffLen];

while ((count = CertFile.Read(buf, 0, buffLen)) > 0)
{
serverStream.Write(buf, 0, count);
}

CertFile.Close();
serverStream.Close();
return null;
}

我的客户端访问方法

protected void Page_Load(object sender, EventArgs e)
{
SmartConnect.Service1Client Serv = new
SmartConnectClient.SmartConnect.Service1Client();
string applicationDirectory = @"\\intrepid\utility\Bryan";
byte[] CertMail = File.ReadAllBytes(applicationDirectory + @"\100mb_test.zip");
MemoryStream str = new MemoryStream(CertMail);

SmartConnectClient.SmartConnect.SendStreamRequest request =
new SmartConnectClient.SmartConnect.SendStreamRequest();
SmartConnectClient.SmartConnect.SmartStream SmartStr =
new SmartConnectClient.SmartConnect.SmartStream();
request.DataStream = str;

SmartConnectClient.SmartConnect.SendStreamResponse response = Serv.SendStream(request);
}

最佳答案

Pinu,我仍然认为您可能没有正确执行 WCF 流的服务契约(Contract)。请参阅MSDN docs on WCF Streaming - 特别是有关限制的部分:

Restrictions on Streamed Transfers

Using the streamed transfer mode causes the run time to enforce additional restrictions.

Operations that occur across a streamed transport can have a contract with at most one input or output parameter. That parameter corresponds to the entire body of the message and must be a Message, a derived type of Stream, or an IXmlSerializable implementation. Having a return value for an operation is equivalent to having an output parameter.

从您发布的代码来看,我仍然认为您正在尝试混合缓冲和流式传输。

如果您想拥有真正的流媒体,您的服务契约(Contract)必须类似于:

[ServiceContract(Namespace=".....")]
interface IUploadFileService
{
[OperationContract]
UploadResponse UploadFile(Stream file);
}

这就是您在该服务方法的服务契约(Contract)中所能拥有的全部内容。

另请参阅一些关于流媒体的非常好的博客文章 herehere .

关于WCF-大数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2462595/

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