在 Windows 窗体应用程序中承载交互式 Web 服务
本文关键字:交互式 Web 服务 Windows 窗体 应用程序 | 更新日期: 2023-09-27 18:30:34
所以我知道如何在Windows窗体应用程序中承载WCF服务。但是如何让服务与窗体上的控件进行交互。例如,我希望 Web 服务调用将图像加载到图片控件中。 如果您找到了执行此操作的方法,请告诉我。
你可以这样做的一种方法是这样的...
注意:我会有点担心这种方法,并且可能想在做这样的事情之前更多地了解您想要实现的目标,但为了回答您的问题,这里是......
假设您希望允许某人向您发送图片以显示在窗体上的图片框中,因此从服务开始,它可能如下所示:
[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 方法可以简单地包含以下内容(徽标.png将显示在窗体上):
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);