7

我开发了一个调用 Web 服务的小型 C# 表单应用程序。一切正常,但我需要保持状态,如果我没记错的话,我需要使用 CookieContainer。

我使用项目的“添加服务参考”菜单创建了服务参考,一切运行良好。但我不知道如何在创建的客户端上添加 CookieManager。

我发现了一些显示示例的示例,例如:

serviceClient.CookieContainer=new CookieContainer()

但这种情况并非如此。我的服务客户端没有这样的属性。顺便说一句,我正在起诉 Visual Studio 2010 Beta。

先感谢您!

这是生成的 ServiceReference(它是自动创建的):

//------------------------------------------------------------------------------

// // 此代码由工具生成。// 运行时版本:4.0.30128.1 // // 对此文件的更改可能会导致不正确的行为,如果重新生成代码,则会丢失 // ////---------------------------------------------- --------------------------------

命名空间 WSClient.SecurityServiceReference {

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://max/", ConfigurationName="SecurityServiceReference.SecurityService")]
public interface SecurityService {

    // CODEGEN: Generating message contract since element name return from namespace  is not marked nillable
    [System.ServiceModel.OperationContractAttribute(Action="http://max/SecurityService/nextValRequest", ReplyAction="http://max/SecurityService/nextValResponse")]
    [System.ServiceModel.TransactionFlowAttribute(System.ServiceModel.TransactionFlowOption.Allowed)]
    WSClient.SecurityServiceReference.nextValResponse nextVal(WSClient.SecurityServiceReference.nextValRequest request);

    // CODEGEN: Generating message contract since element name return from namespace  is not marked nillable
    [System.ServiceModel.OperationContractAttribute(Action="http://max/SecurityService/reportSessionIDRequest", ReplyAction="http://max/SecurityService/reportSessionIDResponse")]
    [System.ServiceModel.TransactionFlowAttribute(System.ServiceModel.TransactionFlowOption.Allowed)]
    WSClient.SecurityServiceReference.reportSessionIDResponse reportSessionID(WSClient.SecurityServiceReference.reportSessionIDRequest request);
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class nextValRequest {

    [System.ServiceModel.MessageBodyMemberAttribute(Name="nextVal", Namespace="http://max/", Order=0)]
    public WSClient.SecurityServiceReference.nextValRequestBody Body;

    public nextValRequest() {
    }

    public nextValRequest(WSClient.SecurityServiceReference.nextValRequestBody Body) {
        this.Body = Body;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.Runtime.Serialization.DataContractAttribute()]
public partial class nextValRequestBody {

    public nextValRequestBody() {
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class nextValResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Name="nextValResponse", Namespace="http://max/", Order=0)]
    public WSClient.SecurityServiceReference.nextValResponseBody Body;

    public nextValResponse() {
    }

    public nextValResponse(WSClient.SecurityServiceReference.nextValResponseBody Body) {
        this.Body = Body;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.Runtime.Serialization.DataContractAttribute(Namespace="")]
public partial class nextValResponseBody {

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
    public string @return;

    public nextValResponseBody() {
    }

    public nextValResponseBody(string @return) {
        this.@return = @return;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class reportSessionIDRequest {

    [System.ServiceModel.MessageBodyMemberAttribute(Name="reportSessionID", Namespace="http://max/", Order=0)]
    public WSClient.SecurityServiceReference.reportSessionIDRequestBody Body;

    public reportSessionIDRequest() {
    }

    public reportSessionIDRequest(WSClient.SecurityServiceReference.reportSessionIDRequestBody Body) {
        this.Body = Body;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.Runtime.Serialization.DataContractAttribute()]
public partial class reportSessionIDRequestBody {

    public reportSessionIDRequestBody() {
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
public partial class reportSessionIDResponse {

    [System.ServiceModel.MessageBodyMemberAttribute(Name="reportSessionIDResponse", Namespace="http://max/", Order=0)]
    public WSClient.SecurityServiceReference.reportSessionIDResponseBody Body;

    public reportSessionIDResponse() {
    }

    public reportSessionIDResponse(WSClient.SecurityServiceReference.reportSessionIDResponseBody Body) {
        this.Body = Body;
    }
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.Runtime.Serialization.DataContractAttribute(Namespace="")]
public partial class reportSessionIDResponseBody {

    [System.Runtime.Serialization.DataMemberAttribute(EmitDefaultValue=false, Order=0)]
    public string @return;

    public reportSessionIDResponseBody() {
    }

    public reportSessionIDResponseBody(string @return) {
        this.@return = @return;
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public interface SecurityServiceChannel : WSClient.SecurityServiceReference.SecurityService, System.ServiceModel.IClientChannel {
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")]
public partial class SecurityServiceClient : System.ServiceModel.ClientBase<WSClient.SecurityServiceReference.SecurityService>, WSClient.SecurityServiceReference.SecurityService {

    public SecurityServiceClient() {
    }

    public SecurityServiceClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public SecurityServiceClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public SecurityServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public SecurityServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    WSClient.SecurityServiceReference.nextValResponse WSClient.SecurityServiceReference.SecurityService.nextVal(WSClient.SecurityServiceReference.nextValRequest request) {
        return base.Channel.nextVal(request);
    }

    public string nextVal() {
        WSClient.SecurityServiceReference.nextValRequest inValue = new WSClient.SecurityServiceReference.nextValRequest();
        inValue.Body = new WSClient.SecurityServiceReference.nextValRequestBody();
        WSClient.SecurityServiceReference.nextValResponse retVal = ((WSClient.SecurityServiceReference.SecurityService)(this)).nextVal(inValue);
        return retVal.Body.@return;
    }

    [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
    WSClient.SecurityServiceReference.reportSessionIDResponse WSClient.SecurityServiceReference.SecurityService.reportSessionID(WSClient.SecurityServiceReference.reportSessionIDRequest request) {
        return base.Channel.reportSessionID(request);
    }

    public string reportSessionID() {
        WSClient.SecurityServiceReference.reportSessionIDRequest inValue = new WSClient.SecurityServiceReference.reportSessionIDRequest();
        inValue.Body = new WSClient.SecurityServiceReference.reportSessionIDRequestBody();
        WSClient.SecurityServiceReference.reportSessionIDResponse retVal = ((WSClient.SecurityServiceReference.SecurityService)(this)).reportSessionID(inValue);
        return retVal.Body.@return;
    }
}

}

4

4 回答 4

13

当您可以在绑定上启用 cookie 时,为什么要恢复使用旧技术?

http://megakemp.wordpress.com/2009/02/06/managing-shared-cookies-in-wcf/

于 2011-01-11T14:59:34.220 回答
0

引用:如果 XML Web 服务方法使用会话状态,那么 cookie 会在响应标头中传递回 XML Web 服务客户端,该客户端唯一地标识该 XML Web 服务客户端的会话。为了让 XML Web services 为客户端维护会话状态,客户端必须存储 cookie。

更多信息:http: //msdn.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.cookiecontainer.aspx

于 2010-05-26T11:51:47.710 回答
0

上面的答案只有一个非常有用的文章的链接,但它没有其他详细信息
链接唯一的答案在 StackOverflow 上不是首选,因此这里是该文章的摘要

这是文章中的罪过

在 WCF 世界中,情况有些不同。WCF 是一种与传输无关的技术,它不允许 cookie 的概念直接反映在高级 API 中,因为它特定于 HTTP 协议。这实际上转化为没有任何 CookieContainer 属性要设置和检索的 Web 服务客户端对象。

这是 WCF 的解决方案(也适用于我)

private static void Main(object[] args)
{
    string sharedCookie;

    MyWebServiceClient client = new MyWebServiceClient();

    using (new OperationContextScope(client.InnerChannel))
    {
        client.DoSomething();

        // Extract the cookie embedded in the received web service response
        // and stores it locally
        HttpResponseMessageProperty response = (HttpResponseMessageProperty)
        OperationContext.Current.IncomingMessageProperties[
            HttpResponseMessageProperty.Name];
        sharedCookie = response.Headers["Set-Cookie"];
    }

    MyOtherWebServiceClient otherClient = new MyOtherWebServiceClient();

    using (new OperationContextScope(otherClient.InnerChannel))
    {
        // Embeds the extracted cookie in the next web service request
        // Note that we manually have to create the request object since
        // since it doesn't exist yet at this stage
        HttpRequestMessageProperty request = new HttpRequestMessageProperty();
        request.Headers["Cookie"] = sharedCookie;
        OperationContext.Current.OutgoingMessageProperties[
            HttpRequestMessageProperty.Name] = request;

        otherClient.DoSomethingElse();
    }
}
于 2021-07-14T13:27:12.797 回答
-3

使用 WCF 和 WCF 创建的此服务引用独立于实际的通信媒介,这意味着这些服务可以使用 web 服务或远程处理或 MSMQ 来使用。而且remoting和MSMQ没有Cookies的概念。

使用 WSDL.exe 更好地创建您的 Web 服务引用并将生成的类添加到您的项目中。我做了同样的事情。

于 2010-08-12T13:17:13.113 回答