.NET의 SOAP 클라이언트-참조 또는 예제?
배경:
SOAP 및 기타 프로토콜을 통해 다양한 유형의 간단한 서비스를 제공하는 웹 서비스 사이트를 만들고 있습니다. 목표는 변환, RSS 구문 분석, 스팸 확인 및 기타 여러 유형의 작업을 쉽게 수행 할 수 있도록하는 것입니다. 이 사이트는 주로 초보 개발자를 대상으로합니다.
내 문제:
나는 그 문제에 대해 C # 또는 .NET을 개발 한 적이 없습니다. 몇 년 전에 VB6을 해킹했지만 그게 다입니다. 이제 C #에서 SOAP를 통해 RPC 호출을 수행하는 몇 가지 예제가 필요합니다 . 나는 이것을 찾기 위해 웹과 Stack Overflow를 검색하려고 시도했지만 많은 리소스를 찾지 못했고 리소스의 순위를 매기는 방법을 알지 못합니다 (오래된 것, 잘못된 것은 무엇입니까? 등).
PHP에서 다음과 같이 호출되는 간단한 예제 서비스를 만들었습니다.
<?php
$client = new SoapClient('http://webservi.se/year'); //URL to the WSDL
echo $client->getCurrentYear(); //This method returns an integer, called "year"
?>
이제 C #에서 가능한 한 쉽게이 메서드를 호출하고 싶습니다. 모든 참조 및 예제는 매우 환영합니다. 어디서부터 시작합니까? 어떤 클래스 / 모듈 / 무엇을 활용할 수 있습니까?
더 나은 통신 프레임 워크 (백엔드는 확장 가능함)가있는 경우 솔루션은 SOAP를 전혀 포함 할 필요가 없지만 서버 측은 Unix의 PHP로 구현되므로 Microsoft의 독점 솔루션은 의문의 여지가 없습니다. 서버 측.
J. Random Web Developer가 따를 수있는 문서를 작성할 수 있도록이 정보가 필요합니다 (공유 웹 호스팅에 있더라도). 따라서 최선의 접근 방식은 코드로만 수행하는 것이되어야한다고 생각하지만, 다른 방법도 물론 환영합니다.
내가 이해했듯이 C # 클라이언트 응용 프로그램에서 웹 서비스를 호출하고 싶습니다. 이미 서비스를 가지고 있고 WSDL 파일을 게시했습니다 (내가 틀린 경우 수정). 이제 가장 간단한 방법은 C # 애플리케이션에서 프록시 클래스를 생성하는 것입니다 (이 프로세스를 서비스 참조 추가라고 함). 이를 수행하는 두 가지 주요 방법이 있습니다. .NET은 SOA를 수행하는 구식 인 ASP.NET 서비스와 John이 제안한대로 MS의 최신 프레임 워크 인 WCF를 제공하며 개방형 및 MS 독점 프로토콜을 포함한 많은 프로토콜을 제공합니다.
이제 충분한 이론과 단계적으로 해보자
- Visual Studio에서 프로젝트 열기 (또는 새 프로젝트 만들기)
- 솔루션 탐색기에서 프로젝트 (솔루션이 아닌 프로젝트)를 마우스 오른쪽 단추로 클릭하고 서비스 참조 추가를 클릭합니다.
아래 스크린 샷에 표시된 대화 상자가 나타납니다. wsdl 파일의 URL을 입력하고 확인을 누르십시오. 확인을 누른 후 오류 메시지가 표시되면 URL에서? wsdl 부분을 제거해보십시오.
솔루션 탐색기에서 서비스 참조를 확장하고 ServiceReference1 (이름은 다를 수 있음)을 두 번 클릭합니다. 생성 된 프록시 클래스 이름과 네임 스페이스가 표시되어야합니다. 제 경우 네임 스페이스는 WindowsFormsApplication1.ServiceReference1이고 프록시 클래스의 이름은 Service1Client입니다. 위에서 말했듯이 클래스 이름은 경우에 따라 다를 수 있습니다.
C # 소스 코드로 이동합니다. 추가
using WindowsFormsApplication1.ServiceReference1
.- 이제이 방법으로 서비스를 호출 할 수 있습니다.
Service1Client service = new Service1Client();
int year = service.getCurrentYear();
도움이 되었기를 바랍니다. 문제가 발생하면 알려주세요.
I have done quite a bit of what you're talking about, and SOAP interoperability between platforms has one cardinal rule: CONTRACT FIRST. Do not derive your WSDL from code and then try to generate a client on a different platform. Anything more than "Hello World" type functions will very likely fail to generate code, fail to talk at runtime or (my favorite) fail to properly send or receive all of the data without raising an error.
That said, WSDL is complicated, nasty stuff and I avoid writing it from scratch whenever possible. Here are some guidelines for reliable interop of services (using Web References, WCF, Axis2/Java, WS02, Ruby, Python, whatever):
- Go ahead and do code-first to create your initial WSDL. Then, delete your code and re-generate the server class(es) from the WSDL. Almost every platform has a tool for this. This will show you what odd habits your particular platform has, and you can begin tweaking the WSDL to be simpler and more straightforward. Tweak, re-gen, repeat. You'll learn a lot this way, and it's portable knowledge.
- Stick to plain old language classes (POCO, POJO, etc.) for complex types. Do NOT use platform-specific constructs like List<> or DataTable. Even PHP associative arrays will appear to work but fail in ways that are difficult to debug across platforms.
- Stick to basic data types: bool, int, float, string, date(Time), and arrays. Odds are, the more particular you get about a data type, the less agile you'll be to new requirements over time. You do NOT want to change your WSDL if you can avoid it.
- One exception to the data types above - give yourself a NameValuePair mechanism of some kind. You wouldn't believe how many times a list of these things will save your bacon in terms of flexibility.
- Set a real namespace for your WSDL. It's not hard, but you might not believe how many web services I've seen in namespace "http://www.tempuri.org". Also, use a URN ("urn:com-myweb-servicename-v1", not a URL-based namespace ("http://servicename.myweb.com/v1". It's not a website, it's an abstract set of characters that defines a logical grouping. I've probably had a dozen people call me for support and say they went to the "website" and it didn't work.
</rant>
:)
If you can get it to run in a browser then something as simple as this would work
var webRequest = WebRequest.Create(@"http://webservi.se/year/getCurrentYear");
using (var response = webRequest.GetResponse())
{
using (var rd = new StreamReader(response.GetResponseStream()))
{
var soapResult = rd.ReadToEnd();
}
}
Take a look at "using WCF Services with PHP". It explains the basics of what you need.
As a theory summary:
WCF or Windows Communication Foundation is a technology that allow to define services abstracted from the way - the underlying communication method - they'll be invoked.
The idea is that you define a contract about what the service does and what the service offers and also define another contract about which communication method is used to actually consume the service, be it TCP, HTTP or SOAP.
You have the first part of the article here, explaining how to create a very basic WCF Service.
More resources:
Aslo take a look to NuSOAP. If you now NuSphere this is a toolkit to let you connect from PHP to an WCF service.
You're looking in the wrong place. You should look up Windows Communication Framework.
WCF is used both on the client and on the server.
Here you can find a nice tutorial for calling a NuSOAP-based web-service from a .NET client application. But IMO, you should also consider the WSO2 Web Services Framework for PHP (WSO2 WSF/PHP) for servicing. See WSO2 Web Services Framework for PHP 2.0 Significantly Enhances Industry’s Only PHP Library for Creating Both SOAP and REST Services. There is also a webminar about it.
Now, in .NET world I also encourage the use of WCF, taking into account the interoperability issues. An interoperability example can be found here, but this example uses a PHP-client + WCF-service instead of the opposite. Feel free to implement the PHP-service & WFC-client.
There are some WCF's related open source projects on codeplex.com that I found very productive. These projects are very useful to design & implement Win Forms and Windows Presentation Foundation applications: Smart Client, Web Client and Mobile Client. They can be used in combination with WCF to wisely call any kind of Web services.
Generally speaking, the patterns & practices team summarize good practices & designs in various open source projects that dealing with the .NET platform, specially for the web. So I think it's a good starting point for any design decision related to .NET clients.
참고URL : https://stackoverflow.com/questions/3100458/soap-client-in-net-references-or-examples
'Development Tip' 카테고리의 다른 글
릴리스 빌드를 수행하기 위해 Gradle 및 Android Studio를 설정하는 방법은 무엇입니까? (0) | 2020.11.06 |
---|---|
SQL 문 들여 쓰기 우수 사례 (0) | 2020.11.06 |
Cygwin Make bash 명령을 찾을 수 없습니다. (0) | 2020.11.06 |
Django에서 사용자를 만드는 방법은 무엇입니까? (0) | 2020.11.06 |
Google 경고 : 리소스가 글꼴로 해석되지만 MIME 유형 application / octet-stream으로 전송 됨 (0) | 2020.11.06 |