|
Hi. I am a new member of Code Project. I have some basic beginner questions, and wonder if this is the place to ask them. I'll ask one, and if there is a better area for beginner questions, please let me know. My current question involves the key word static. Could someone please give me an explanation of it's function? I am reading a Microsoft book on C#, and I am having trouble understanding what static is all about. I looked but do not find a FAQ on this site which answers such questions. Thanks, Steve
|
|
|
|
|
|
The static modifier, keeps the method,property, or field "static in memory". That means it persists and you don't need to instantiate it. You use them all the time when programming in .NET... one very good example is when you use any of the public members of Console such as Write() or Read().
The wole point behind a static keyword is to allow use of the so&so method/field without creating a new instance... How else would you be able to run any of the programs you write? Look at the general structure of a Main method.
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Form1());
}
you can run Main at anytime, and you create a new instance of the Form1 and then the program runs. Also, Application.Run() happens to be a static method as well
Got a coding problem? Hand it to the CodeDevil!
|
|
|
|
|
hi everyone,
I've written a small application for my company and now would like it to integrate with VS.NET 2003. In the integration part, what I specifically want is to have a menu item placed in the context menu of solution explorer such that whenever the user right clicks on any .cs or .vb file, "Open with myApp" comes as a menu item in the context menu.
Can anyone guide me with this? How should I go about doing this? I have seen many applications doing the same, that means this is possible. Some code snippet will be highly appreciated.
thanks in advance...
Cheers !! and have a Funky day !!
|
|
|
|
|
While this won't solve your immediate problem, it's important to understand the design-time features of the .NET Framework. Read Enhancing Design-Time Support[^] in the .NET Framework SDK for more details. Much of what you see in VS.NET when designing with forms and controls is actually provided by classes in the BCL.
For integration like you want, see the Visual Studio Extensibility Center[^]. Also see the Automation and Extensibility Reference[^] in MSDN Librayr, which documents the DTE (Design-Time Environment) object model and gives C# and VB.NET (as well as C++, of course) examples. Whatever you read for VB (as in VB6), the same is pretty much true since the EnvDTE assembly is an interop assembly created from the typelib that automation clients like VB6 use to automate and object library.
Specifically, you'll want to take a look at the VSProjectItem Object[^].
If you look in VS.NET's New Project dialog, you'll also see a couple of extensibility projects that serve as a good start. There may even be a few articles here on Code Project is you search for them.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Can anyone give me independent advice on c# code generators for database tier. I know there are many products out there but its hard to distinguish between them.
I have had a brief look at llblgen Pro, RapTier, CodeSmith, TierDevelopment, IronSpeed.
There are some freeware solutions on "SourceForge.Net" but they seem very limiting.
Thanks in advance
|
|
|
|
|
I've used CodeSmith and MyGeneration.Net, and found both to be very capable. I'm thinking the next thing I will try is NHibernate (an O/R Mapper), which does away with the need for a specific data layer.
There is a long list on http://codegeneration.net/.
my blog
|
|
|
|
|
Thanks Steven for the recommendations, I also wasn’t aware of NHibernate, I will have a look at it. Cheers, Jared
|
|
|
|
|
Is is possible through, and I assume this would be using Platform Invoke, to create a hwnd without creating a Windows.Forms.Form?
Would i have to register a class and create a windows without showing it through windows api or is there some way just to obtain or register a unique hwnd without making a window.
Thank you very much!
|
|
|
|
|
To get an HWND (an IntPtr in .NET) for an existing Window, P/Invoke FindWindow[^].
If you want to create a Window without instantiation a Form *, you can P/Invoke CreateWindow or, preferably, the CreateWindowEx[^] API, which you can read about in the MSDN Library[^].
* Keep in mind that any control or form you create is a window. Each is tied to an HWND (via the Control.Handle property that every control - including the Form class - inherits), just like with native code. Even Java AWT and Swing classes are ultimately tied to an HWND , though through the JVM.
Also, just instantiating a Control derivative doesn't create the window. The control must be set to visible so that the window is actually created and the Control class derivative is tied to the new HWND . This is reflected by the OnHandleCreated event handler and the HandleCreated event.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thats what I though. I just didn't want to use any existing hwnds as i am writing a notify icon tray things and i dont want to tie it in with windows, so that it could be created in a console program for exaple (for whatever reason you would want to do that in the first place really beats me though).
|
|
|
|
|
That's where the NativeWindow class comes in handy, which you can attach to an existing window (including those windows in managed coding using their Control.Handle property).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
The dedicated server doesn't send out emails.
I always got this msg:
The server rejected one or more recipient addresses. The server response was: 553 sorry, that domain isn't in my list of allowed rcpthosts
The email sending out code works very well on local servers, why it doesn't work on dedicated server?
I tried to change smtpserver to "localhost", it didn't work.
Can anybody help me?
Thanks a lot!!
|
|
|
|
|
You need to set up your SMTP server - if running under ASP.NET - to either forward emails for other domains or a whole slew of other problems. This is not the forum for that, however. Your problem is not with C#, but with understanding and setting up an SMTP server.
The SmtpMail class - as I've discussed previously if you would've clicked "Search comments" above this forum to search first before asking - uses the CDO client libraries, or the Virtual SMTP Service or Microsoft Exchange if installed. For ASP.NET web applications, this must be one of the latter implementations of CDO, which is a COM library that the SmtpMail class encapsulates.
You should ask about configuring the Virtual SMTP Server (since the client CDO library would probably not cause such as issue) in the Operating Systems / SysAdmin[^] forum here on Code Project.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
You are right, that's server's problem.
Thank you so much!
|
|
|
|
|
Hi all,
Sorry in advance if the C# forum is the wrong place for this question, but none of the others looked quite right either...
I am trying to implement asymmetrical encryption on a web service using WSE 2.0. The example the MS includes in the Quick Start Examples that come with WSE works just fine, but I am trying to switch from the x509 cert that they provide to one that I created with makecert. Trouble is: I keep getting an error message saying that the key is bad when WSE 2.0 attempts the encryption.
My certs have private keys and the users under which both the client and server are running have full control over the directories that contain the private key files...
Has anyone managed to get this to work? Can you tell me what command line arguements you passed to makecert? Any guidance appreciated.
Thanks,
Bill
|
|
|
|
|
Bill Dean wrote:
I keep getting an error message saying that the key is bad when WSE 2.0 attempts the encryption.
Please be specific: what is the exception type and message? What are you doing in code when the exception is thrown?
Without this information, I can only guess that you're having one of three problems:- The X.509 certificate is not signed with the necessary OIDs that identify the certificate for client and/or server authentication.
- Since you made this certificate yourself, you most likely used a self-signing certificate authority (CA). You must add the CA to the trusted certificate store for the machine.
- The ceritifcate is not accessible because you've stored the private key into another user's ceritifcate store (like your local user's or the administrative store), or because the ASPNET user accountcannot access private keys by default and you're not impersonating another user (or running ASP.NET under a different user context).
I'm going to assume that since you have WSE 2.0 installed you have the Help 2 topics installed as well, so please read ms-help://MS.WSE20.1033/wse/html/34caf185-5484-42b3-98ea-3f17f3fc1f16.htm[^] and follow the links for more information.
If you post the specific exception type and message - as well as any other relevent data or code - we may be able to provide a better answer.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks Heath,
I should know better than to post such a detail-bare question. My bad.
My client application is a .net web application, running on the same machine that the web service runs on: a win2kpro development machine. I am trying to: 1) sign the request from the client with an x509 cert. 2) sign the response with an x509 cert, and 3) encrypt the response with an x509 cert.
1) and 2) are no problem. But when I add the code for 3) I get this exception:
System.Web.Services.Protocols.SoapHeaderException: Server unavailable, please try later ---> System.ComponentModel.Win32Exception: Bad Key at Microsoft.Web.Services2.Security.Cryptography.RSACryptoServiceProvider.Encrypt(Byte[] plaintext, Boolean useOAEP) at Microsoft.Web.Services2.Security.Cryptography.RSA15KeyExchangeFormatter.EncryptKey(Byte[] plainKey) at Microsoft.Web.Services2.Security.EncryptedKey.Encrypt() at Microsoft.Web.Services2.Security.EncryptedKey.GetXml(XmlDocument document) at Microsoft.Web.Services2.Security.Security.SerializeXml(SoapEnvelope document) at Microsoft.Web.Services2.Security.SecurityOutputFilter.ProcessHeader(Security security, SoapEnvelope envelope) at Microsoft.Web.Services2.Security.SecurityOutputFilter.ProcessMessage(SoapEnvelope envelope) at Microsoft.Web.Services2.Pipeline.ProcessOutputMessage(SoapEnvelope envelope) at Microsoft.Web.Services2.WebServicesExtension.AfterSerializeServer(SoapServerMessage message) --- End of inner exception stack trace ---
This exception is reported by the client and occurs sometime after the return in the web method is called.
The stack trace is:
SoapHeaderException: System.Web.Services.Protocols.SoapHeaderException: Server unavailable, please try later ---> System.ComponentModel.Win32Exception: Bad Key
at Microsoft.Web.Services2.Security.Cryptography.RSACryptoServiceProvider.Encrypt(Byte[] plaintext, Boolean useOAEP)
at Microsoft.Web.Services2.Security.Cryptography.RSA15KeyExchangeFormatter.EncryptKey(Byte[] plainKey)
at Microsoft.Web.Services2.Security.EncryptedKey.Encrypt()
at Microsoft.Web.Services2.Security.EncryptedKey.GetXml(XmlDocument document)
at Microsoft.Web.Services2.Security.Security.SerializeXml(SoapEnvelope document)
at Microsoft.Web.Services2.Security.SecurityOutputFilter.ProcessHeader(Security security, SoapEnvelope envelope)
at Microsoft.Web.Services2.Security.SecurityOutputFilter.ProcessMessage(SoapEnvelope envelope)
at Microsoft.Web.Services2.Pipeline.ProcessOutputMessage(SoapEnvelope envelope)
at Microsoft.Web.Services2.WebServicesExtension.AfterSerializeServer(SoapServerMessage message)
--- End of inner exception stack trace ---]
System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
SecureServiceLib.RestekSecureServiceClient2.Invoke(String methodName, Object[] parameters) in c:\projects\securewebservice\secureservicesolution\secureservicelib\clsresteksecureserviceclient2.cs:26
WSE2Client.localhost.Service2.HelloWorld() in c:\inetpub\wwwroot\WSE2Client\Web References\localhost\Reference.cs:45
WSE2Client.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\wse2client\webform1.aspx.cs:24
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
The code that adds the encryption is:
protected virtual void AddSignAndEncryptInformation()
{
_addResponseEncryption();
_addResponseSignature();
}
private void _addResponseEncryption()
{
SoapContext responseContext = ResponseSoapContext.Current;
X509SecurityToken e =this.GetResponseEncryptingSecurtyToken ();
responseContext.Security.Tokens.Add (e);
responseContext.Security.Elements.Add (new EncryptedData (e));
}
protected virtual X509SecurityToken GetResponseEncryptingSecurtyToken()
{
X509SecurityToken securityToken = null;
X509CertificateStore store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
bool open = store.OpenRead();
try
{
byte[] certHash=this.GetHash ("RestekClient");
X509CertificateCollection certs =store.FindCertificateByHash (certHash);
X509Certificate cert =
((X509Certificate) certs[0]);
if (cert != null)
{
securityToken = new X509SecurityToken(cert);
}
}
finally
{
if (store != null)
store.Close();
}
bool b= securityToken.Certificate.SupportsDataEncryption ;
if (b)
return securityToken;
throw new Exception ("Cert does not support data encryption");
}
private byte[] GetHash(string KeyName)
{
byte[] b=null;
if (KeyName=="RestekClient")
b=new byte[] {0xee, 0x2b, 0xba, 0xb4, 0xeb, 0x6d, 0xff, 0x1e, 0xf2, 0xc4, 0x70, 0xf9, 0x10, 0xa5, 0xfd, 0xf4, 0x03, 0xd8, 0xfb, 0xaf};
if (KeyName=="RestekServer")
b=new byte[] {0xab, 0xaf, 0xe2, 0x9d, 0xa2, 0x0a, 0xda, 0xd1, 0x02, 0x0d, 0xee, 0x50, 0xf1, 0xf1, 0xb5, 0xc1, 0x22, 0x4c, 0x1d, 0xe3};
if (KeyName=="WSEQuickStartServer")
b=new byte[] {0x87, 0xd9, 0x2c, 0xca, 0xb7, 0x14, 0xc3, 0x0d, 0xb0, 0xe0, 0xba, 0xe6, 0xb9, 0xb0, 0xb6, 0x5b, 0x5d, 0x6d, 0xf7, 0xcf};
return(b);
}
and the trivial webmethod I am using to test all this stuff is:
[WebMethod]
public string HelloWorld()
{
this.AssureCredentialsValid ();
this.AddSignAndEncryptInformation ();
return ("Hello World");
}
According to the WSE2 Cert tool, the ASPNET user has full control over the private key file for the RestekClient cert. Yes, the cert is self-signed. Any further guidance anyone can provide will be greatly appreciated.
Thanks again
Bill
|
|
|
|
|
For starters, make sure the CA that signed your X.509 certificate is installed into the trusted CA store of your machine.
If that fails I can look into it more. The certificate must be trusted, however, which is one likely cause of "Bad key" (it could also be that you didn't enable the right attributes for the key to sign and encrypt data, so be sure to check that as well).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thanks for the follow-up Heath.
The cert is self-signed, so I am assuming that I need to add the certificate itself to my trusted CA store...right? Assuming this is correct, I exported the cert from Local Computer \ Personal and then imported it into Local Machine \ Trusted Root Authority...but the problem remains.
How can I tell for sure if it is trusted?
How can I tell if I have enable[ed] the right attributes for the key to sign and encrypt data?
Thank for entertaining my questions...I've been through the WSE2.0 docs 1/2 a dozen times and cannot find the answer, so I am a little at whit's end. Any other good sources of documentation?
Bill
|
|
|
|
|
Actually, you must add the CA (the CA was self-signed, and then used to sign your certificate - at least that's what you were supposed to do) to the trusted CA store. If you don't, then the security implementation won't trust your certificate you use to sign and encrypt data because it doesn't trust what signed it.
Read a very old tutorial of mine about certificates and using OpenSSL at http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=209[^]. It may provide some limited insight.
You should not be using your self-signed CA for signing and encrypting data. You use that to sign certificates for certain purposes, and the CA must be set up to be able to sign with those privileges. My tutorial covers that, and there's a lot of information on the web about X.509, which is beyond the scope of this article.
You can look at the certificate in Windows to see what it is signed to do. Just double-click the file assuming it's in a format that Windows understands (like an X.509 DER file - a binary DER-encoded file). There usually have .pfx extensions (but of course the extension is meaningless if the file format isn't correct).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
|
I'm not a new programmer, but this one's got me stymied; hopefully it's a fairly trivial problem.
I'm using a socket connection to receive communication from a server. Normally, the entire message is received before the program moves on with its next instructions. However, at times when the processor is particularly busy the program seems to progress with its instructions without having received the entire communication. In other words, it seems to clean out the buffer, which due to the computer's business is holding only the first packet at this point, then move on without waiting for the buffer to fill with the next packet.
Is there a way to stop the program until it has been told that the entire communication has been received? Or a way to iteratively receive the packets until the program is told the communication is finished?
Here's my code:
<br />
IPHostEntry IPAdd = Dns.Resolve("servername.com");<br />
IPEndPoint IPEP = new IPEndPoint(IPAdd.AddressList[0], 2628);<br />
Socket s = new Socket(IPEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);<br />
string str = "Message"<br />
Byte[] Receiver = new Byte[50000];<br />
s.Connect(IPEP);<br />
Int32 bytes = s.Receive(Receiver, Receiver.Length, 0);<br />
Byte[] Sender = System.Text.Encoding.ASCII.GetBytes(str);<br />
s.Send(Sender, Sender.Length, 0);<br />
bytes = s.Receive(Receiver, Receiver.Length, 0);<br />
strResponse = System.Text.Encoding.ASCII.GetString(Receiver, 0, bytes);<br />
...Furthur code
Thanks for your help!
|
|
|
|
|
Take a look at some of the options from the SocketOptionName enumeration. This is used in conjunection with the SocketOptionLevel to set socket options using the Socket.SetSocketOption method. Not allowing your packets to be fragmented might be one option.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Interesting. I'll take a look.
Thanks.
|
|
|
|
|