Thursday, February 27, 2014

WCF Concurrency and Instances

A quick tidbit regarding WCF in terms of performance.

You may be used to using the [ServiceContract] attribute to decorate your WCF method without giving it a second thought.  Then as time goes on you may notice that as more users hit your service the performance isn't what you were expecting.

One of the drawbacks to just using [ServiceContract] is that be default it is to create a new instance of your service which only handles one call.  This is fine if you need to do session management but in many cases a REST service will not.

A good practice is to decorate your methods with [ServiceBehavior] which allows you to change how the service operates. By using this behavior and settings the InstanceContextMode to Single you now have a service where all clients calling will use the same instance.  So if you are caching data then the performance from that can be realized.  In the default mode each client gets its own instance which takes up memory and doesn't serve anyone except the same caller on sequent calls.

Note: You thread safety becomes important in this case.

You can also change the ConcurrencyMode to determine how a call is handled.  In Single mode only one call is handled at a time.  So if you had a method that returned a number that is incremented continually, no call would get the same number because each call would wait until previous calls completed.

In a Multiple concurrency mode you would have to wrap the incrementing number with a lock or other thread safe mechanism otherwise you might get a conflict or deadlock because too many operations are trying to perform operations against the simple number field.

There is a 3rd mode called Reentrant which would be a special case that I've chosen not to cover here.

So in summary, look at adding the InstanceContextMode and ConcurrencyMode attributes found in the ServiceBehavior attribute instead of just a ServiceContract attribute to better control how your WCF service operates and to tweak performance based on how you need to service to scale/perform.

Wednesday, February 12, 2014

GAC Priority over Local

Something to keep in mind that came up today on a project.  If you have a DLL installed in the GAC and the same DLL in the local folder of the application then GAC will take priority.

In the case we ran across today there was a wrapper DLL given to multiple teams and someone installed the DLL in the GAC.  There was a bug that was found and updated in the wrapper but the DLL in the GAC wasn't updated.

This would be a good reason to version your DLL as well.  If the version in the GAC doesn't match the local folder then the local folder would be utilized.

Thursday, February 6, 2014

SQL Field Encryption

If you need to encrypt some data for security in SQL here is how you go about it...

1. You need to create a master key in your database if one does not already exist
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'YourPassword';

2. Create a certificate.
CREATE CERTIFICATE YourCertificateName WITH SUBJECT = 'Some Description';

3. Create a symmetric key.
CREATE SYMMETRIC KEY YourKeyName WITH ALGORITHM = DES
ENCRYPTION BY CERTIFICATE YourCertificateName;

4. To encrypt a field. Example below will encrypt each YourField column in table YourTable. NOTE: Be sure to set your encrypted fields to varbinary(128) so you can see the field value. Otherwise the field may appear blank.
OPEN SYMMETRIC KEY YourKeyName DECRYPTION BY CERTIFICATE YourCertificateName;
UPDATE YourTable SET YourField = EncryptByKey(Key_GUID('YourKeyName'), YourFieldName);
CLOSE SYMMETRIC KEY YourKeyName;

5. To decrypt your field. This will put the field YourField back to plain text in the output query.

OPEN SYMMETRIC KEY YourKeyName DECRYPTION BY CERTIFICATE YourCertificateName;

SELECT CONVERT(VARCHAR, DecryptByKey(YourField)
    AS 'Decrypted Field'
    FROM YourTAble;

 CLOSE SYMMETRIC KEY YourKeyName;

Wednesday, February 5, 2014

WHS 2011 Web Access Setup


In order to be able to access your WHS 2011 server remotely via the web you'll  have a couple of setup steps.

1. Enable Web Access. Open the Dashboard and one of the tasks will be enabling the web access.  It will enable it but it may fail due to router settings.

2. Enable port forwarding on your router.  Technically, you could DMZ the server but its not secure or the preferred method.  Port forwarding is a better option.  You'll want to record the internal IP that your server is on and forward the appropriate ports to your server.

Ports to forward:
443 is HTTPS which is what the remote access site included with WHS will run under.
3389 is the RDP port if you want to remotely access your server using Remote Desktop.
80 is HTTP is you're going to host a website or something.

There are other ports for mail, ftp, etc if you're interested in enabling those for your purposes.

If you're looking for a free solution to access your server with a friendly name rather than the public IP (which can change) I would recommend looking at No-Ip.com.  They have an executable that can run as a service to update their DNS when yours changes.  So you get a friendly IP even without a static public IP and its free.


Sunday, February 2, 2014

Windows Home Server 2011 Client Connector Woes

I tried to fire up my original Windows Home Server (WHS) machine but one of the drives was failing and the system wouldn't boot due to file corruption.

So I built a new box running WHS2011 which I was able to buy for $50 at the local computer parts store.  That version is basically a flavor of Windows Server and even has IIS installed so its great for a home development box for whatever projects you have.

Unfortunately, upgrading wasn't simple.  The new connector software complained there was already a version of the connector installed.  The version installed didn't want to uninstall.

I tried to manually delete the files and some registry entries but the new connector still complained.  Additionally, it caused my active window to lose focus because an installer was trying to load up constantly.

After a few hours I figured it all out.  I went through every entry in the registry with "Windows Home Server" and deleted it.  This made the connector installer work, but it would fail part way through and rollback.

I found a Microsoft KB article about a service needing to be enabled and at least set to manual if not started.  The name escapes me but it was Media Center related.  As it turned out I deleted some registry entries for WHS related to Media Center thinking the new connector install would replace them.

To fix this issue I went to Programs & Features and uninstalled all the "Media Features" which required a reboot then installed them again.  Then the new connector installed no problems and everything works great.

Suggestions that might help you circumvent these issues:
1. Create a restore point on your system and save backups of the registry.
2. Uninstall the old WHS connector while your old server is still running or before upgrading the software. I think part of the uninstall issue might have been the uninstaller trying to remove the user from the WHS machine and couldn't because mine was offline.
3. Try using a different uninstall program like CCleaner.