15 c# web-services web-reference asmx service-reference
例如,在.NET 2.0项目上向ASMX服务添加WebService引用时,
var objService = new NameSpace.groupservices();
Run Code Online (Sandbox Code Playgroud)
那里存在,
objService.CookieContainer = new System.Net.CookieContainer();
Run Code Online (Sandbox Code Playgroud)
例如,在.NET 4.0项目上将ServiceReference添加到ASMX服务时,
var objService = new NameSpace.groupservicesSoapClient();
Run Code Online (Sandbox Code Playgroud)
objService没有任何CookieContainer属性
有人可以指导在哪里找到该物业?
abj*_*ngs 14
打开app.config文件并将allowCookies ="true"添加到绑定中.
像这样的东西:
<binding allowCookies="true" />
Run Code Online (Sandbox Code Playgroud)
Mar*_*kus 13
与绑定到HTTP传输的ASMX Web服务相比,WCF允许使用各种传输协议.因此,并非所有特定于协议的选项(例如用于HTTP传输的Cookie)都可在WCF服务引用中使用.
但是,您可以添加一个消息检查器来检查在客户端和服务器之间发送的消息.该文章描述的方式来发送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;
}
Run Code Online (Sandbox Code Playgroud)
以下测试程序为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();
}
}
Run Code Online (Sandbox Code Playgroud)
该行为用于添加自定义消息检查器并移交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) { }
}
Run Code Online (Sandbox Code Playgroud)
消息检查器在向BeforeSendRequest方法中的服务器发送请求时添加cookie,并检索应在AfterReceiveReply方法中更新的cookie .请注意,correlationState返回的by BeforeSendRequest用于检索以下内容中的Uri AfterReceiveReply:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6746 次 |
| 最近记录: |