One way you could do this is like below...
NOTE:我对这种做法感到担忧,并可能想更多地了解你在做类似事情之前所要达到的目标,但为了回答你在此提出的问题,这就是:。
Let s say you want to allow someone to send you a picture to display in a picture box on a form so starting from the service, it could look like this:
[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));
}
}
}
现在,你将制作显示照片的表格(图1是表格的名称,图Box1是有关图片箱)。 这部法典就是这样:
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();
}
}
简言之,添加一字(显然,你担任这一职务并不真正重要,因为世界团结基金将在很大程度上予以撤销,但我想给你一个全面的工作榜样):
<?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>
也就是说,如果你派一个形象的旁观阵列,将以这种形式展示。
For example say create a console application and add a service reference to the service hosted in the winforms application defined above, the main method could simply have this in it (and the logo.png will get displayed on the form):
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);