问题描述:

This question already has an answer here:

  • Hosting WCF service inside a Windows Forms application

    3 answers

网友答案:

You can open your app, and place something like this in your form:

  1. Create your WCF interface

    <ServiceContract()>
    Public Interface IyourInterface
    <OperationContract()>
    Function asyouwant ....
    
  2. Create the class that implements it

    Public Class clsYourClass
    Implements IyourInterface
    
  3. Instantiate it from your winforms app.

    (This is vb.net)

    Dim oYourService As ServiceHost
    Dim oYourBinding As New System.ServiceModel.BasicHttpBinding 
       ' Or WSHttpBinding ... configure as you want
    Dim aAddress As Uri()
    aAddress=   New Uri() {New Uri("http://localhost:port")}
    oYourService = New ServiceHost(GetType(clsYourClass), aAddress)
    oYourService.AddServiceEndpoint(GetType(IyourInterface), oYourBinding, "myWinformService.svc")
    oYourService.Open()
    

4 - Try this: http://localhost:port/myWinformService.svc

网友答案:

Simple service for example..

IService

[ServiceContract]
public interface IService
{
    [OperationContract]
    string Calculate(int price, int Qty);
}

Service

public class Service : IService
    {
        public string Calculate(int price, int Qty)
        {
            return Convert.ToString(price * Qty);
        }
    } 

Consume service by user

Go to Add Service reference option and discover the service. Add the service. Now the service displays in solution explorer.

http://localhost/WCFServiceSample/Service.svc

To check in web browser.

Usage in Application

using WindowsFormsApplicationWCF.ServiceReference1;

   Service1Client obj = new Service1Client();

   private void btnSubmit_Click(object sender, EventArgs e)
   {
       string result;
       result = obj.Calculate(Convert.ToInt32(txtPrice.Text), Convert.ToInt32(txtQty.Text));

        lblresult.Text = "The total price is" + result;
    }

Check these links for you reference,

Hosting WCF service inside windows form

http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-wcf-service-in-windows-application/

https://msdn.microsoft.com/en-us/library/ms731758(v=vs.110).aspx

相关阅读:
Top