使用 CookieContainer 的 WCF Web 服务客户端
我开发了一个调用 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;
}
}
}
I have developed a small C# form application which calls a web service.
Everything works nicely but I need to keep state and to do that I need to use a CookieContainer if I am not mistaken.
I created the Service Reference by using the "Add Service Reference" menu of my project and everything worked nicely. But I do not know how to add a CookieManager on the created client.
I found some examples that were showing samples like:
serviceClient.CookieContainer=new CookieContainer()
but this is not the case. My service client does not have such a property. I am suing Visual Studio 2010 Beta by the way.
Thank you in advance!
This is the resulted ServiceReference(which was automatically created):
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.30128.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
namespace 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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您只需在绑定上启用 cookie 时,为什么要恢复到旧技术呢?
http://megakemp.wordpress.com/2009/02/06/managing -shared-cookies-in-wcf/
Why revert to old technology, when you can just enable cookies on the binding?
http://megakemp.wordpress.com/2009/02/06/managing-shared-cookies-in-wcf/
引:
如果 XML Web 服务方法使用会话状态,则会在响应标头中将 cookie 传递回 XML Web 服务客户端,该 cookie 唯一标识该 XML Web 服务客户端的会话。为了让 XML Web 服务维护客户端的会话状态,客户端必须存储 cookie。
详细信息: http://msdn .microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.cookiecontainer.aspx
Quoted:
If an XML Web service method uses session state, then a cookie is passed back in the response headers to the XML Web service client that uniquely identifies the session for that XML Web service client. In order for an XML Web service to maintain session state for a client, the client must store the cookie.
More information: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.cookiecontainer.aspx
上面的答案只有一篇非常有用的文章的链接,但没有其他详细信息
链接唯一的答案在 StackOverflow 上不是首选,因此这里是该文章的摘要,
这里是文章中的sinnept
这是 WCF 的解决方案(这也适用于我)
The answer above only has a link to a very helpful article, but it has no other details
link the only answer is not preferred on StackOverflow and because of that here is the summary of that article
here is sinnept from the article
and here is the solution for WCF (that is also worked for me)
使用 WCF 创建的此服务引用独立于实际的通信媒介,这意味着这些服务可以使用 Web 服务、远程处理或 MSMQ 来使用。而且远程处理和MSMQ 没有Cookie 的概念。
最好使用 WSDL.exe 创建 Web 服务引用并将生成的类添加到您的项目中。我也做了同样的事情。
This service reference in created using WCF and WCF is independent of the actual medium of communication that means these services can be used either using webservice or remoting or MSMQ. And remoting and MSMQ doesn’t have a concept of Cookies.
Better create your web service reference using WSDL.exe and add the resulting class in your project. I did the same thing.