Posts Tagged ‘.net’
How to Debug Window Service without installing
Posted by: admin in .NET, Window Services on September 3rd, 2010
Usually if we want to debug the windows service project to see if it is behaving as we expected, what we do is by installing the windows service and attaching that project to the process.
There is also another way to debug without installing it, what we need to do is by creating another project such as console project then copy and paste the code below.
-
Sub Main()
-
#If Not Debug Then
-
Dim servicesToRun As System.ServiceProcess.ServiceBase()
-
-
servicesToRun = New System.ServiceProcess.ServiceBase() {New MyService()}
-
System.ServiceProcess.ServiceBase.Run(servicesToRun)
-
#Else
-
Dim service As AS2Client.AS2SenderService = New MyService()
-
service.myentrysub()
-
-
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite)
-
-
#End If
-
-
-
-
End Sub
References:
Run Windows Service as a Console program
How to Capture form without showing it in .Net
-
using System;
-
using System.Drawing;
-
using System.Runtime.InteropServices;
-
using System.Windows.Forms;
-
-
public class ControlPainter
-
{
-
private const int
-
WM_PRINT = 0×317, PRF_CLIENT = 4,
-
PRF_CHILDREN = 0×10, PRF_NON_CLIENT = 2,
-
COMBINED_PRINTFLAGS = PRF_CLIENT | PRF_CHILDREN | PRF_NON_CLIENT;
-
-
[DllImport("USER32.DLL")]
-
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, int lParam);
-
-
public static void PaintControl(Graphics graphics, Control control)
-
{ // paint control onto graphics
-
IntPtr hWnd = control.Handle;
-
IntPtr hDC = graphics.GetHdc();
-
SendMessage(hWnd, WM_PRINT, hDC, COMBINED_PRINTFLAGS);
-
graphics.ReleaseHdc(hDC);
-
}
-
}
-
-
<strong>TO USE IT:
-
</strong>
-
using (Form f = new MyForm()) {
-
Image i = new Image(f.Width, f.Height);
-
Graphics g = Graphics.FromImage(i);
-
ControlPainter.PaintControl(g, f);
-
}
How to get the difference between two dates in .net
Posted by: admin in .NET, Date and Time on August 10th, 2010
You can use the built-in function for getting the difference between two dates.
Example:
-
Dim d1 As DateTime = Now
-
Dim d2 As DateTime = Now.AddDays(3)
-
-
Dim m As TimeSpan = d2.Subtract(d1)
-
Dim t As String
-
-
t = "Days : " & m.Days.ToString() & vbCrLf
-
t += "Hours : " & m.Hours.ToString() & vbCrLf
-
t += "Minutes : " & m.Minutes.ToString() & vbCrLf
-
t += "Seconds : " & m.Seconds.ToString() & vbCrLf
-
t += "Milliseconds : " & m.Milliseconds.ToString() & vbCrLf
-
MessageBox.Show(t)
sn.exe Access is denied
You can do this by running the command prompt as a administrator, just right click and run as administrator.
You can also refer to the following link if the above solution does not work:
- sn.exe fails with Access Denied error message
- How To Create an Assembly with a Strong Name in .NET Framework SDK
Convert color to string
To convert a color to a string use the ColorConverter class but this can be access through TypeConverter class;
-
Dim myColor as Drawing.Color = Color.Red
-
-
Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(myColor)
-
dim ColorName as string
-
-
ColorName = clor.ConvertToString(Color.Red)
Convert color from string
To convert a color from a string use the ColorConverter class but this can be access through TypeConverter class;
-
Dim myColor as Drawing.Color
-
-
Dim clor As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Drawing.Color))
-
-
myColor = clor.ConvertFromString("Blue")
MyColor variable is now ready to use.
An error occurred while validating. HRESULT = ‘80004005′
Posted by: admin in .NET, Deployment, Exception on July 27th, 2010
An error occurred while validating. HRESULT = ‘80004005′, this error just came up when I’m trying to organize my .Net Project files in my Solution, I removed some of the projects and just use the compiled dll in referencing to the main Project. However, this error has appeared during rebuilding the setup project and it took some of my time to find the solution or the cause.
So, what I did was I removed all References from the Target Primary Output project and added them and rebuilding it again ,
that’s it no more errors.
“Thank you for reporting this issue. We were able to reproduce the problem and have identified the root cause. The problem is caused by cross-solution project reference between Solution1 and Solution2. From the attached project, the project “WindowsFormApplication1” in Solution2 references a project that is not in Solution2 (it references ClassLibrary1 from Solution1). To fix the error, the workaround is to copy the ClassLibrary1 project to Solution2 and re-add the reference to ClassLibrary1 within its own solution.
Project-to-project references only works within the same solution. If you have to split into two solutions and split the code for your class library into two projects, you need to also split the project that references the class library into two projects (one for each solution) in order to avoid project references outside the current solution.
I hope this helps.
Candy Chiang
Program Manager – Visual Studio”
For more information and explanation about this error just click here
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information
Posted by: admin in Reflection on July 20th, 2010
I’m currently developing an application that has a plugin style or loading of DLL dynamically. So this is how I did I have created 4 projects;
- Plugin Interface project (Class Library)
- Test Plugin project (Class Library)
- Consumer project (Class Library) - read and execute the plugins
- Windows Apps that uses/references the Consumer project
When I run the #4 project I get this error “Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information“, as I’m just a beginner and can’t find a solution out of my head so I did research on the web and seen lots of solutions and recommendations but none of them works.
Then I tried to have the project referenced from #1 project and you know what it works :).
Extension method in .net
Extension methods enable developers to add a method to an existing class without deriving or affecting the existing class. This extension methods only support a sub routine or a function.
Example if we want to extend the functionality of the String type
-
Imports System.Runtime.CompilerServices
-
-
Module StringExtensions
-
-
<Extension()>
-
Public Sub Print(ByVal aString As String)
-
Console.WriteLine(aString)
-
End Sub
-
-
<Extension()>
-
Public Sub PrintAndPunctuate(ByVal aString As String,
-
ByVal punc As String)
-
Console.WriteLine(aString & punc)
-
End Sub
-
-
End Module
To use that function
-
Sub Main()
-
-
Dim example As String = "Example string"
-
example.Print()
-
-
example = "Hello"
-
example.PrintAndPunctuate(".")
-
example.PrintAndPunctuate("!!!!")
-
-
End Sub
C#.NET
If you notice the parameter there is a this keyword, that is how it done.
-
namespace ExtensionMethods
-
{
-
public static class StringExtensions
-
{
-
public static int Print(<strong>this </strong>String str)
-
{
-
Console.WriteLine(String );
-
}
-
}
-
}
-
-
string s = "Hello Extension Method";
-
s.Print();
Namespace or type specified in the project-level Imports ‘System.Xml.Linq’ doesn’t contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn’t use any aliases
There are 2 ways to remove this warning
First, you may need to convert the current .Net Framework used by the application to higher version, let’s say from .net framework 2 to 3.5
- Goto Property Pages of the web site project
- View Menu->Property Pages or press shift+F4
- Click the Build option from the left side
- Make sure the Target Framework is .Net Framework 3.5
Second, remove the namespace that shown in the warning in this case the “System.Xml.Linq” from the web.config
