我已经开发了一个小型的C#表单应用程序,该应用程序调用Web服务。一切都很好,但是我需要保持状态并做到这一点,如果我没记错的话,我需要使用烹饪店。

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

我发现一些例子显示了样本,例如:

serviceClient.CookieContainer=new CookieContainer()

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

先感谢您!

这是结果的服务(自动创建):

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

// //此代码是由工具生成的。 //运行时版本: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;
    }
}

}

有帮助吗?

解决方案

使用WCF和WCF创建的此服务参考独立于通信的实际媒介,这意味着可以使用WebService或远程服务或MSMQ使用这些服务。远程和MSMQ没有cookie的概念。

最好使用wsdl.exe创建Web服务参考,然后在项目中添加结果类。我做了同样的事情。

其他提示

当您只能在绑定上启用cookie时,为什么还要恢复旧技术?

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top