2012-04-09 3 views
0

그래서 Windows 양식 응용 프로그램에서 WCF 서비스를 호스팅하는 방법을 알고 있습니다. 그러나 서비스에서 양식의 컨트롤과 상호 작용하는 방법 예를 들어, 그림 컨트롤에 이미지를로드하는 웹 서비스 호출을 원합니다. 이 작업을 수행 할 수있는 방법을 찾으면 알려주십시오. 이 아래처럼 할 수Windows 양식 응용 프로그램에서 대화 형 웹 서비스 호스팅

+0

코드 실행을 다시 UI 스레드로 가져와야합니다. IDesign 샘플 중 일부는 이것을 보여줍니다. – stephenl

+0

당신은 무엇을 시도 했습니까? – JotaBe

답변

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); 
관련 문제