简体   繁体   中英

Can WCF Service Host call its service?

This is my project:

IWCFService.cs

[ServiceContract]
public interface IWCFService
{
    [OperationContract]
    void SetValue(string value);

    [OperationContract]
    bool SaveToDatabase();
}

WCFService.cs

public class WCFService : IWCFService
{
    public void SetValue(string value);
    {
        //Code insert value
    }

    public bool SaveToDatabase);
    {
        //Code save values to database
    }
}

WindowsServiceHost.cs

public partial class WindowsServiceHost : ServiceBase
{
    private ServiceHost _host;

    public WindowsServiceHost()
    {
        InitializeComponent();
    }
    protected override void OnStart(string[] args)
    {
        _host = new ServiceHost(typeof (WCFService.WCFService));
        _host.Open();
    }

    protected override void OnStop()
    {
        _host.Close();
    }
}

frmClient.cs

public partial class frmClient : Form
{
    private WCFServiceClient _client = new WCFServiceClient();

    public frmClient()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        _client.SetValue(39);
        MessageBox.Show("Done!");
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _client.SetValue(50);
        MessageBox.Show("Done!");
    }

    private void button3_Click(object sender, EventArgs e)
    {
        _client.SaveToDatabase();
        MessageBox.Show("Done!");
    }
}

Where can I put shared memory (to set value from many clients)? (in WCF Service or Windows Service)

Can I call SaveToDatabase() from Windows Service? (calling automatically after 5 seconds)

Thanks!

Web服务方法调用应视为完整操作,因此,如果需要存储传递的值,则应在WCFService.SetValue()内的某个地方调用SaveToDatabase并且无需将SaveDatabase公开为Web服务方法。

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