Sunday, December 31, 2006

FREE Book: Petzold's .NET Book Zero

Charles Petzold is offering a free eBook on his site. The book is called: What the C or C++ Programmer Needs to Know About C# and the .NET Framework.

http://www.charlespetzold.com/dotnet/

Method overloading in webservices created with Visual Studio 2005

When creating a webservice in Visual Studio 2005, the IDE adds the following WebServiceBinding:


[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

This line indicates that your webservice conforms to the Web Services Interopability Organization's (WS-I) Baisc Profile 1.1. The Basic Profile defines a set of rules to which your webservice must conform.

One of its rules is that the webservice may not include method overloading. If you run a webservice that uses method overloading lile the one below, you will receive an errormessage telling that your service is not conform to WS-I Basic Profile v1.1:

Code:
using System.Web.Services;

[WebService(Namespace
= "http://tempuri.org/"
)]
[WebServiceBinding(ConformsTo
=
WsiProfiles.BasicProfile1_1)]
public class
Service : System.Web.Services.WebService
{
[WebMethod(MessageName = "HelloWorld")]
public string
HelloWorld()
{
return "Hello World";
}
[WebMethod(MessageName = "HelloWorldWithName")]
public string HelloWorld(string name)
{
return "Hello world " + name;
}
}


Exception:

Message: Service 'Service' does not conform to WS-I Basic Profile V1.1. Please examine each of the normative statement violations below ...

To be able to still use method overloading, you need to change the WebServiceBinding as follows
:
[WebServiceBinding(ConformsTo = WsiProfiles.None)]


References:

Wednesday, December 20, 2006

My first .NET Applet

In one of the current projects I'm working on, performance has become an issue. To gain more performance, we decided to implement the solution as a .NET applet. At first it looked a bit strange. I always associated applets with Java. But apparently this is not the case.

A .NET applet is nothing more than a windows form control hosted in Internet Explorer. As a sample, I've created a small application that shows the current time in a .NET applet.

Here is how to do it:
First you need to create a new Windows Control Library project. Add a button and a textbox to the usercontrol.



Add code to the Click event of the button as shown below:

using System;



using System.ComponentModel;



using System.Drawing;



using System.Windows.Forms;







namespace MyApplet



{



public class TimeControl : UserControl



{



public TimeControl()



{



InitializeComponent();



}







private void btnShowTime_Click(object sender, EventArgs e)



{



txtTime.Text
= DateTime.Now.ToString();



}







public void ShowTime(object sender, EventArgs e)



{



txtTime.Text
= DateTime.Now.ToString();



}



}



}
Now that you have your control, you need to create an application that hosts it. In this example we will use Internet Explorer as host. 
So add a new website to your solution. Within the default.aspx page, you will create a reference to your control via an object tag. To be able to do this, you need to copy the DLL to your website. Don't copy it in the bin folder because that folder isn't browsable. The object tag will look like this:
<object id="applet1" classid="http:MyApplet.dll#MyApplet.TimeControl"></object>
The classid needs to be in the format http:NameOfDll.dll#Namespace.ClassName.
NOTE: It is best to use a separate tag to close the object, otherwise the tags after the object tag can give problems.
If you now run the example it should look like the screen below:
 
Note:
  • .NET applets are restricted to Internet Explorer.
  • In some companies ActiveX components are not allowed.

Tuesday, December 19, 2006

Microsoft® Visual Studio® 2005 Service Pack 1

Microsoft has released a Service Pack for the Visual Studio 2005 products. Fixes in the Service Pack include stability, perfomance and security enhancements made in many areas of the product.

Additional info:
http://support.microsoft.com/?kbid=928957

Download:
http://www.microsoft.com/downloads/details.aspx?familyid=BB4A75AB-E2D4-4C96-B39D-37BAF6B5B1DC&displaylang=en

Thursday, December 14, 2006

Editing a .NET project file from within the WSDL tool

For one of my projects I had the need to customize the proxy classes created by the WSDL tool when performing "Add Web Reference..." within Visual Studio .NET 2005. When the WSDL tool detects a class that it doesn't recognize, it creates a new class definition within the proxy class. In case of having this new class definition, I wanted to use my own class definition.

After googling a while, I bounced on an article from Jelle Druyts who explained exactly what I needed (Article).

The only difference for my particular scenario was the need for a file with the mappings in the project folder. The problem is that when implementing a custom class that extends the SchemaImporterExtension, you don't have immediate access to the calling project.

With a little help from a colleague, Gabriel Lozano-Morán, I succeeded to get a reference to the calling project.

This is the code to retrieve the calling project from within the custom SchemaImporterExtension:

using EnvDTE;
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

public class CallingClass
{
public static void ChangeProjectName(string newName)
{
_DTE ide
= DTEHelper.GetCurrentIDE();
Array projs
= (Array)ide.ActiveSolutionProjects;

// The proj object allows you to edit the project.
Project proj
= (Project)projs.GetValue(0);

proj.Name = newName;
}
}

public static class DTEHelper
{
[DllImport(
"ole32.dll")]
public static extern uint GetRunningObjectTable(
uint reserved, out IRunningObjectTable ROT);


[DllImport(
"ole32.dll")]
public static extern uint CreateBindCtx(
uint reserved, out IBindCtx ctx);



public static _DTE GetCurrentIDE()
{
IRunningObjectTable runningObjectTable;
IEnumMoniker iterator;
IMoniker[] monikers
= new IMoniker[1];
    int currProcId =
System.Diagnostics.Process.GetCurrentProcess().Id;
    // Get a pointer to the running object table.
GetRunningObjectTable(0, out runningObjectTable);
    // Enumerator to loop through the running objects.
runningObjectTable.EnumRunning(out iterator);
    // Reset the enumeration sequence to the beginning.
iterator.Reset();
    IntPtr numberFetched = IntPtr.Zero;
while (iterator.Next(1,
monikers,
numberFetched) == 0)
{
IBindCtx ctx;
CreateBindCtx(
0, out ctx);
      string runningObjectName;
monikers[
0].GetDisplayName(
ctx,
null, out runningObjectName);
Marshal.ReleaseComObject(ctx);
      object runningObjectValue;
runningObjectTable.GetObject(
monikers[
0], out runningObjectValue);
      // Reference to the IDE if the object is the
// current Visual Studio environment.
if (runningObjectName ==
String.Format(
"!VisualStudio.DTE.8.0:{0}",
currProcId.ToString()))
return (_DTE)runningObjectValue;
}
    return null;
}
}

First post !

Hi all,

Welcome to my blog. Since this is my first blog it can take a while to get something decent on it! Anyway, it is the intention to write down some of my experiences that I gained during my career as a developer. I will mostly focus this blog on .NET and SQL Server related topics.

If you have any questions or comments, feel free to contact me!

Geert