I have spend hours to convert an old net v2 project and to port it to WinRT in C#.
Its a RTSP client thats sending commands to a DVBservice (DVBViewer). So, at this moment, the WinForm Program has been renewed and its working prety fine. Connecting, sending and receiving commands to the server and finaly the stream to my localhost UDP port are fully working and the ts stream is fully readable via VLC and RTP protocol.
But now, i'd like to write my Metro app with this stuff. I managed to do the work and seems to be almost finish (at least the Socket and stream stuff).
But now, I'm getting stuck on a stupid problem. I CAN'T communicate with the RTSP Server.
My Stream Reader/Writer is'nt working and I've tried a lot.
The app is based on code from the Uniriotec.DV project, so for any futher info, you can find it by google.
So, here's the point I'm getting stuck:
Its the main handle, thats getting the StreamSocket with the Stream (the messages) together.
//Set input and output stream filters in the main client app
RTSPBufferedReader = new BufferedReader<Stream> (RTSPsocket);
RTSPBufferedWriter = new BufferedWriter<Stream> (RTSPsocket);
namespace RTSP.Client
{
public class BufferedReader<T> : StreamReader where T : Stream
{
private StreamSocket socket;
private T unbufferedStream;
private StreamSocket streamSocket;
public T UnbufferedStream
{
get { return unbufferedStream; }
set { unbufferedStream = value; }
}
public BufferedReader(T myStream)
: base(myStream)
{
unbufferedStream = myStream;
}
public BufferedReader(StreamSocket mySocket)
: base(new Stream(mySocket)) // <== here is the problem, saying "could'nt establish an instance of the abstract or interface class "System.IO.Stream"....
{
this.streamSocket = mySocket;
}
}
}
Do you have an idea where I did the error?
Thanks for answering,
Jo
PS: I need await writer.StoreAsync(); because the answer is send approx 10-15 sec. later, when the server is ready to thread the request and sends me SessionID and so back.
The Stream class is abstract and you cannot instantiate an abstract class directly, like you are doing with the base(new Stream(mySocket)). See here for the System.IO.Stream definition
http://msdn.microsoft.com/en-us/library/windows/apps/system.io.stream.aspx
You need to replace the Stream class with the appropriate derived class, such as DataReader. Here is an example of using a StreamSocket sample that may help:
http://code.msdn.microsoft.com/windowsapps/StreamSocket-Sample-8c573931
Related
I am trying to add a grpc protofile to my swagger-ui. I am consuming a grpc webservice which needs a protofile as input. The input to my spring boot restful webservice needs to have that same grpc structure as its interface. I recevied a jar from the individual that made the protofile and imported it to my webserivce. When I try to add the #ResponseBody tag around the object from the protofile jar, my app hangs on this in the console at startup:
s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
Thanks,
Brian
Never return entity objects in controller method.
in my case. my Controller methods takes this parameter.
"#AuthenticationPrincipal UserSession userSession"
when i exlude UserSession object swagger back to normal.
There were 2 way to do that
first is "#ApiIgnore #AuthenticationPrincipal UserSession userSession"
second is in swaggerConfig class
private Class[] clazz = {UserSession.class};
Docket().ignoredParameterTypes(clazz)
Incase someone needs a solution, what I did was as a work around for now.
in my service's code (response is a String)
return JsonFormat.printer().print(myProtoObject);
in my client's code:
Builder b = ProtoObject.newBuilder();
JsonFormat.parser().merge(result.getBody(), b);
ProtoObject protoObject = b.build();
Below I present a part of an Azure Web App that handles a device notification logic. What I'm doing is invoking the code shown below from a ASP MVC Controller.
But when I run it I get an hung (ever-pending) request from the browser.
I thought I've found a workaround by wrapping the SendAsync call in a BackgroundWorker thread. It's better, but I doesn't work right. For first couple of times (one or two) it works ok, but then it happens again, the wrapped thread hangs.
The code is not far different from the one on MSDN for a console application. What am I missing?
using System.Web.Configuration;
using Microsoft.Azure.Devices;
namespace MyTest
{
public class Sender
{
private readonly string connectionString;
private readonly Microsoft.Azure.Devices.ServiceClient serviceClient;
public Sender()
{
connectionString = WebConfigurationManager.AppSettings["ConnectionString"];
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
}
public async void SendRequest(string deviceId, string msgText)
{
var message = new Message();
message.Properties.Add("text", msgText));
await serviceClient.SendAsync(deviceId, message);
}
}
}
The problem was caused by inappropriate usage of ASP MVC framework.
It turned out that AsyncController has to be used instead of just Controller when a long running async\await is utilized. The pipeline must be async all the way.
I am having a really weird issue with MVC3 and signalr.. I have a simple hub;
[HubName("test")]
public class Test: Hub
{
public object GetStuff()
{
return new { dummy = "Test" };
}
}
And some client-side code;
var connection = $.connection.test;
connection.start();
connection.getStuff();
This throws an error;
TypeError: Object # has no method 'start'
If I instead do
var connection = $.connection("test");
I get a different error;
TypeError: Object # has no method 'getStuff' jquery-1.6.4.min.js:4
POST http://localhost:63021/Controller/test/negotiate 405 (Method Not Allowed)
Note its trying to negotiate under the controller for some reason?
Is there some specific route I need to register? Some other magic I dont know about?
UPDATE
So playing a bit with console -- the first version does in fact create an object that has getStuff() which i can call. But signalr throws up because i have to call start() first -- which doesn't exist! The second one creates an object that DOES have start(), but it doesnt have getStuff()..
UPDATE 2
Tried doing $.connection.hub.start instead. This seems to work in the console, but not in the page onload.. Possibly start isnt finished before the hub call is made? Is it async?
Starting the SignalR connection is not instantaneous. You call to connection.GetStuff(); may fail if the connection has not yet been established. If you want this code to run after a connection to the hub is established you should use a callback function.
var connection = $.connection.test;
$.connection.hub.start(function(){
// By convention all exposed hub methods start with lowercase
connection.getStuff();
});
Hub Quickstart: https://github.com/SignalR/SignalR/wiki/QuickStart-Hubs
In-depth look at SignalR javascript client: https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs
You must add the hub portion:
$.connection.hub.start();
Try this:
var connection = $.connection("#Url.Content("~/echo")");
I have asp.net mvc app that shows varios events. All events stored in a database. But now, I have to load data from the database and remote program. This program have external service (this is simple program that listening specific TCP port and recieve a query and send xml back).
And, I wrote simple page for test that connects to external program. The code got from 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)
{
//
}
}
This is my action:
public ActionResult GetData()
{
string query = "some query";
var response = Connect("192.168.0.1", query);
var model = ParseResponse(response);
return View(model);
}
I think this solution will reduce the perfomance.
What is best practicies to use TCPClient in ASP.NET MVC 3 app?
What you think about my code?
Any suggestions are welcome.
I think this solution will reduce the perfomance.
Well. All/most database operations are done over sockets. And you do not notice that, do you?
The most likely performance issues are:
Your server
Server location
Connection setup
The only thing I would do now is to build in checks in the client to monitor the response time and write to a log (or send an email) when the response times are too high.
Don't try to optimize performance until that happen.
Solutions for the above mentioned issues:
Refactor and optimize
Either put the server on the same lan or create a cache proxy server.
Use connection pooling instead of disconnecting the connections every time.
I think this solution will reduce the perfomance.
It's as any other remote request that your server does - an I/O intensive operation. So you could use an asynchronous controller and the asynchronous versions of the TcpClient methods. This way you won't be jeopardizing any worker threads on your server during the execution of the remote request.
I need to provide a file-download feature where the web server retrieves the file from another source (via HTTP) and simultaneously streams it to the browser. I am guessing that using MVC's Controller.File ActionResult will not work, but I wrote a prototype like this anyway:
public ActionResult Download()
{
HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create("http://somewhere/somefile.pdf");
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
Stream stream = webResponse.GetResponseStream();
var mimeType = "application/pdf";
var fileName = "somefile.pdf";
return File(stream, mimeType, fileName);
}
This works fine, but there is no way to call Close() on the HttpWebResponse and Stream after the return statement. The help on the HttpWebResponse.GetResponseStream method says, "You must call either the Stream.Close or the HttpWebResponse.Close method to close the stream and release the connection for reuse. It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error. Failure to close the stream will cause your application to run out of connections."
Should I create an HttpHandler and manually read bytes from the source stream and write them out to the response, along the lines of this or this? Is there another approach I'm not aware of?
While I'm not directly familiar with trying something like this, my first though was to do what you suggested in regards to reading in the stream, closing the connection, then returning the bytes as the response. Being a stream, I don't know how you can get around leaving it open for the sake of returning its contents as you do in your prototype, but then being able to close it when you're done.