.Net: simplest WCF service and client

In VS2015, I wanted to create the very simplest possible WCF service, and a client, in the form of a console app, to prove it works.

In VS, use the WCF template to create a WCF service library. As the template is a Hello World, I choose to call it HelloAcmeService (I don’t know why I’m randomly picking fictitious brands):

That creates an interface file, and a class file, which contain more than I want, so we strip those back to a minimum, and do some renaming.

Before:

HelloAcmeService02


HelloAcmeService03


HelloAcmeService04


HelloAcmeService05


 

After…

…removing the stuff I don’t want, and some renaming:

HelloAcmeService06


 

HelloAcmeService07


HelloAcmeService08


HelloAcmeService10

After an OK build let’s make sure we can reference it from the browser, by pressing Start in VS , and then clicking on the BaseAddress hyperlink  in the app.config (or just typing it in):

HelloAcmeService11

Now we could plug the WSDL into SoapUI and get a richer client experience (if those words mean nothing, that can be for another tutorial). For now, we’ll just create a console app to use the service.

Stop the service running in VS, so you can go back to editing. Create a console app under the solution (please note – there is in fact a compile error later using this name for reasons I am not going into here, and I won’t redo the screenshots – please call the console app HelloServiceAClient – note the ‘A’):

HelloAcmeService12

That gives you this new content in the Solution Explorer:

HelloAcmeService13

You now need to add a Service Reference to use the service from this client. Right click on References/Add Service Reference…, and in the dialogue, click Discover:

HelloAcmeService14

Click OK, and Solution Explorer now looks like this:

HelloAcmeService15

Now in Program.cs, add the text highlighted to access the service and use the GetData() method from the service:

HelloAcmeService16

The last action before testing it is to right-click on HelloServiceAClient and select Set as Startup Project.

HelloAcmeService17

Now press the Play button in VS, and you get this exciting output:

HelloAcmeService19

That’s it.

 


Advertisements

Visual Studio… Code Snippets: no snippets manager?

.., no problem:

Keeping things simple, this is a basic snippet, that can act as a template. It has no parameters:

BasicSnippet01

You will see that the Title and the ShortCut, and the filename, are all [MyTestMethodEmpty]. That helps with cataloguing and maintenance, I find.

So that is sitting on the file system in some scratch location, and we want to use it in Visual Studio.

These are the steps:

In Visual Studio, import the snippet you created earlier using any-old text editor (note that even though I intend to use this in C#, I have left the language at the default of Basic):

BasicSnippet02

Browse to the location where you created/saved your snippet, and click [Open] ([Open] not shown in the screenshot):

BasicSnippet03

The next dialog carries forward the named snippet, and suggests a location: this might be a default, or it might just be remembering my previous choice. I don’t care how it decided, this is fine for my purposes:

BasicSnippet04

We then click [Finish]/[OK] (not shown here), and we return to the IDE.

There are 2 things to check: a) Does Visual Studio find the snippet, and b) where has it stored it?

a) Does Visual Studio find the snippet

In the IDE, I start to type [myt], and it finds the shortcut:

BasicSnippet05

I then press tab twice… and it brings up the boilerplate that is clearly from the snippet I created (the squiggles are because I did the editing in a non-Test class). So this is clearly working:

BasicSnippet06

b) where has it stored it?

I just don’t trust Windows Search (and that continues into Windows 10), so I use PowerShell to look for the wild card. Although it’s not efficient, on an SSD it goes pretty quickly. And this screenshot confirms on the file system what we asked for in the IDE, in that we created the file c:\temp, and Visual Studio must have followed the instruction to put in the [My Code Snippets folder].

BasicSnippet07

gci -Path c:\ -Filter *empty*.snippet -Recurse -File -ErrorAction SilentlyContinue

Backup

As this are all pretty small, it is valid to take the whole content of the Code Snippets parent folder, and back that up – 25 files looks a bit suspiciously small, so maybe validate that before putting faith in the backup, but you get my point regarding size:

BasicSnippet08

Blue below represents the things you would change in a template without parameters:

<?xml version=”1.0″ encoding=”utf-8″?>
<CodeSnippets xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”&gt;
<CodeSnippet Format=”1.0.0″>
<Header>
<Title>MyTestMethodEmpty</Title>
<Author>Dennis</Author>
<Description>MyTestMethodEmpty</Description>
<HelpUrl></HelpUrl>
<SnippetTypes />
<Keywords />
<Shortcut>MyTestMethodEmpty</Shortcut>
</Header>
<Snippet>
<References />
<Imports />
<Declarations />
<Code Language=”csharp” Kind=”” Delimiter=”$”><![CDATA[[TestMethod]
public void TestMethod() {
Assert.Fail();
}]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Visual Studio: code snippets

As at work we might stop using ReSharper, I’ll assume that and come back to how you do Code Snippets, Visual Studio-style.

There’s this from MSDN which shows you how to create one.

This is how you can use it of course: you start to type letters and after some pre-defined pause it assumes you want to see the snippet library, in this case for all snippets starting with [p]:

VSSnippets01

Where has this come from?

VSSnippets02

VSSnippets03

Now interesting here is that e.g. the [propa] and [propdp] snippets are not in that list, so presumably it at least aggregates from a few different sources under the [C:\Program Files (x86)\Microsoft Visual Studio 14.0] area. So if I do try to find e.g. propdp,

VSSnippets05

, which expands to this promising code:VSSnippets04

I need to know the location and extension of a snippet, so from the Manager, I can get the location, giving this:

VSSnippets06

Notice that [Snippets] is below [VC#], rather than the other way round, which seems a bit mad to me. Regardless,I now know where to look for the propdp snippet:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC#\Snippets\1033\NetFX30

VSSnippets09

VSSnippets08

So that too is under the VC# umbrella, so maybe it is just grouping the C# bits together.

More MSDN [List of common C# snippets (.e.g. for properties)] [Create a New Snippet with Replacements] [Distribute Code Snippets]

Now my pet rant on C# code snippets vs ReSharper was the fact that you can’t do any on-the-fly substitution (see my YouTube elsewhere on what I mean by that) on macro variables. Fine, with a few extra keystrokes, you can achieve the necessary with the standard snippet [propfull]:

PropFull01 PropFull02

and in simple uses, this is probably enough (propg):

Propg01

Might be helpful to show what underpins the fuller one, propfull:

snippett01

Code

Visual Studio: diagnostics

I am a fan of the debugger in Visual Studio. For a number of years now, I have seen the Intellitrace window appear more as an irritant… because I knew nothing about it. So today I took the trouble to watch the VS-embedded video on this. And it sounds promising, particularly the ability to run Intellitrace on a machine that does not have VS, and to collect the data for subsequent analysis on a machine that does.

The diagnostics section of the MSDN blog:

Channel 9 Visual Studio videos, including the Intellitrace one.

Visual Studio: Productivity Tools

At home, I am running Visual Studio 2015, but I have no compatible ReSharper. The free alternative to ReSharper is Productivity Power Tools 2015. Not as good of course, but at $free what do you expect? Some people evidently report some issues on that same page, but mine seems to be OK.

PPTools01

And generally 2015, Enterprise Edition at least, gives some really helpful stuff e.g. inline getting of metadata to see what a referenced class looks like.