Asp.net mvc asp.net mvc应用程序中的TCPClient
我有一个显示varios事件的asp.net mvc应用程序。存储在数据库中的所有事件。但是现在,我必须从数据库和远程程序加载数据。这个程序有外部服务(这是一个简单的程序,它监听特定的Asp.net mvc asp.net mvc应用程序中的TCPClient,asp.net-mvc,Asp.net Mvc,我有一个显示varios事件的asp.net mvc应用程序。存储在数据库中的所有事件。但是现在,我必须从数据库和远程程序加载数据。这个程序有外部服务(这是一个简单的程序,它监听特定的TCP端口,接收查询并将xml发送回) 并且,我为测试编写了一个简单的页面,该页面连接到外部程序。从MSDN获取的代码: static string Connect(String server, String message) { try { // Create a TcpClient.
TCP
端口,接收查询并将xml发送回)
并且,我为测试编写了一个简单的页面,该页面连接到外部程序。从MSDN获取的代码:
static string Connect(String server, String message)
{
try
{
// Create a TcpClient.
Int32 port = 9197;
TcpClient client = new TcpClient(server, port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
stream.Close();
client.Close();
return responseData;
}
catch (ArgumentNullException e)
{
//
}
catch (SocketException e)
{
//
}
}
这是我的行动:
public ActionResult GetData()
{
string query = "some query";
var response = Connect("192.168.0.1", query);
var model = ParseResponse(response);
return View(model);
}
我认为这个解决方案会降低性能
在ASP.NET MVC 3应用程序中使用TCPClient的最佳实践是什么?你觉得我的代码怎么样? 欢迎提出任何建议 我认为这个解决方案会降低性能 与服务器执行的任何其他远程请求一样,这是一个I/O密集型操作。因此,您可以使用和异步版本的TcpClient方法。这样,在执行远程请求期间,您就不会危及服务器上的任何工作线程 我认为这个解决方案会降低性能 嗯。所有/大多数数据库操作都是通过套接字完成的。你没有注意到,是吗 最可能的性能问题是: