2011-12-08 2 views
1

Silverlight 앱에서 이루어지는 모든 RIA 서비스 요청마다 HTTP 헤더를 전달해야합니다. 헤더의 값은 쿠키가 아니라 앱 인스턴스에서 가져와야합니다. DTO에 넣음으로써 이것이 가능하다는 것을 알았지 만 많은 서비스 호출이 엔티티와 변경 세트를 사용하기 때문에 옵션이 아닙니다. 그래서 모든 요청에 ​​대해 기본 클래스가 없습니다. 그래서 개발자들이 걱정할 필요가 없도록 각 요청에 대해 다시 무언가를 전달하는 중앙 집중적이고 안전한 수단을 찾고 있습니다. 사용자 지정 HTTP 헤더가 제대로 작동하지만 아웃 바운드 요청을 가로 채어 설정하는 방법을 알지 못합니다.Silverlight RIA 요청에 HTTP 요청 헤더를 추가하는 방법

누구나 시도 할 수있는 아이디어가 있습니까?

답변

3

하위 레벨에서 IClientMessageInspector의 도움으로 HTTP 헤더를 추가 할 수 있습니다. this post on SL forum부터 시도해보십시오.

다음 단계는 사용 사례에 따라 다릅니다.

DomainContext이 호출하는 모든 메소드에 대해 헤더 값이 같아야하는 경우 부분 클래스를 사용하여 컨텍스트를 확장하고 헤더 값에 대한 속성을 추가 한 다음 속성에서 속성을 사용할 수 있습니다.

각 메서드 호출마다 다른 값을 전달해야하는 경우 DomainContext을 다른 클래스로 래핑하고 헤더 값을 받아들이는 컨텍스트의 각 메서드에 인수를 추가하고 해당 값을 여하튼 경위. 말할 필요도없이, 코드 생성기가 없다면 이것은 어렵습니다. 내가 필요

public sealed partial class MyDomainContext 
{ 
    public string HeaderValue { get; set; } 

    partial void OnCreated() 
    { 
    WebDomainClient<IMyDomainServiceContract> webDomainClient = (WebDomainClient<IMyDomainServiceContract>)DomainClient; 
    CustomHeaderEndpointBehavior customHeaderEndpointBehavior = new CustomHeaderEndpointBehavior(this); 
    webDomainClient.ChannelFactory.Endpoint.Behaviors.Add(customHeaderEndpointBehavior); 
    } 
} 

public class CustomHeaderEndpointBehavior : IEndpointBehavior 
{ 
    MyDomainContext _Ctx; 

    public CustomHeaderEndpointBehavior(MyDomainContext ctx) 
    { 
    this._Ctx = ctx; 
    }  

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters) { } 
    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime) 
    { 
    clientRuntime.MessageInspectors.Add(new CustomHeaderMessageInspector(this._Ctx)); 
    } 
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher) { } 
    public void Validate(ServiceEndpoint endpoint) { } 
} 

public class CustomHeaderMessageInspector : IClientMessageInspector 
{ 
    MyDomainContext _Ctx; 

    public CustomHeaderMessageInspector(MyDomainContext ctx) 
    { 
    this._Ctx = ctx; 
    } 

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState) {} 
    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel) 
    { 
    string myHeaderName = "X-Foo-Bar"; 
    string myheaderValue = this._Ctx.HeaderValue; 

    HttpRequestMessageProperty property = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]; 
    property.Headers[myHeaderName] = myheaderValue; 
    return null; 
    } 
} 
+0

정확히 무엇을 :

다음은 첫 번째 경우의 SL 포럼에서 적응 샘플입니다. 사실, WCF 확장 점을 사용하여 서버에서 비슷한 작업을 수행 한 이후로 알았어 야합니다. 고마워요 파벨!. –

관련 문제