16

例如,将 WebService 引用添加到 .NET 2.0 项目上的 ASMX 服务时,

var objService = new NameSpace.groupservices();

那里存在,

objService.CookieContainer = new System.Net.CookieContainer();

例如,将 ServiceReference 添加到 .NET 4.0 项目上的 ASMX 服务时,

var objService = new NameSpace.groupservicesSoapClient();

objService 没有任何 CookieContainer 属性

这里问了一个类似的问题,没有积极的解决方案。

有人可以指导在哪里找到房产吗?

4

3 回答 3

16

与绑定到 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;
}

以下测试程序为 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) { }
}

消息检查器在方法中向服务器发送请求时添加 cookie,BeforeSendRequest并检索应在AfterReceiveReply方法中更新的 cookie。请注意,correlationState返回的 byBeforeSendRequest用于检索 中的 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);
    }
}
于 2014-04-23T07:10:45.707 回答
15

打开您的 app.config 文件并将allowCookies="true"添加到绑定中。

像这样的东西:

<binding allowCookies="true" />
于 2014-08-08T22:13:45.640 回答
-1

在这里找到解决方案:

http://msdn.microsoft.com/en-us/library/bb628649.aspx

事实证明我需要一个网络参考而不是服务参考

于 2014-04-23T03:10:29.567 回答