Very useful tip – sometimes you need the public key for referencing the assembly in things like web.config – see Getting Public Key Token of Assembly Within Visual Studio – Kirk Evans Blog – Site Home – MSDN Blogs.
Archive for the ‘Uncategorized’ Category
Getting Public Key Token of Assembly Within Visual Studio – Kirk Evans Blog – Site Home – MSDN Blogs
Friday, September 3rd, 2010Windows Forms Designer gives “Value does not fall within expected range” error
Friday, September 3rd, 2010I was working on a project that I’ve been working on for a very long time in Visual Studio 2010. I opened a designer and got the error “Value does not fall within expected range”, which was accompanied by a particularly useless stack trace and a dead link to a help article.
Cleaning the solution, closing Visual Studio and reopening it seemed to fix it for me. Hope this helps!
Companies House Electronic Filing Problems Part 1 – Introduction
Wednesday, September 1st, 2010I’ve been doing a fair amount of work over the years for a limited company formation agent and unfortunately, this has meant dealing with Companies House. I’ve not really had much other work where I’ve had to interact with government systems, but I think that, to put it politely, working with Companies House on a technical level has its own unique challenges.
Before I go into too much detail, why am I posting this? I guess mostly it is because this is my blog and it seems like a good place to vent frustration, but there is also an element that I hope it may help some other people, whether working on similar systems that integrate with Companies House, other government agencies or just other systems that might have similar issues.
If you don’t feel that it is useful then by all means, please feel free to ignore this series of posts – no one is forcing you to read it!
As a bit of background, Companies House is the government agency responsible for forming and maintaining the records for companies, including (and mostly) limited companies in various different forms. My client is a company formation agent, which means that they deal with forming companies, providing suitable documents for the constitution of the company called memorandum and articles or M&As for short (there are standard ones, but they have all sorts of legal problems) and providing advice about forming companies. They also have a list of “off the shelf”/”pre-made” company names – companies that they have already set up and are ready for someone to buy and trade with immediately.
As well as all this, they provide a number of other services, mostly targeted towards accountants. I’m not really going to go into much detail about these, because they don’t involve working with Companies House and that is the topic of this series of posts.
From a technical point of view, it all started quite a number of years back (I forget when exactly), when Companies House started accepting incorporations electronically. This was done via a rather bizarre system of us sending and receiving emails with numbered tags in to pass the data back and forwards. This was subject to all sorts of problems and was not very human readable (when the problems happened to try to debug them), so Companies House eventually decided to replace their quirky tag-by-email system with a more modern system of POSTing XML data to a gateway using HTTPS. They even elected to use the government standard GovTalk encapsulating envelope system to standardise the communications.
Anyway, that’s the brief background – I’ll post some more detail in future posts.
- Companies House Electronic Filing Problems Part 1 - Introduction
sp_change_users_login and ALTER USER
Monday, August 23rd, 2010I previously posted about using sp_change_users_login to repair users that have become disconnected from their logins eg. after transferring a database from one server to another. I’ve just noticed that Microsoft have said that they will be removing sp_change_users_login from future versions of SQL Server and to use ALTER USER instead, so I thought it would be interesting to look at the functionality of the 2, first of all to see how to use ALTER USER to achieve the same functionality as the ‘auto_fix’ and also to see what other functionality is available in the 2 alternatives.
Just a bit of background first in case you aren’t clear on the SQL Server terminology. A login consists of a username and some means of authenticating (usually a password or Windows authentication, but it could be a certificate or key). It also has a default database and language. It is stored at the server level and can have server wide roles. This doesn’t affect the rights within a specific database though.
A user has a name and is connected to a login. It is stored at the database level and it has particular roles for that specific database that affect the permissions for that user. A login can have multiple users, but a user can only have one login. When you log in as a specific login and try to access a database, SQL server looks to find a user in that database that maps to the login and applies permissions appropriately.
When you move databases from one server to another, reinstall SQL server as a new instance or similar situations, the user and the login become disconnected and you can’t login any more. In principle, you could just delete the user and recreate it from the login, but that isn’t always practical, either because you have lots of them to do, or commonly because the user owns certain objects in the database and it isn’t in a state where it can be deleted. In practice, therefore, it is necessary to have a way to reconnect a user and login.
My previous post was about how to reconnect these using sp_change_users_login with the ‘auto_fix’ parameter, but as I have said, Microsoft are removing that and we are supposed to use ALTER USER instead.
So what else can sp_change_users_login do apart from reconnecting users and logins? It has 4 parameters: -
- @Action
- @UserNamePattern
- @LoginName
- @Password
The @Action one is the one that we’ve been using as ‘Auto_Fix’ (capitalisation isn’t important). There are 2 other valid values for @Action, which are ‘Report’ and ‘Update_One’. I’ll come back to these in a minute, but first, let’s see what else we can do with ‘Auto_Fix’.
The ‘Auto_Fix’ action has a second usage – if you specify a password in @Password then it will create the login if it doesn’t already exist. If the login does already exist then @Password is ignored. Either way, @LoginName must be left out or explicitly set to NULL.
The ‘Report’ action reports on any users that aren’t linked to a login. @UserNamePattern, @LoginName and @Password should all be set to NULL or left blank. This is useful if you want to check to see if there are any unmapped users that can be deleted or if anything has gone wrong and users have been disconnected. This also returns the security identifier (SID) with the username. The SID is SQL Server’s ID field for keeping track of users. This is stored as a GUID, which is a binary field. I’m not going to post about GUIDs here, but I may post more about them in future.
The final type of action is ‘Update_One’. This attaches a user to a specific login. @UserNamePattern must be set to the correct user, @LoginName must be set to the correct login name. @Password should be left out or NULL. Presumably this will re-map a user that is currently connected to an existing login to a different one if you want, but I haven’t actually tested this.
That’s everything that sp_change_users_login does. The question is, how to reproduce this functionality with ALTER USER and any methods that aren’t marked as obsolete. I see there being 5 possible operations here: -
- reattach a user to a matching login name by just specifying the user (‘auto_fix’)
- create a login name for a user by specifying a password (‘auto_fix’)
- reattach a user if the login exists and if not then create it with the specified password (‘auto_fix’)
- find users that aren’t attached to a login (‘report’)
- connect a user to an existing login (‘update_one’) (I don’t think it is relevant whether or not the user is currently connected – this is still one operation)
I’ll go through these one at a time (please note that this bit isn’t tested, so it might need some correction, but it should work in principle)…
reattaching a user to a login: -
ALTER USER username WITH LOGIN = loginname
where username and loginname are the same.
create a login name for a user by specifying a password: -
CREATE LOGIN loginname WITH PASSWORD = password;
GO
ALTER USER username WITH LOGIN = loginname;
Again, where loginname and username are the same and password is the password.
reattach a user if the login exists and if not then create it with the specified password: -
The simplest way of doing this is actually to just do the same as above -
CREATE LOGIN loginname WITH PASSWORD = password;
GO
ALTER USER username WITH LOGIN = loginname;
Again, where loginname and username are the same and password is the password. If the login exists, the first statement will throw an error, but the second one will use the login anyway. This is fine if you don’t care about errors. If you want something a bit more robust then you’d have to do something like using IF EXISTS on a query to find the name in master.sys.server_principals, checking that you are looking at a type of principle that is a login type and not a role or anything else. If it doesn’t exist then use the above top line to create it, then end the IF and then either way run the ALTER USER statement to attach the user to the login.
to find users that aren’t attached to a login: -
select UserName = name, UserSID = sid from sysusers where issqluser = 1 and (sid is not null and sid <> 0x0) and (len(sid) <= 16) and suser_sname(sid) is null order by name
ALTER USER username WITH LOGIN = loginname;
This time, username and loginname may actually be different. This is exactly the same code as above though.
It does appear as though there are really only 2 SQL statements that you need to be able to reproduce the majority of the functionality of this stored procedure, so it shouldn’t be a major problem to replace your code.
SagePay VSP Access becomes Reporting and Admin API but the protocol documentation is wrong!
Friday, August 13th, 2010I just received an email this morning from SagePay to warn of their latest payment system update. For those of you who don’t know – SagePay is the (relatively) new name for ProtX since Sage bought them. ProtX developed a system back in 2008 called VSP Access, which was designed to allow you to programmatically access payment details for payments that have been put through using one of their payment systems (Form, Server or Direct (previously VSP Form, VSP Server and VSP Direct)). For some reason, ProtX used to be downright secretive about this system and were difficult to get the information out of about it. However, this is very useful for one of my clients as it allows us to confirm payments have actually gone through, keep track of refunds and spot any that somehow made it through without being logged by any of my systems (I don’t think that has ever actually happened, but it is a good double-check). They use that report for entering payments into their accounts software.
Anyway, back to the email I got today… SagePay are warning that they are upgrading their systems between 27th and 29th August and first of all, if anyone has IP addresses of their payment gateways hard coded, they need to update them (sounds like a bad idea anyway), and secondly, to say that if anyone is using the Reporting and Admin API then please check the Reporting and Admin API Protocol document (linked to in the email) as the code may need updating.
First of all, a couple of minor observations: -
- They are allowing 14 days notice to make these changes. That isn’t a great deal of time as many systems have a schedule of upgrades that are rolled out. If anyone is in the middle of a big upgrade that could be a real pain in the neck.
- The new name is rubbish – which marketing guy thought that “Reporting and Admin API” is easier to remember than “Access”?
However, that’s not the real problem. The real problem came when I looked at the document.
The document says
All requests are sent to one gateway at address:
https://test.sagepay.com/access/access.htm (Test)https://live.sagepay.com/access/access.htm (Live)
The XML field will contain the XML message, which always takes the following format:<vspReporting and Admin API><command>whatever</command><vendor>Vendor Name</vendor><user>User name</user><other command specific parameters in here…./><signature>MD5 Hash Signature</signature></vspReporting and Admin API>
Extension Methods stop working!?
Wednesday, August 11th, 2010I’m working on a solution where I have several different projects. Some of these projects are class libraries (DLLs) and some projects reference others. In some of them I have a number of extension methods.
I’ve noticed something strange happening occasionally. The extension methods in one of the class libraries stop functioning as extension methods for no apparent reason. This is even if they are called from within the same class library. It is usually after I have changed something, but not anything significant, and the namespace references all appear to be correct when I check. This isn’t just intellisense either – the build fails compilation.
The simple solution to this problem that I have found (which may solve other weird compilation bugs as well) is simply to clean the solution, close Visual Studio, reopen it and build again. This has worked every time that I’ve tried it so far.
This applies to Visual Studio 2010 Professional, and possibly 2008 as well, but I can’t remember if I had that problem then.
Why I’m not posting how to get .NET 4.0 working in Windows 2000
Tuesday, August 3rd, 2010The most popular part of my blog is the posts that I have made on how to get .NET 3.5 (partially) working in Windows 2000, so why am I not continuing this with .NET 4.0?
The truth is that I originally did the .NET 3.5 approach for a specific project where a client of mine had a large number of Windows 2000 PCs. I wanted to be able to use .NET 3.5, and I didn’t want him to be saddled with the upgrade costs. However, since then, he has been gradually replacing those PCs with Windows XP and now Windows 7 PCs, until he only had 2 or 3 Windows 2000 machines left. I recommended to him that it was better for him to upgrade/replace the last ones rather than me trying to get .NET 4.0 working on the old ones, which probably wouldn’t be as cost efficient.
I realise that this might be a disappointment to some people who have still got lots of Windows 2000 machines out there. If anyone does come up with a way of doing it (possibly based on my .NET 3.5 approach), please let me know and I’ll post it up, crediting you.
Unhandled exception clr20r3 mxyabj2rsfg4uknkgmspj2kfpmzxhcc5
Tuesday, August 3rd, 2010A few weeks ago, I hit a problem that an application that I wrote suddenly started randomly dropping out with an unhandled exception. I can’t remember all the specifics anymore, but I do remember that the event that was written to the logs (that can be viewed in Event Viewer in Administrative Tools in Control Panel): -
EventType : clr20r3
P9 : mxyabj2rsfg4uknkgmspj2kfpmzxhcc5
The most confusing thing about this was that I have a system built in to the application to catch unhandled exceptions, and this had been working fine in the past for a few years. I couldn’t understand why it wasn’t catching this exception.
What was even more confusing was that it was randomly affecting several machines in different sets of circumstances. I eventually managed to find something that would reliably reproduce the problem, but when I tried it on my development PC in the debugger, it worked fine (I still haven’t worked out why this is).
I initially thought that maybe it was because I had just upgraded to Visual Studio 2010 and tried to back-convert my application back to Visual Studio 2008, but the problem continued. I also tried using remote debugging to track down the problem, which also caused a lot of problems as the application still seemed to be dropping out even with the remote debugger running!
I eventually tracked down the problem using a Try, Catch clause around code which I knew would reproduce the problem. The exception was about a missing resource file. I hunted around and eventually found that the resx file for the error handling dialogue window seemed to have been excluded from the project, presumably due to some bug while it was being upgrade to Visual Studio 2010.
After I put the resx file back, I was able to see that the application was throwing an exception. My code for dealing with the unhandled exception had caught it and been throwing the other exception, which didn’t have anything to catch it, so the Common Language Runtime (or Windows?) killed it.
Edit: The actual exception type that was being thrown was System.Resources.MissingManifestResourceException











