1

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
  • Unclear why you have the requirement that says _*Server* wcf hosted in winapp_ . I'm sure there are ways to do so but the question is should you? One rationale for creating a Web Service is for it to be re-usable across clients. If clients can connect via http then WCF/IIS sounds like it should do. Any client can connect to it (even a winapp).You mention such in your post and I guess "simplify" is first order.... – EdSF May 25 '13 at 16:29
  • Hi EdSF, I am not sure what you are referring to? I need a Many-to-one type setup. Many client connections to one WCF service (hosted in app, I do not wish to setup IIS on the machine hosting it)? – Louis van Tonder May 27 '13 at 07:30
  • Could be of use: *[Hosting a WCF Service Library using Windows Forms](http://www.dotnetcodecentral.com/Post/43/wcf-hosting/host-wcf-service-using-windows-forms)* – Peter Mortensen Jul 24 '15 at 14:22
  • Possible duplicate of *[Hosting WCF service inside windows form](http://stackoverflow.com/questions/5231867/hosting-wcf-service-inside-windows-form)*. – Peter Mortensen Jul 24 '15 at 14:30

3 Answers3

1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Luis
  • 1,235
  • 1
  • 12
  • 12
  • Thanks, this worked. Using the info in the link provided, I created a winforms app, added my classes and interface classes, and used the code example to open a service host. As a matter of reference to anyone using this answer, I did not use the using block... not sure if I missed the point why they were there... as the code obviously closed the service host at the end of using... – Louis van Tonder May 27 '13 at 09:05
  • `using` would be useful if exceptions are thrown during execution of the service because the `Close()` would not be reached. However, there are other problems with it: refer to this other answer (and the discussion in the original question) for more details: http://stackoverflow.com/questions/573872/what-is-the-best-workaround-for-the-wcf-client-using-block-issue/5048865#5048865 – Luis May 27 '13 at 15:04
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajan chawla
  • 71
  • 1
  • 1
1

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();
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CSharped
  • 1,247
  • 4
  • 20
  • 49
  • Hi Pradeep. Although, from the solution Luis provided, I can see that your solution was on the right track, Luis provided the full solution. Thanks. – Louis van Tonder May 27 '13 at 09:02