所以我知道如何在 Windows 窗体应用程序中托管 WCF 服务。但是如何让服务与表单上的控件进行交互。例如,我希望 Web 服务调用将图像加载到图片控件中。如果您找到了这样做的方法,请告诉我。
问问题
2188 次
1 回答
0
您可以执行此操作的一种方法如下所示...
注意:我会有点担心这种方法,并且可能想在做这样的事情之前更多地了解你想要实现的目标,但为了在这里回答你的问题,它是......
假设您想允许某人向您发送一张图片以显示在表单上的图片框中,因此从服务开始,它可能如下所示:
[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 将显示在表单上):
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);
于 2012-04-10T09:22:09.727 回答