Louis van Tonder
Louis van Tonder

Reputation: 3720

Host WCF in a Windows Forms application

I have created a WCF service (DLL file), and I can use it when adding a service reference to it from my "adjacent" project in the solution.

I wish to make this WCF service accessible / host it, in a Windows Forms application. I need to use it from a remote location and need to access it via a URI. (IP address : Port !?)

What I am unsure of, is how to host it in the Windows Forms application? I have gone though many examples, but I can't quite get behind what needs to be done...

Do I add the DLL file reference to a new Windows Forms application, and somehow "shell" the DLL file? Can I change my WCF service project type to a Windows Forms project? What needs to happen here?

I would appreciate some basic examples, that I could build upon. I have no preference for binding, but although I will now be accessing it from another remote Windows Forms application, ultimately, it will be accessed/used by a remote ASP.NET web application.

For now, I need to get it working on:

Remote Windows Forms application <---> (server) WCF service (hosted in its own Windows Forms application)

How can I do this?

Upvotes: 1

Views: 1232

Answers (3)

Rajan chawla
Rajan chawla

Reputation: 71

You can refer to the article Four Steps to create first WCF Service: Beginners Series.

Upvotes: 1

Luis
Luis

Reputation: 1235

If I understand correctly, rather that ASP.NET, it sounds like you are looking for self-hosting. See How to: Host a WCF Service in a Managed Application.

Your service can stay in its own class library; you only need to instantiate it from a Windows Forms project. For example, copy that Program.Main() into your Program.cs, replacing the...

Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();

...lines with the...

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

...ones typically included in a Windows Forms project.

Upvotes: 1

CSharped
CSharped

Reputation: 1287

Try this...

Add the DLL file reference of your already-created WCF library to the new Windows application project and on any event, like a button click, try the following code.

ServiceHost sh = new ServiceHost("http://localhost:9092/MyService")
sh.open();

Upvotes: 1

Related Questions