The difference between Visual Studio's WCF Service Application and WCF Service Library project templates

July 7, 2009 08:32

I've been working with WCF lately. One of the immediate points of confusion was which type of project to start out with: WCF Service Application or WCF Service Library. Let's take a look at each, then examine the differences. Hopefully we can come to some conclusion regarding the time and place to use one or the other.

The WCF Service Application Template

You create a WCF Service Application by way of Visual Studio's "Add New Project" dialog:

image

The end result is this:

image

It's no coincidence that the structure is similar to that of a web application. Specific to the WCF service, you have the service contract (IService1.cs), service implementation (Service1.svc.cs), and the web configuration file (web.config). In addition, we get a service host file (Service1.svc).

The WCF Service Library Template

Similar to the Service Application template, you create a WCF Service Library by way of Visual Studio's "Add New Project" dialog:

image

This produces a project tree as:

image

Much like the WCF Service Application project, a service library has a service contract (IService1.cs) and service implementation (Service1.cs). Instead of a web configuration file, though, we have an application configuration file (app.config). Also, no service hosting file.

With regard to the app.config file, MSDN states:

Visual Studio is configured to recognize the App.config file as the configuration file for the project when it is run using the WCF Service Host (WcfSvcHost.exe), which is the default configuration.

So what's the diff?

If you do a file comparison between like files you'll find the code is identical. Even the Service Application's web.config and Service Library's app.config are nearly the same (when looking at just the <system.serviceModel> sections). Also, both projects, when compiled, produce DLL's.

Perhaps the best way to illustrate the major difference is to run each project.

First, running the WCF Service Application give us the usual "Debugging Not Enabled" dialog just like any other web app:

image

Click-through that and you get the default directory listing:

image

Click on the Service1.svc hosting file to see the actual service:

image

As one might expect, the Service Application runs just like any other web app and is, in fact, hosted by default by the ASP.NET Development Server:

image

Side note: One bit of puzzlement came when I also saw the WCFSvcHost app come up when running the WCF Service Application (NOT the Service Library):

image

Turns out there is an option in the Service Library project that says:

image

Un-checking that box keeps WcfSvcHost from coming up when I'm running another application in the solution.

End side note

OK, now let's run the WCF Service Library and see what happens. It's hosted not by the development server, but by the WCF Test Client:

image

Why is this? Because the Service Library project does not, by default, have a service hosting file or a web.config. You can remedy this by adding each, but then you're essentially creating a WCF Service Application, right?

This gets us back to the original question, which I think can be answered inasmuch as the web hosting option is concerned with a definite "not much". Whether you choose Service Application or Library, you still need a .svc hosting file (you'll have to create one manually for the Service Library project), you still need to move the <system.serviceModel> config info from the WCF project to the web app config, and you still need either the codebehind file (from the Service Application) or the dll (from the Service Library).

The only reason I can see for going with a WCF Service Library is if you want to host the WCF service in something other than IIS, like, for example, a Windows service.

Conclusion

Hopefully I've illustrated the differences between a WCF Service Application and WCF Service Library, and you'll be able to make that first decision a little more easily now.

References

MSDN: WCF Visual Studio Templates


Can't establish a connection to the ASP.NET development server at localhost

March 23, 2009 12:13

I recently started getting "Failed to Connect, Firefox can't establish a connection to the server at localhost" when trying to debug a web page from Visual Studio using the ASP.NET development server. This was happening regardless of the project being new or not.

I found a temporary workaround: use 127.0.0.1 instead of "localhost". But this is a hassle because you have to edit the address each time.

Another workaround is to simply switch your project to use IIS instead of the ASP.NET development server:

image

This works, but is not ideal if you don't want to use IIS.

So, I did some digging and came up with this post which did the trick. The gist of it is to edit the "hosts" file which, in Vista, is located at "c:\windows\system32\drivers\etc\hosts" (you'll need admin rights to edit). If there's an entry called "::1 localhost", comment it out and add "127.0.0.1 localhost".

This worked like a charm for me.


Visual Studio 2008 Shortcuts

January 4, 2009 10:57

See my analogous post on Visual Studio 2010 Shortcuts.

Keyboard Shortcuts

1. Ctrl-L to Cut Line to Clipboard

ctrl_key_smallplus letter_l_small

It's not necessary to highlight an entire line and then hit the delete key. Just hit "Ctrl-L" and the line the cursor is currently on will be removed and copied to the clipboard. "Ctrl-x" performs the same operation by default.

 

2. Shift-F5 to Stop Debugging

shift_key_smallplus f5_key_small

To stop debugging a project, use "Shift-F5". Much quicker than going through the Debug menu.

 

3. Create new property

propplustab_key2_smallplustab_key2_small

Add a new property in code by typing prop then hitting the Tab key twice. This "prop-TAB-TAB" combination will create a property template that looks like:

   1: public int MyProperty { get; set; }

 

4. Comment/Uncomment code

ctrl_key_smallplus letter_k_smallplus letter_c_small

You can comment or uncomment code by highlighting the respective code block and hitting "Ctrl-k-c" to comment or "Ctrl-k-u" to uncomment. This shortcut even works in .aspx pages.

image

image

 

5. Reformat a block of code

ctrl_key_smallplus letter_k_smallplus letter_f_small

If you've just pasted in a bunch of code where the formatting is screwed-up, just highlight it and hit "Ctrl-k-f" to format.

6. Reformat entire document

ctrl_key_smallplus letter_k_smallplus letter_d_small

If you want to reformat all of the code in the current document, hit "Ctrl-k-d".

7. Find matching curly brace or region

ctrl_key_smallplus right_curly_brace_paren_small

Position the cursor next to a curly brace ('{' or '}') and hit "Ctrl-}'. This will toggle the cursor between the matching braces.

image

This shortcut will also work for #region...#endregion's.

8. View coding window full-screen

alt_key_smallplus shift_key_smallplus enter_key_small  

Hitting "Alt+Shift+Enter" will make the coding window full-screen and (temporarily) drop most other windows from view. Restore to your usual window configuration by de-pressing the "Full Screen" button at top:image

9. Search without the dialog

ctrl_key_smallplus f3_key_small

ctrl_key_small plus shift_key_smallplus f3_key_small 

Just highlight the text you want to search on and hit "Ctrl-F3" to search down or "Ctrl-Shift-F3" to search up.

10. Toggle Outlining

ctrl_key_smallplus letter_k_smallplusletter_l_small  

If you want to open (expand) or close (collapse) all outlining in a code file, use "Ctrl-k-l". It works like a toggle, alternatively expanding or collapsing all outlining in the current file.

11. Code Snippets

ctrl_key_smallplusletter_k_smallplusletter_s_small  

Easily open a code snippets selection dropdown with "Ctrl-k-s":

image

 

12. Go to Definition

f12_key_small

Put the cursor on an item and hit 'F12'. Instant 'go to definition' functionality.

13. Add/Remove Breakpoint

f9_key_small

'F9' will toggle breakpoints for the current line.

14. Find Usages

shift_key_smallplusf12_key_small 

'Shift-F12' will find all usages of the item the cursor is currently on and display the "Find Usages" dialog (this is ReSharper's version of the dialog; standard VS2008 looks a little different):

image

Mouse Shortcuts

1. Right-click to add references

Say you've just entered a line of code like the following where the red squiggly indicates you haven't yet added the necessary reference:

image

You could jump to the top of the file and type it in manually, or you could right-click on the squiggly-indicated word and choose "Resolve | using …":

image

This will add the needed reference for you, no typing required.

2. Remove unused references

Many times there are references included in the "using…" section that are not needed. Removing the unnecessary ones is easy: Right-click anywhere on the code file, select "Organize Usings | Remove Unused Usings".

image

3. Sort usings

To sort your "usings…" section, right-click anywhere on the code file and select "Organize Usings | Sort Usings".

image

4. Open a file's "containing folder"

Right-click on an open file's tab, select "Open Containing Folder".

image

5. Copy a file's folder location to the clipboard

Right-click on an open file's tab, select "Copy Full Path".

image

6. Close all files but "this" one

If you have more than one file open, you can right-click on one of the file tabs (the one which you want to stay open) and choose "Close All But This". This will close all other files.

image

7. Close tab

A quick way of closing a tab is to use the mouse scroll button. Hover the mouse cursor over the tab, click the mouse scroll button, and the file (and it's tab) will close.

Conclusion

Again, if anyone has anything I missed (I know there's got to be a lot), leave a comment. I'd love to learn some new ones!

Sources:

 

[ Follow me on Twitter ]