Friday, October 28, 2005

Using Web Response

You can the HTTP WebRequest and WebResponse objects to contact a remote server through your code, send POST data and handle a response.

Below is a method to support this activity. The example code uses a payment gateway service, sending it POST data for a payment transaction and retrieving the response.

private object ProcessHTTPRequest(IPaymentProvider provider, string POSTdata)
{
ASCIIEncoding encoding=new ASCIIEncoding();
POSTdata = "VPSProtocol=2.2&TxType=PREAUTH&Vendor=MyVendor&VendorTxCode=55555&Amount=444&Currency=GBP&Description=TestDesc";
POSTdata += "&CardHolder=Test&CardNumber=000000000000&StartDate=0105&ExpiryDate=1205&IssueNumber&CV2&CardType=Visa&BillingAddress=sdklfjsdf&BillingPostCode=cmdmdm";
byte[] data = encoding.GetBytes(POSTdata);


// Create a new HTTP Web Request.
HttpWebRequest request;


try
{
System.Uri uri = new System.Uri(provider.AbsoluteUrl);
request = (HttpWebRequest)WebRequest.CreateDefault(uri);

}
catch (WebException exRequest)
{
throw exRequest;
}

// Set properties of the request object.
request.Method = "POST";
request.ContentType="application/x-www-form-urlencoded";
request.ContentLength = data.Length;

// *** WRITE THE POST DATA *** //
try
{
Stream newStream = request.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
}
catch
{}


// Create a new WebResponse object.
WebResponse response;

try
{
response = request.GetResponse();
}
catch (WebException exResponse)
{
throw exResponse;
}

string serverResponse = ""; // String for response output.

try
{
// *** READ *** //
// Get the response.
Stream responseStream = response.GetResponseStream();
// Create a stream reader from the returned stream.
StreamReader sr = new StreamReader(responseStream);
// Read the response.
serverResponse = sr.ReadToEnd();
}
catch (WebException exRead)
{
throw exRead;
}
catch (Exception genException2)
{
throw genException2;
}

// Return the response.
return serverResponse;

}

Monday, October 24, 2005

The Global Assembly Cache (GAC)

The Global Assembly Cache (GAC)

The Global Assembly Cache (GAC) is a central repository for storing shared assemblies. The GAC allows multiple versions of the same assembly to be installed concurrently and also prevents different assembly vendors from overwriting each other's assemblies.
The use of the GAC is encouraged for assemblies that will be used by more than one application on the system, and also for assemblies that are expected to be versioned in the future. When an application is launched, Mono extracts the name of the assembly, the version and its public key token and loads the required assembly from the GAC.
The GAC is a black boxed repository of assemblies capable of keeping multiple version/cultures of a same assembly. A strongname is considered a unique entry in the GAC (i.e. the GAC requires all it's assemblies to be strongnamed and signed). Note that the GAC has nothing to do with security! In fact assemblies in the GAC aren't verified when loaded from the runtime. The GAC is for manageability (avoiding DLL hell).

Source: http://www.mono-project.com/Assemblies_and_the_GAC

"The located assembly's manifest definition with name 'MyRootNamespace.MyNamespace' does not match the assembly reference."

"The located assembly's manifest definition with name 'MyRootNamespace.MyNamespace' does not match the assembly reference."

Ahhhh, this one is a joy, isn't it? Just when you think everything is working out fine - your latest dll error-free and nicely compiled, then bam - this lovely little son-of-a-gun comes along.

The error is caused by the assembly reference within your project being 'out of synch' with the assembly itself - i.e. there exists a reference to a different version of the assembly, be it older or newer. This can occur more commonly when you're throwing around new versions of a dll between projects and you forget to update the appropriate assembly reference info.

What to do if this happens? Well, if using VS IDE, simply re-saving should solve it, however if it doesn't, right click the reference under the 'References' folder, select 'Remove' and then add it back in again.

Thursday, October 06, 2005

"Requested registry access is not allowed"

Security Exception

Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

[SecurityException: Requested registry access is not allowed.]
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) +473

This is caused by an unauthorised user account trying to write to the event log.


MSDN says:

CAUSE

This problem occurs because the user account that you used to log on does not have sufficient permissions.

See: http://support.microsoft.com/default.aspx?scid=kb;en-us;842795

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Fixes to common .NET problems, as well as information on .NET features and solutions to common problems that are not language-specific.

Z