例如,在.NET 2.0项目上向ASMX服务添加WebService引用时,
var objService = new NameSpace.groupservices();
存在,
objService.CookieContainer = new System.Net.CookieContainer();
例如,在.NET 4.0项目中将ServiceReference添加到ASMX服务时,
var objService = new NameSpace.groupservicesSoapClient();
objService
没有任何CookieContainer属性类似的问题被问到here没有正面解决方案。
有人可以指导在哪里找到该物业吗?
答案 0 :(得分:12)
打开app.config文件,将 allowCookies =“true”添加到绑定中。
这样的事情:
<binding allowCookies="true" />
答案 1 :(得分:11)
与绑定到HTTP传输的ASMX Web服务相比,WCF允许使用各种传输协议。因此,并非所有特定于协议的选项(例如用于HTTP传输的Cookie)都可在WCF服务引用中使用。
但是,您可以添加一个消息检查器来检查在客户端和服务器之间发送的消息。此article描述了将cookie发送到服务器的方法。
我已将示例扩展为使用CookieContainer。此外,以下代码显示如何评估服务器发送的Set-Cookie
标头,以将新cookie添加到容器中。请注意,示例显示了基本大纲,但可能需要扩展或更多验证。但是,在一个简单的场景中它起作用。
以下代码段显示了一个WCF服务的测试方法,该方法托管在IIS上并集成在ASP.NET框架中。它基本上回应了以字符串形式发送到服务器的cookie,并添加了两个新的:
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;
}
以下测试程序为cookie创建CookieContainer,添加演示cookie并为服务端点注册新行为:
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();
}
}
该行为用于添加自定义消息检查器并移交CookieContainer:
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) { }
}
消息检查程序在BeforeSendRequest
方法向服务器发送请求时添加cookie,并检索应在AfterReceiveReply
方法中更新的cookie。请注意,correlationState
返回的BeforeSendRequest
用于检索AfterReceiveReply
中的Uri:
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);
}
}
答案 2 :(得分:-1)