C# 将大型数组传递给WCF服务
我需要将一个大数组传递到WCF服务中 当我尝试这样做时,我得到的是沟通异常:C# 将大型数组传递给WCF服务,c#,wcf,C#,Wcf,我需要将一个大数组传递到WCF服务中 当我尝试这样做时,我得到的是沟通异常: The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout
The socket connection was aborted. This could be caused by an error processing
your message or a receive timeout being exceeded by the remote host, or an
underlying network resource issue. Local socket timeout was '24.00:59:59.9649980'.
我该如何解决这个问题
守则:
服务器:
[ServiceContract(CallbackContract = typeof(ICallback))]
public interface IService
{
[OperationContract]
string Ping(string name);
[OperationContract]
int Count(string[] data);
}
public interface ICallback
{
[OperationContract]
void OnPing(string pingText, DateTime time);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class ServiceImplementation : IService
{
#region IService Members
public string Ping(string name)
{
Semaphore wait = new Semaphore(0,1);
//get the callback item
var callback = OperationContext.Current.GetCallbackChannel<ICallback>();
if (callback!=null)
{
new Thread(() =>
{
wait.WaitOne();
callback.OnPing(name, DateTime.Now);
}).Start();
}
Console.WriteLine("SERVER - Processing Ping('{0}')", name);
wait.Release();
return "Hello, " + name;
}
#endregion
public int Count(string[] data)
{
return data.Count();
}
}
private static System.Threading.AutoResetEvent stopFlag = new System.Threading.AutoResetEvent(false);
public static void Main()
{
ServiceHost svh = new ServiceHost(typeof(ServiceImplementation));
svh.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "net.tcp://localhost:8000");
// Check to see if the service host already has a ServiceMetadataBehavior
ServiceMetadataBehavior smb = svh.Description.Behaviors.Find<ServiceMetadataBehavior>();
// If not, add one
if (smb == null)
smb = new ServiceMetadataBehavior();
//smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
svh.Description.Behaviors.Add(smb);
// Add MEX endpoint
svh.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexTcpBinding(),
"net.tcp://localhost:8000/mex"
);
svh.Open();
Console.WriteLine("SERVER - Running...");
stopFlag.WaitOne();
Console.WriteLine("SERVER - Shutting down...");
svh.Close();
Console.WriteLine("SERVER - Shut down!");
}
public static void Stop()
{
stopFlag.Set();
}
client:
class Callback : IServiceCallback
{
static int s;
int id = 0;
public Callback()
{
id = s++;
}
public void OnPing(string pingText, DateTime time)
{
Console.WriteLine("\r\n{2}:Callback on \"{0}\"\t{1}",pingText,time,id);
}
}
static Random rnd = new Random();
static string GenetateStuff(int length)
{
StringBuilder sb = new StringBuilder();
string s = "',.pyfgcrlaoeuidhtns;qjkxbmwvsz";
for (int i = 0; i < length; i++)
{
sb.Append(s[rnd.Next(s.Length)]);
}
return sb.ToString();
}
static void Main(string[] args)
{
InstanceContext ctx = new InstanceContext(new Callback());
ServiceClient client = new ServiceClient(ctx);
List<string> data = new List<string>();
for (int i = 0; i < 10000; i++)
{
data.Add(GenetateStuff(10000));
}
Console.WriteLine(client.Count(data.ToArray()));
}
client xml:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IService" closeTimeout="24:01:00"
openTimeout="24:01:00" receiveTimeout="24:10:00" sendTimeout="24:01:00"
transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10"
maxReceivedMessageSize="65536">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8000/" binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IService" contract="TestTcpService.IService"
name="NetTcpBinding_IService">
<identity>
<userPrincipalName value="badasscomputing\menkaur" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
[ServiceContract(CallbackContract=typeof(ICallback))]
公共接口设备
{
[经营合同]
字符串Ping(字符串名称);
[经营合同]
整数计数(字符串[]数据);
}
公共接口ICallback
{
[经营合同]
void OnPing(字符串pingText,日期时间);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
类服务实现:IService
{
#区域副成员
公共字符串Ping(字符串名称)
{
信号量等待=新信号量(0,1);
//获取回调项
var callback=OperationContext.Current.GetCallbackChannel();
if(回调!=null)
{
新线程(()=>
{
等等,等等;
OnPing(name,DateTime.Now);
}).Start();
}
WriteLine(“服务器处理Ping({0}')”,名称);
等等,释放();
返回“你好,”+姓名;
}
#端区
公共整数计数(字符串[]数据)
{
返回data.Count();
}
}
私有静态System.Threading.AutoResetEvent stopFlag=新系统.Threading.AutoResetEvent(false);
公共静态void Main()
{
ServiceHost svh=新的ServiceHost(类型(ServiceImplementation));
AddServiceEndpoint(typeof(IService),新的NetTcpBinding(),“net。tcp://localhost:8000");
//检查服务主机是否已具有ServiceMetadataBehavior
ServiceMetadataBehavior smb=svh.Description.Behaviors.Find();
//如果没有,请添加一个
如果(smb==null)
smb=新的ServiceMetadataBehavior();
//smb.HttpGetEnabled=true;
smb.MetadataExporter.PolicyVersion=PolicyVersion.Policy15;
svh.Description.Behaviors.Add(smb);
//添加MEX端点
svh.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexTcpBinding(),
“净。tcp://localhost:8000/mex"
);
svh.Open();
WriteLine(“服务器正在运行…”);
WaitOne();
Console.WriteLine(“服务器-正在关闭…”);
svh.Close();
WriteLine(“服务器-关闭!”);
}
公共静态无效停止()
{
Set();
}
客户:
类回调:IServiceCallback
{
静态int-s;
int id=0;
公共回调()
{
id=s++;
}
公共void OnPing(字符串pingText,日期时间)
{
WriteLine(“\r\n{2}:回调\“{0}\”\t{1}”,pingText,time,id);
}
}
静态随机rnd=新随机();
静态字符串GenetateStuff(整型长度)
{
StringBuilder sb=新的StringBuilder();
字符串s=“”,.pyfgcrlaoeuidhtns;qjkxbmwvsz”;
for(int i=0;i
可以下载完整的项目。替换以下行
svh.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "net.tcp://localhost:8000");
与
注意:引用System.Runtime.Serialization.dll并导入命名空间
这将增加大小的所有默认限制,应该可以。同样,找到通过代码添加maxObjInItemGraph的方法。替换以下行
svh.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "net.tcp://localhost:8000");
与
注意:引用System.Runtime.Serialization.dll并导入命名空间
这将增加大小的所有默认限制,应该可以。类似地,找到通过代码添加MaxObjectsItemGraph的方法。有关
maxObjectsInGraph
和最大接收消息大小的信息,请参见以下内容:嘿!谢谢你的回复!我不知道如何将此应用于上面的托管代码。。。。。看看什么是确切的例外。我同意原因可能是MaxItemsInObjectGraph
。有关maxObjectsInGraph
和最大接收消息大小的信息,请参阅以下内容:嘿!谢谢你的回复!我不知道如何将此应用于上面的托管代码。。。。。看看什么是确切的例外。我同意,MaxItemsInObjectGraph
可能是原因。。