简体   繁体   中英

How to create a winform app in visual studio 2010 to host a wcf service

I have a working skeleton WCF service. I want to host it in a winform app with a simple start and stop button.

This is how I host in a console app, easy to change to win app

public Program()
        {                
                Console.WriteLine("This is the SERVER console");

                var myUri = new Uri[1];                    
                myUri[0] = new Uri(ConfigurationManager.AppSettings["baseAddress"]);

                var timeEntryService = new WCFTimeEntryService();    
                var host = new ServiceHost(timeEntryService, myUri);    
                host.Open();

                Console.WriteLine("Service Started!");    
                Console.WriteLine("Click any key to close...");
                Console.ReadKey();

                host.Close();    

        }

EDIT

First you need an interface that both client and server will use to communicate.

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Data;

namespace TimeEntryService
{
    [ServiceContract]
    public interface ITimeEntry
    {
        [OperationContract]
        string Ping();    
    }
}

Then you create the class that will do the work when a client calls.

using System.ServiceModel;
using System.Data;

namespace TimeEntryService
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class WCFTimeEntryService : ITimeEntry
    {
        public string Ping()
        { 
            return "Pong";
        }
    }
}

Then make sure you update your App.config (Use WCF Service Configuration Editor) In my VS2010 its under Tools -> Service Configuration Editor (Not sure if you need to do something to get it to show there).


When it runs up, you can use the WCF Test Client to confirm its working. C:\\Program Files\\Microsoft Visual Studio 10.0\\Common7\\IDE\\WcfTestClient.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM