WSDL에서 서비스 참조를 생성했습니다. 나는 서비스 레퍼런스에 대한 클라이언트를 성공적으로 코딩하고있다. serviceRef.serviceMethod (params ...) 패턴을 사용하여 서비스 기반 메소드를 호출했습니다. 이제 보낸 메시지에 http 헤더를 추가해야합니다. 서비스의 모든 메시지를 기본값으로 설정할 위치를 찾을 수 없으며 특정 방법을 호출 할 때 설정할 수있는 위치를 찾을 수 없습니다. 일부 기사에서는 IClientMessageInspector를 사용할 수 있다고 제안하지만 구현이 복잡해 보입니다. 어떤 제안?서비스 참조 서비스 메소드에 http 헤더 추가
4
A
답변
3
많은 장소에서 차용하지만 대부분 : 나는 간단
하지만 난 정의 httpheaders을 추가합니다 구현을 생각합니다.
public class HttpHeaderMessageInspector : IClientMessageInspector
{
private readonly Dictionary<string, string> _httpHeaders;
public HttpHeaderMessageInspector(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AfterReceiveReply(ref Message reply, object correlationState) { }
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
HttpRequestMessageProperty httpRequestMessage;
object httpRequestMessageObject;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers[httpHeader.Key] = httpHeader.Value;
}
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
foreach (var httpHeader in _httpHeaders)
{
httpRequestMessage.Headers.Add(httpHeader.Key, httpHeader.Value);
}
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
return null;
}
}
internal class HttpHeadersEndpointBehavior : IEndpointBehavior
{
private readonly Dictionary<string,string> _httpHeaders;
public HttpHeadersEndpointBehavior(Dictionary<string, string> httpHeaders)
{
this._httpHeaders = httpHeaders;
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
var inspector = new HttpHeaderMessageInspector(this._httpHeaders);
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher) { }
public void Validate(ServiceEndpoint endpoint) { }
}
그런 다음 newing까지 내 서비스 참조를 한 후 : 다른
var httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("header1", "value1");
httpHeaders.Add("header2", "value2");
_serviceRef.Endpoint.Behaviors.Add(new HttpHeadersEndpointBehavior(httpHeaders));
아무것도 변화가 없었다. 더 간단한 방법을 생각하면 알려주세요.
관련 문제
- 1. 동적으로 서비스 참조 추가
- 2. SOAP 웹 서비스/VS2010 서비스 추가 참조
- 3. "서비스 참조 추가"가 실패했습니다
- 4. net.pipe 서비스에 서비스 참조 추가
- 5. SOAP 웹 서비스 참조 추가
- 6. 웹 사이트에 서비스 참조 추가
- 7. .cs 파일에 서비스 참조 추가
- 8. 웹 참조 용 서비스 참조 추가
- 9. 웹 서비스 참조 추가 후 애매한 참조
- 10. 웹 서비스 프록시에 사용자 지정 Http 헤더 추가
- 11. 도메인 간 AJAX REST 서비스 HTTP 헤더
- 12. Visual Studio/.NET 서비스 참조 HTTP 버전
- 13. 웹 서비스 추가 서비스 참조 .NET 데이터 복제 계약
- 14. 생성 "서비스 참조 추가"아니 프록시 방법
- 15. JavaScript에 헤더 추가 WCF 서비스 호출
- 16. ASMX 서비스 요청에 SOAP 헤더 추가
- 17. 서비스 참조 문제
- 18. 웹 서비스 참조 추가 WP3에서 이상한 문제
- 19. net.msmq를 사용할 때 WCF 서비스 참조 추가
- 20. WCF 서비스 참조
- 21. 활동을 통해 서비스 메소드에 액세스
- 22. net.msmq 사용시 WCF 서비스 참조 추가 방법
- 23. WCF 서비스 참조 추가 XmlSerializer로 돌아갑니다
- 24. Java 응용 프로그램에 WCF 서비스 참조 추가
- 25. 빈 reference.cs 파일 추가 서비스 참조
- 26. WCF 추가 서비스 참조 동작 사용자 지정?
- 27. Net.TCP를 사용하는 WCF 서비스에 서비스 참조 추가
- 28. Silverlight 서비스 참조 배포
- 29. WCF 서비스 웹 참조
- 30. Silverlight의 WCF 서비스 참조
이 솔루션을 질문이 아닌 답변에 입력하십시오. –
http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.outgoingmessageheaders(v=vs.90).aspx는 간단한 솔루션입니다. – cederlof
@cederlof, 귀하의 링크는 SOAP 헤더를 참조합니다. 질문은 HTTP 헤더에 관한 것입니다. – NovaJoe