C# 类型';System.Net.Sockets.Socket';无法序列化

C# 类型';System.Net.Sockets.Socket';无法序列化,c#,asp.net,wcf,sockets,C#,Asp.net,Wcf,Sockets,无法序列化类型“System.Net.Sockets.Socket”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。 我正在WCF中创建一个服务,该服务打开连接并向服务器发送消息,但在运行该服务时出现了上述错误。我不知道如何用数据契约属性标记它,也不知道如何解决这个问题 public class Service1 : IService1 { public void Connect(String a , Str

无法序列化类型“System.Net.Sockets.Socket”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。

我正在WCF中创建一个服务,该服务打开连接并向服务器发送消息,但在运行该服务时出现了上述错误。我不知道如何用数据契约属性标记它,也不知道如何解决这个问题

public class Service1 : IService1
{


    public void Connect(String a , String b)
    {

        int hit = Convert.ToInt32(a);

        int delay = Convert.ToInt32(b);
        delay = delay * 1000; 

 // I have eliminated the log making part       string LogPath = "C:\\VoltTestApp\\Logs\\";


        for (int i = 1; i <= hit; i++)
        {

            try
            {
                TcpClient tcpClient = new TcpClient("10.111.13.72", 80);
                Console.WriteLine("Initialized Socket  .............\n");
                Socket socket = tcpClient.Client;
                string str = "ID_T$";

                try
                { // sends the text with timeout 10s
                    Console.WriteLine("Going To Send Request  .............\n");
                    Send(socket, Encoding.UTF8.GetBytes(str.Trim()), 0, str.Length, 10000);
               }


                socket.Close();
                tcpClient.Close();
                Console.WriteLine("Socket Closed  .............\n");
            }

            Thread.Sleep(delay);
        }

    }



    public  void Send(Socket socket, byte[] buffer, int offset, int size, int timeout)
    {
        try
        {
            int startTickCount = Environment.TickCount;
            int sent = 0;  // how many bytes is already sent
            do
            {
                if (Environment.TickCount > startTickCount + timeout)
                    throw new Exception("Timeout.");
                try
                {
                    sent += socket.Send(buffer, offset + sent, size - sent, SocketFlags.None);
                }
                catch (SocketException ex)
                {
                    if (ex.SocketErrorCode == SocketError.WouldBlock ||
                        ex.SocketErrorCode == SocketError.IOPending ||
                        ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
                    {
                        // socket buffer is probably full, wait and try again
                        Thread.Sleep(30);
                    }
                    else
                        throw ex;  // any serious error occurr
                }
            } while (sent < size);
        }

    }
公共类服务1:IService1
{
公共无效连接(字符串a、字符串b)
{
int hit=转换为32(a);
int delay=转换为32(b);
延迟=延迟*1000;
//我已经消除了日志生成部分字符串LogPath=“C:\\voltestapp\\Logs\\”;
对于(int i=1;i开始计时+超时)
抛出新异常(“超时”);
尝试
{
sent+=socket.Send(缓冲区、偏移量+发送、大小-发送、SocketFlags.None);
}
捕获(SocketException例外)
{
if(ex.SocketErrorCode==SocketError.WouldBlock||
ex.SocketErrorCode==SocketError.IOPending||
ex.SocketErrorCode==SocketError.NoBufferSpaceAvailable)
{
//套接字缓冲区可能已满,请稍候,然后重试
睡眠(30);
}
其他的
throw ex;//发生任何严重错误
}
}while(发送<大小);
}
}

错误消息具有误导性,解决方法是不要将
DataContractAttribute
添加到
System.Net.Sockets.Socket
。问题是您无法通过网络发送套接字。更具体地说,您不能序列化它,要发送它,您必须能够序列化它


您需要找到一个不涉及通过网络发送套接字(或将其保存到磁盘/数据库)的解决方案。

您无法明智地在调用wcf服务之间保持tcp连接。在你的情况下 我将从服务契约中删除Connect方法,在Service1类中将其更改为private, 从send方法中删除socket参数(在IService1及其在Service1类中的实现中)
并从send方法调用connect方法(以便每次发送时连接和断开连接)

在本例中,可能是因为您的服务上有一个公共方法,该方法将套接字作为参数。很自然,在尝试服务发现时无法正确序列化


如果
Send
仅在内部使用,请将其设置为private。

如果没有相关代码,我们将无法为您提供帮助。但建议不要尝试将实际套接字作为消息发送。请提供一些代码。不要认为序列化Socket是这里要做的正确的事情。所以,如果我去掉send函数,只在connect函数中做这一部分,这样我的方法就不会接受Socket参数,它会工作吗?或者可能的解决方案是什么,因为这是我的第一个wcf应用程序请进一步查看我的答案-如果您不在其他任何地方使用,请将Send设置为private我确实发现,尝试为以后保存一个套接字的前景相当有趣所以你建议我重新设计我的解决方案。我实际上有一个web表单,它将某些数据发送到服务,然后服务将连接到一个非本地的服务器(我目前只关注客户端部分,所以如果我不能使用sockets,你建议我如何将我的服务连接到主机上?但是你的解决方案能否解决不允许套接字序列化的问题?我不能序列化套接字,那么我不能将其与WCF一起使用这是我迄今为止学到的知识。不,我是在假设w你想做什么(因为这并不明显)。你绝对不能序列化套接字-它们代表/包装两个网络节点之间的开放网络连接。在我看来(我假设),你试图使用WCF服务来简化(从客户端应用程序的角度来看)与tcp服务器的通信——也就是说,您希望您的客户端应用程序只处理WCF,而不是处理tcp套接字本身。这不是您正在尝试做的吗?