Tuesday, 22 June 2010

An error occurred creating the configuration section handler for system.serviceModel/bindings

Suddenly I got a problem when trying to access a web service,
the problem only occurred on my PC that has "Vista SP1 x64"
operating system, my workmates hasn't this problem at all, this is the exception
string appeared:


System.TypeInitializationException: The type initializer for
'my internal class name' threw an exception. --->
System.Configuration.ConfigurationErrorsException: An error occurred creating
the configuration section handler for system.serviceModel/bindings: Could not
load type
'System.Security.Authentication.ExtendedProtection.Configuration.ExtendedProtectionPolicyElement'
from assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
(D:\Projects\Hajj1431\Sources\Desktop\Hajj\bin\Release\Hajj.exe.Config line 38)
---> System.TypeLoadException: Could not load type
'System.Security.Authentication.ExtendedProtection.Configuration.ExtendedProtectionPolicyElement'
from assembly 'System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.


I googled and found some thing similar

here
,

here
,

here
and

here
. All of them points to a corrupted windows update with code KB982168, I
spend long time searching about KB982168 or any thing similar but nothing. Where
all of the previous threads were talking about Win2003 and WinXP updates.


I tried to restore my operating system to old date but Windows
Restore was disabled by group policy created by Domain Admin.


I tried to go deep in the problem to find which assembly cause
the problem but it seems it is some thing larger than a single assembly.


After long time of try and error I reached a solution by
installing Vista Service Pack 2.


Where, I noticed that Microsoft announced it will not support
Vista without SP2, so, I realized that any update my not work properly without
SP2, again searching, I found x64 version of mentioned SP2, it takes an hour to
download and another hour or more to install, but finally web-services is back
to work again.


Thanks god ...


Service Pack 2 (KB948465):



It is 745MB size and can be downloaded from this link:


http://www.microsoft.com/downloads/details.aspx?FamilyID=8ad69826-03d4-488c-8f26-074800c55bc3&displaylang=en&displaylang=en

Tuesday, 18 May 2010

The remote server returned an error: (502) Bad Gateway.

The remote server returned an error: (502) Bad Gateway.

Simply I want to access internet-world from inside my web-application using the following code.

public static string BrowseURL(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
//
request.Proxy = WebProxy.GetDefaultProxy();
request.Proxy.Credentials = CredentialCache.DefaultCredentials;
//
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
string responseData = reader.ReadToEnd();
return responseData;
}

On my development machine it works fine, but on production server I faced a problem of "Bad Gateway" when reaching GetResponse(), I realized that it was an authentication problem, and after some google_ing, I found a good thread that speaks about solving Proxy problems with authentication. Now, the following code is working properly on my production environment.

public static string BrowseURL(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "GET";
request.Credentials = CredentialCache.DefaultCredentials;
//that is the solution
WebProxy proxy = new WebProxy("proxy ");
proxy.Credentials = new NetworkCredential("userName", "Password", "Domain");
request.Proxy = proxy;
//
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
string responseData = reader.ReadToEnd();
return responseData;
}

Saturday, 15 May 2010

Conditional buidl Events using Viusal Studio 2008

Sometimes you need some extra actions to be trigged after build, eg. copy exe files. To add an extra action, go to project properties and select “Build Events” tab. You can add the additional actions before and after build process.

But what if some conditions are needed to check before performing your action. !

Two things you have to investe in order to know how to make conditional build event in VS2008, first, is the command macros introduced by Microsoft in VS2008, second, is the batch file ".BAT" commands.


Command Macros are
here.
BAT Commands are
here.

Example:

IF NOT $(ConfigurationName) == Release GOTO end
Copy "$(SolutionDir)\$(ConfigurationName).exe" "$(TargetPath)"
:end

Wednesday, 5 May 2010

C++ Macro Concatination

You can use ## operator (double number sign) to concatinate two tokens (text or arguments) in macro invocation.

Example:

enum Types {T1,T2};
#define MakeDrivedClass(name, type) class name##type : public type##Base{}
MakeDrivedClass(CMy,t1);

Output:
class CMyT1 :  public T1Base {};

Saturday, 3 April 2010

500.19 IIS7 ERROR

After createing a new ASP.NET application, and after publishing to certain folder, IIS7 refused to open the published site because of 500.19 error, IIS7 said that the requested page cannot be accessed because the related configuration data for the page is invalid.



But, in fact it is a security issues that prevent mister IIS7 from accessing web.config file. Particularly, it was computername\IIS_IUSRS that must take some permisions, read this article for more information.

Solution:
Simply add the following groups into security tab to solve this problem:
1- Authenticated Users
2- Users (computername\Users)



Tuesday, 16 March 2010

Allow only one instance of any Windows Form, C#

The ability to load forms once, is something essential in many applications. But, this is a simple way to do that:

public Boolean IsFormAlreadyLoaded(string formToLoadName)
{
    //Allow only one instance of any MDI child form in your MDI application
   
foreach (Form frmChild in this.MdiChildren)
    {
        if (frmChild.Name == formToLoadName)
        {
            frmChild.Activate();
           
return true;
        }
    }
   
return false;
}

Monday, 15 March 2010

Printer capabilities

This is an excelent article about printing essentials, and,  here is my own experiments using some different printers:

HP Officejet Pro All-in-One


HP Color LaserJet 3800
KONICA MINOLTA mc7450 PS