Unmanaged code: calling win32 api from .Net

The first and second shots show the happy path. The third shot shows the effect of willfully misspelling MessageBox. You don’t get a compile time error because it is after all unmanaged code that .Net is accessing.

Useful site here

 

using System;
using System.Runtime.InteropServices;
namespace cli01 {
 internal class Program {
enum Options {
 OptionOk = 0,
 OptionOkCancel,
 OptionAbortRetryIgnore,
 OptionYesNoCancel,
 OptionYesNo,
 OptionRetryCancel,
 OptionCancelTryagainContinue
 }
[DllImport("user32.dll")]
 private static extern int MessageBox(IntPtr hWnd, string boxContent, string boxCaption, Options options);
private static void Main() {
 MessageBox(IntPtr.Zero, "The message in the box", "The caption", Options.OptionOkCancel);
 }
 }
}


The MSDN link on MessageBox. Look to be some errors in there suggesting it doesn’t get much love.

.Net:a client for an IIS-hosted WCF service

This is the third and last post on the basics of WCF usage. I use SoapUI to consume the WSDL which exposes the single method we created in the first post, and hosted using IIS in the second post. None of this covers how to install SoapUI, although hopefully there is enough here to show you how to use it.


 

At the end of the previous post, we had access to the service wsdl via the link in blue:

WCFinIIS15

Click on the link, and from the URL that comes up next, copy that to the clipboard:

WCFinIIS16.JPG

Start SoapUI, and create a project as shown here. My PC is called [i7], so adjust your entries to match your setup, and then click OK:

WCFinIIS17

After that, the left side panel should look something like this:

WCFinIIS18

Double click [Request 1] and the main panel looks like this:

WCFinIIS19

Just to see what happens, click the green play button in the TLH corner:

WCFinIIS20

If you now put e.g. 101 rather than the default [?], you get…

WCFinIIS21

, which is exactly what we expect and want.

 

.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.

 


DNX: working Console Application

Summary: I have at least proven that I can run a Console App from the command line, using dnu, dnx, dnvm, yada.This is the output from an hour’s effort, that it is, a console app called using dnx.exe:

VSDNX17

The snippets below record my steps a) to see if by building a console app in VS2015 using a template I could infer exactly how it is done manually, b) failing to understand why it could not resolve the System.* dependencies, c) ignoring guidance in MSDN blogs about Nuget package manager in VS sorting it out, and instead letting dnvm restore do its job, d) F5 and ctrl-F5 to get a clue about how it is executed (i.e. the path to the required dnx.exe), e) executing dnx.exe from the command line, passing in the location of the project.json file, which holds all the necessary info about how and what to execute, f) an implicit confirmation that console applications are no longer about building and running an .exe. (And I get that aspVNext/Roslyn etc is not per se about console apps – I was just looking for a way to do the simplest thing, with the smallest number of dependencies. This at least does now open up the door to knowing (I think!) how I can execute this on a low power Windows machine (or Linux – I have no access to OSX) without Visual Studio present. I could of course just have grabbed an old style csc.exe… but this is about doing Roslyn and related, not revisiting old ground. So the noisy snippets:

VSDNX01

VSDNX02

VSDNX03

VSDNX04

VSDNX05

VSDNX07

VSDNX08

VSDNX09

VSDNX10

VSDNX11 VSDNX12

VSDNX13

VSDNX14

VSDNX15

VSDNX16

VSDNX17

DNX

Just a bunch of links and shots right now:

aspnet 1,

I already have VS installed on my machine, hence presumably why it finds [dnvm]:

dnx01

dnx02

dnvm upgrade:

dnx03

Build script location:

dnx04

I cloned the repo to my desktop:

dnx05

Then ran a build.cmd… just to see:

dnx06

 

dnx07

The remainder is screenshots based on this post. Didn’t get to the end as although I selected a console app, it has generated a dll. My stamina has now gone.

dnx01 dnx02 dnx03 dnx04 dnx05 dnx06 dnx07 gruntdnx01 gruntdnx02 gruntdnx03 gruntdnx04 gruntdnx05 gruntdnx06 gruntdnx07 gruntdnx08 gruntdnx09 gruntdnx10 VSJson01 VSJson02 VSJson03 VSJson04 VSJson06 VSJson07 VSJson08