在Windows窗体应用程序中托管交互式Web服务

时间:2012-04-09 14:38:07

标签: c# windows wcf forms

所以我知道如何在Windows窗体应用程序中托管WCF服务。 但是如何让服务与表单上的控件进行交互。 例如,我希望Web服务调用将图像加载到图片控件中。如果你找到了办法,请告诉我。

1 个答案:

答案 0 :(得分:0)

你可以这样做的一种方式就像下面......

注意:我会对这种方法感到有点担心,并且可能想要在做这样的事情之前更多地了解你想要达到的目标,但为了回答你的问题,这里是... < / em>的

假设您希望允许某人向您发送图片以在表单上的图片框中显示,从服务开始,它可能如下所示:

[ServiceContract]
public interface IPictureService
{
    [OperationContract]
    void ShowPicture(byte[] picture);
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
public class PictureService : IPictureService
{
    private readonly Action<Image> _showPicture;

    public PictureService(Action<Image> showPicture)
    {
        _showPicture = showPicture;
    }

    public void ShowPicture(byte[] picture)
    {
        using(var ms = new MemoryStream(picture))
        {
            _showPicture(Image.FromStream(ms));    
        }            
    }
}

现在创建一个用于显示图片的表单(Form1是表单的名称,pictureBox1是相关的图片框)。代码看起来像这样:

public partial class Form1 : Form
{
    private readonly ServiceHost _serviceHost;

    public Form1()
    {
        // Construct the service host using a singleton instance of the
        // PictureService service, passing in a delegate that points to
        // the ShowPicture method defined below
        _serviceHost = new ServiceHost(new PictureService(ShowPicture));
        InitializeComponent();
    }

    // Display the given picture on the form
    internal void ShowPicture(Image picture)
    {
        Invoke(((ThreadStart) (() =>
                                   {
                                       // This code runs on the UI thread
                                       // by virtue of using Invoke
                                       pictureBox1.Image = picture;
                                   })));
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // Open the WCF service when the form loads
        _serviceHost.Open();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        // Close the WCF service when the form closes
        _serviceHost.Close();
    }
}

为了完整性,添加一个app.config并放入这个(显然你主机服务并不重要,因为WCF会在很大程度上抽象它,但我想给你一个完整的例子):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
        <service name="WindowsFormsApplication1.PictureService">
            <endpoint address="" binding="wsHttpBinding" contract="WindowsFormsApplication1.IPictureService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8732/WindowsFormsApplication1/PictureService/" />
                </baseAddresses>
            </host>
        </service>
    </services>
  </system.serviceModel>
</configuration>

就是这样 - 如果你向ShowPicture操作发送一个作为图像的字节数组,它将显示在表单上。

例如,假设创建一个控制台应用程序并向上面定义的winforms应用程序中托管的服务添加服务引用,main方法可以简单地将其包含在内(并且logo.png将显示在表单上):< / p>

var buffer = new byte[1024];
var bytes = new byte[0];
using(var s = File.OpenRead(@"C:\logo.png"))
{
    int read;
    while((read = s.Read(buffer, 0, buffer.Length)) > 0)
    {
        var newBytes = new byte[bytes.Length + read];
        Array.Copy(bytes, newBytes, bytes.Length);
        Array.Copy(buffer, 0, newBytes, bytes.Length, read);
        bytes = newBytes;
    }              
}

var c = new PictureServiceClient();
c.ShowPicture(bytes);