[英]Hosting an interactive web service in a windows form application
所以我知道如何在 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 方法可以簡單地將其包含在其中(並且 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.