English 中文(简体)
采用窗口形式的互动式网络服务
原标题:Hosting an interactive web service in a windows form application

So I know how to host an WCF service in a windows form application. But How do I get the service to interact with the controls on the form. For example I want the web service call to load an image into a picture control. Please let me know if you found a way to do this.

问题回答

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);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签