|
Can anyone tell me what function is used in c# to canonicalizes a URL, which includes converting unsafe characters and spaces into escape sequences?
In Visual c++ exists this function
BOOL InternetCanonicalizeUrl(
LPCTSTR lpszUrl,
LPTSTR lpszBuffer,
LPDWORD lpdwBufferLength,
DWORD dwFlags
);
but i didn't find the correspondent in c#
thank you
|
|
|
|
|
System.Web.HttpServer.UrlEncode() might be what you need.
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Uri url = new System.Uri(myString);
--
-Blake (com/bcdev/blake)
|
|
|
|
|
I have logging code that I want to set a global variable for the whole application, something like bool logging = true;
I don't want to have to go to all my classes and set this variable. What is the best way to handle this? Can I set it in the web.config file? If so, how?
Thanks!
|
|
|
|
|
You could use compiler directives:
#if DEBUG
#endif
This has the benefit of being compiled out when you go to release code.
α.γεεκ Fortune passes everywhere. Duke Leto Atreides
|
|
|
|
|
Take a look at the Trace and TraceSwitch classes.
Brian
|
|
|
|
|
One approach is to create a class that handles the logging and keeps track of whether it's enabled or not, like this:
class Log
{
private static bool m_enabled = System.Configuration.ConfigurationSettings.AppSettings["Logging"] == "true";
public static bool Enabled
{
get { return m_enabled; }
}
public static void Error(string message)
{
if (Enabled)
}
public static void Info(string message)
....
}
Then you can call Log.Error, Log.Info, etc. in your code. For more details on how to use web.config, check out MSDN[^].
Regards,
Alvaro
Hey! It compiles! Ship it.
|
|
|
|
|
Thanks Alvaro. That is exactly what I was looking for.
I will keep the other suggestions in mind for other things, however.
Cheers.
|
|
|
|
|
Hi guys,
I am trying to create TCP Listener windows services using C# to listen to a local TCP port continuously.
I have written a windows console application which does the job properly but when I convert it to windows services it did not work, as you know definitely I can not use infinity loop on the OnStart method but I want the services to keep listen to synchronous connection by a client and send response when data received.
The senior as following:
I want to listen to a port which I will receive a string 200 characters from a client then send response as acknowledgement.
Your response is highly appreciated.
Regards,
|
|
|
|
|
You will need to create a thread to listen on, its a simple task,
Create a function in your service class which has the listneing socket and the loop in it, eg:
private void ListeningThread(void)<br />
{<br />
TcpListener oListener = new TcpListener();<br />
<br />
_mnThreadCount ++;<br />
<br />
oListener.Listen(nWhateverPortYouWant)<br />
while(!_mbCloseThreads)<br />
{<br />
}<br />
<br />
_mnThreadCount -- ;<br />
}
In OnStart, create your thread
public override OnStart()<br />
{<br />
_mbCloseThreads = false;<br />
_mnThreadCount = 0;<br />
<br />
ThreadStart oThreadStart = new ThreadStart((void)this.ListeningThread);<br />
Thread oThread = new Thread(oThreadStart);<br />
<br />
oThread.Start();<br />
}
and in your OnStop you need to signal the thread to close (_mbCloseThreads) and wait for the threads to shut down (_mnThreadCount)
public override OnStop()<br />
{<br />
_mbCloseThreads = true;<br />
while(_mnThreadCount > 0)<br />
Thread.Sleep(1);<br />
}
Things to remember:
If the thread crashed, always decrement the threadcount, put it in a finally block and that should be ok.
Use Thread.Sleep(1); at the beginning of each continuous loop so it fress up CPU time else your CPU will run at 100%
The code above might not be 100% correct as its strtaight out of my head, but I am sure you will get the general idea of it.
Thanks - James
James Simpson
Web Developer
imebgo@hotmail.com
|
|
|
|
|
Dear James,
Thanks for your replay, I was doing the same way what you have descripted but to make thing exact what you have mentioned please see below code:
the problem is when I start the services and send first stream data it work for the first request and it send proper response but for the second request it did not work I need to stop & start the services then it work for the first request what do you thing the problem???
public void ListeningThread()
{
//Get application Setting information from app.config
strIpAddress = ConfigurationSettings.AppSettings["IPAddress"];
intPort = Int32.Parse(ConfigurationSettings.AppSettings["Port"]);
strConnectionString = ConfigurationSettings.AppSettings["ConnectionString"];
IPAddress IpAddress = IPAddress.Parse(strIpAddress);
TcpListener ilListener = new TcpListener(IpAddress,intPort);
//Start listening
ilListener.Start();
_mnThreadCount ++;
LogError("ilListener.Start();");
while ( !_mbCloseThreads)
{
try
{
TcpClient ilSocket = ilListener.AcceptTcpClient();
NetworkStream networkStream = ilSocket.GetStream();
if(networkStream.CanRead)
{
// Reads NetworkStream into a byte buffer.
byte[] bytes = new byte[165];
networkStream.Read(bytes, 0, 165);
// Returns the data received from the host to the console.
strRecievedData = Encoding.ASCII.GetString(bytes);
}
else
{
//error
}
if ( (int) ilSocket.ReceiveBufferSize >=165)
{
LogError("data size read" + ilSocket.ReceiveBufferSize + strRecievedData );
EnCodeData(strRecievedData);//here I am doing some changeson srtRecievedData
strSendResponse = strResponse;
if(networkStream.CanWrite)
{
Byte[] sendBytes = Encoding.ASCII.GetBytes(strResponse);
networkStream.Write(sendBytes, 0, sendBytes.Length);
}
else
{
// error here
}
LogError("data Sent:" + strSendResponse);
}
}
catch (Exception ex)
{
//
}
}
_mnThreadCount -- ;
ilListener.Stop();
}
protected override void OnStart(string[] args)
{
_mbCloseThreads = false;
_mnThreadCount = 0;
ThreadStart oThreadStart = new ThreadStart(this.ListeningThread);
Thread oThread = new Thread(oThreadStart);
oThread.Start();
LogError("Service Started");
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
_mbCloseThreads = true;
while(_mnThreadCount > 0)
Thread.Sleep(1);
LogError("Service Stopped");
}
I appreciate you immediate response and Thanks in advance.
Regards,
Baqer
|
|
|
|
|
Ok, my immediate advice would be to do the following:
1) Put the client connection into a new thread - for this you will need a new class with a member for the TcpClient object and a function for the thread. Reasonm being i think the Read function blocks the thread until it gets some data so your accept wont be getting called - unless the Accept also blocks, im not sure.
2) The ocrrect way to accept the connection you need to first see if a connection is pending, there is a Pending property/function on the listener, if this true then accept the connection, create a instance of the client thread class mentioned above - set a member variable of the class to the connection and start a new thread using the thread function you need to create in the new class. This leaves the server thread still available to accept more incoming connections. You need to keep track of the thread count and the _mbCloseThread information now, you could create a static instance of the data in another class and access it like that or pass in class with the thread counts and just make them public.
3) When receiving data from the client connection, you may want to read how much data is available and read the data accordingly, or you could use a BinaryFormatter and pass around an 'Envelope' (dont know if its the right terminiology but it fits!) object which is a wrapper class you create to store the data you want to send, eg:
Public Class MyEnvelope
{
string Data
}
Set the Data to what you want to send then use the BinarryFormatter to serialize it into the NetworkStream of the connection, and on the receive end, you can Deserialize from the stream.
Sorry if this doesnt make all much sense! I have just woken up!
If you are still having trouble I will email you a simple client/server test service in C# I havent got one to hand at the moment, but I could put one together later.
Cheers, James
James Simpson
Web Developer
imebgo@hotmail.com
|
|
|
|
|
Thanks James,
I will try it then tell you about the result.
cheers,
Baqer
|
|
|
|
|
Hi James,
I am still having the same problem. The issue that the client software who sends the information is not written by me it is software provided by customer. Which send a string & receive a string as acknowledgement.
now the question is does it mater if you send the data binary or string coz as you know the write or send method of the socket is accepting byte anyway I have used as well the Networkstream.write which send as string.
James if you have any code can help or if you have any other way to be done please suggest.
I need this service to work.
Best Regards,
Baqer
|
|
|
|
|
I can send you a basic server service which will echo a response to a telnet client?
I will need your email address tho. If I get that address tonight I will email you the service tonight.
J
James Simpson
Web Developer
imebgo@hotmail.com
|
|
|
|
|
hi,
I need to disable the scrollbars of the MDI parent form. For this I set
the AutoScroll, HScroll and VScroll properties of the MDIForm to false.
But this doesn't work.
In VB this works but in C# it doesn't.
Any clues?
Thanks,
peenu.
|
|
|
|
|
I need to do the same thing...did you ever figure it out?
Gary Kirkham
A working Program is one that has only unobserved bugs
I thought I wanted a career, turns out I just wanted paychecks
|
|
|
|
|
I want to load and unload a dll-file as an assembly dynamically.
how can I realize it? I only know that I have to use AppDomain, but I don't know how. So can anybody help me?
The content of the dll is a windows form. I used the following code before, but it isn't able to unload:
<br />
Assembly SimpAsm = Assembly.LoadFrom(strFile);<br />
frmTemp = (Form)SimpAsm.CreateInstance(strForm );<br />
frmTemp.Show();<br />
try to help me, please! thanks....
|
|
|
|
|
|
yes there's written many information, but not the real code, which I need.
has anybody an example of using AppDomain?
|
|
|
|
|
Anyone tried it ? is it worth the extra money going from proffessional ?
Space reserved for wit , humour and correct spelling.
|
|
|
|
|
I made my custom control.When I use it ,very SELDOM my controls are missing form my form.I can not understand the reason.
|
|
|
|
|
Hi!
I have a very odd problem. In the code segment below (from my webapplication) the bool protected variable bNyPost is set to true in Page_Load but when I click Save the variable is false in the procedure SaveUhItem(). Isn't bNyPost global to all the procedures in the class FelRegMobileForm?
namespace DeDUWebMobile
{
public class FelRegMobileForm : System.Web.UI.MobileControls.MobilePage
{
protected bool bNyPost;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
.
.
.
bNyPost = true;
}
}
.
.
private void CDSave_Click(object sender, System.EventArgs e)
{
SaveUhItem();
bNyPost = false;
}
.
.
private void SaveUhItem()
{
if( bNyPost ) //Is false even though I assigned true in Page_Load
{
.
.
}
}
}
}
MSc Bertil Morefält
|
|
|
|
|
Let me try to explain. Each time a client makes a request to the server a new Page object is instantiated and the Load event is executed. If it's the clients first request for the page the IsPostBack property should be false, but if a control is manipulated (ie. a button is clicked) or if there is some other event that causes the client to post back to the server then the IsPostBack property will be true.
So in your situation the bNyPost variable is getting set to true because it is inside the conditional statement.
Brian
|
|
|
|
|
That's right, but when bNyPost is set to true the first time when IsPostBack is false, it should stay true when tested in SaveUhItem() when I click Save-button as the next action, but it don't.
Bertil
MSc Bertil Morefält
|
|
|
|