bir web başvurusu ihtiyaç çeşitli taşıma protokollerinin kullanılmasına izin verir. Bu nedenle, tüm protokole özel seçenekler (HTTP aktarımı için Çerezler gibi) bir WCF hizmet referansında bulunmamaktadır. Ancak, istemci ve sunucu arasında gönderilen iletileri inceleyen bir mesaj denetçisi ekleyebilirsiniz. Bu article, sunucuya çerez göndermenin bir yolunu açıklar.
Örneği CookieContainer kullanacak şekilde genişlettim. Ayrıca, aşağıdaki kod, kapsayıcıya yeni tanımlama bilgilerini eklemek için sunucu tarafından gönderilen Set-Cookie
başlığının nasıl değerlendirileceğini gösterir. Lütfen örnekte temel bir taslak gösterildiğini, ancak uzantıya veya biraz daha fazla doğrulamaya gerek duyabildiğini unutmayın. Ancak, basit bir senaryoda çalıştı.
Aşağıdaki kod parçacığı, IIS'de barındırılan ve ASP.NET çerçevesinde tümleşik olan bir WCF hizmetinin bir test yöntemini gösterir. Temelde bir dizesinde sunucuya gönderilir çerezleri yankıları ve iki yenilerini ekler:
public string GetData(int value)
{
var reply = string.Join(", ",
from x in HttpContext.Current.Request.Cookies.AllKeys
select x + "=" + HttpContext.Current.Request.Cookies[x].Value);
HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test", "Test123"));
HttpContext.Current.Response.Cookies.Add(new HttpCookie("Test2", "Test1234"));
return reply;
}
aşağıdaki test programı çerezleri için bir CookieContainer yaratır bir demo çerez eklenir ve bitiş noktasının için yeni bir davranış kaydeder hizmeti:
public class CookieBehavior : IEndpointBehavior
{
private CookieContainer cookieCont;
public CookieBehavior(CookieContainer cookieCont)
{
this.cookieCont = cookieCont;
}
public void AddBindingParameters(ServiceEndpoint serviceEndpoint,
System.ServiceModel.Channels
.BindingParameterCollection bindingParameters) { }
public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint,
System.ServiceModel.Dispatcher.ClientRuntime behavior)
{
behavior.MessageInspectors.Add(new CookieMessageInspector(cookieCont));
}
public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint,
System.ServiceModel.Dispatcher
.EndpointDispatcher endpointDispatcher) { }
public void Validate(ServiceEndpoint serviceEndpoint) { }
}
mesajı içinde:
class Program
{
static void Main(string[] args)
{
var cookieCont = new CookieContainer();
using(var svc = new TestServiceReference.TestServiceClient())
{
cookieCont.Add(svc.Endpoint.Address.Uri, new Cookie("TestClientCookie", "Cookie Value 123"));
var behave = new CookieBehavior(cookieCont);
svc.Endpoint.EndpointBehaviors.Add(behave);
var data = svc.GetData(123);
Console.WriteLine(data);
Console.WriteLine("---");
foreach (Cookie x in cookieCont.GetCookies(svc.Endpoint.Address.Uri))
Console.WriteLine(x.Name + "=" + x.Value);
}
Console.ReadLine();
}
}
davranış CookieContainer üzerinde özel bir mesajı müfettişi ve teslim ekleme amaca hizmet spector, BeforeSendRequest
yönteminde sunucuya bir istek gönderildiğinde çerez ekler ve AfterReceiveReply
yönteminde güncelleştirilmesi gereken çerezleri alır. BeforeSendRequest
tarafından döndürülen correlationState
AfterReceiveReply
yılında Uri almak için kullanılır unutmayın:
public class CookieMessageInspector : IClientMessageInspector
{
private CookieContainer cookieCont;
public CookieMessageInspector(CookieContainer cookieCont)
{
this.cookieCont = cookieCont;
}
public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply,
object correlationState)
{
object obj;
if (reply.Properties.TryGetValue(HttpResponseMessageProperty.Name, out obj))
{
HttpResponseMessageProperty httpResponseMsg = obj as HttpResponseMessageProperty;
if (!string.IsNullOrEmpty(httpResponseMsg.Headers["Set-Cookie"]))
{
cookieCont.SetCookies((Uri)correlationState, httpResponseMsg.Headers["Set-Cookie"]);
}
}
}
public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request,
System.ServiceModel.IClientChannel channel)
{
object obj;
if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out obj))
{
HttpRequestMessageProperty httpRequestMsg = obj as HttpRequestMessageProperty;
SetRequestCookies(channel, httpRequestMsg);
}
else
{
var httpRequestMsg = new HttpRequestMessageProperty();
SetRequestCookies(channel, httpRequestMsg);
request.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMsg);
}
return channel.RemoteAddress.Uri;
}
private void SetRequestCookies(System.ServiceModel.IClientChannel channel, HttpRequestMessageProperty httpRequestMessage)
{
httpRequestMessage.Headers["Cookie"] = cookieCont.GetCookieHeader(channel.RemoteAddress.Uri);
}
}
@marc_s: Hatta bir ASMX hizmet için, o "Servis Başvurusu Ekle" yi kullanarak edilmelidir. –