WCF服务主机可以调用其服务吗?

时间:2015-07-23 06:32:26

标签: c# winforms wcf windows-services

这是我的项目:

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!");
    }
}

在哪里可以放置共享内存(从多个客户端设置值)? (在WCF服务或Windows服务中)

我可以从Windows服务调用SaveToDatabase()吗? (5秒后自动呼叫)

谢谢!

1 个答案:

答案 0 :(得分:1)

Web服务方法调用应被视为完整操作,因此如果您需要存储传递的值,则应在SaveToDatabase内的某处调用WCFService.SetValue(),并且不需要公开{{ 1}}作为Web服务方法。