그래서 Windows 양식 응용 프로그램에서 WCF 서비스를 호스팅하는 방법을 알고 있습니다. 그러나 서비스에서 양식의 컨트롤과 상호 작용하는 방법 예를 들어, 그림 컨트롤에 이미지를로드하는 웹 서비스 호출을 원합니다. 이 작업을 수행 할 수있는 방법을 찾으면 알려주십시오. 이 아래처럼 할 수Windows 양식 응용 프로그램에서 대화 형 웹 서비스 호스팅
0
A
답변
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);
관련 문제
- 1. 호스팅 웹 서비스/WCF 서비스?
- 2. 응용 프로그램에서 WCF 서비스 호스팅
- 3. 대화 형 웹 응용 프로그램 스크랩 스크린
- 4. Windows 서비스에서 Silverlight 호환 웹 서비스 호스팅
- 5. Windows 서비스 호스팅
- 6. ASP.NET MVC 웹 응용 프로그램에서 호스팅 WCF 서비스
- 7. WPF 응용 프로그램에서 WCF 나머지 서비스 호스팅
- 8. Windows 서비스 응용 프로그램의 자체 호스팅 ASP.NET 웹 API
- 9. Windows 서비스 응용 프로그램에서 UseUnsafeHeaderParsing
- 10. Windows Vista에서 대화 형 서비스 대화 상자 감지
- 11. 독립 실행 형 응용 프로그램에서 Infopath 양식
- 12. Windows 서비스 응용 프로그램에서 보고서 보내기
- 13. 응용 프로그램에서 웹 양식 처리
- 14. VB 응용 프로그램에서 웹 서비스
- 15. Windows 양식 응용 프로그램에서 http 요청 수신하기
- 16. 웹 응용 프로그램에서 powershell runspace 호스팅
- 17. 응용 프로그램에서 웹 서비스 호출 취소 중
- 18. 대화 형 CLI 유형 웹 응용 프로그램
- 19. PHP로 대화 형 웹 응용 프로그램 설치
- 20. 대화 형 그림 (웹 응용 프로그램)
- 21. 웹 서비스 호스팅 (Netbeans)
- 22. WAS 호스팅 대 Windows 서비스 호스팅
- 23. Silverlight RIA 서비스 호스팅
- 24. Windows 응용 프로그램에서 WCF 데이터 서비스 사용
- 25. ASP.NET 응용 프로그램에서 .NET 컨트롤 호스팅
- 26. ASP.NET 웹 응용 프로그램에서 웹 서비스 사용
- 27. Windows Azure 플랫폼에서 wcf 서비스 호스팅
- 28. Windows 응용 프로그램 호스팅 Windows AZURE Microsoft에서 제공하는 클라우드 서비스
- 29. iPhone 응용 프로그램 asp.net과 함께 웹 서비스 호스팅
- 30. windows 서비스/웹 서비스
코드 실행을 다시 UI 스레드로 가져와야합니다. IDesign 샘플 중 일부는 이것을 보여줍니다. – stephenl
당신은 무엇을 시도 했습니까? – JotaBe