하위 레벨에서 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;
}
}
정확히 무엇을 :
다음은 첫 번째 경우의 SL 포럼에서 적응 샘플입니다. 사실, WCF 확장 점을 사용하여 서버에서 비슷한 작업을 수행 한 이후로 알았어 야합니다. 고마워요 파벨!. –