2010-05-26 31 views
5

Tôi đã phát triển một ứng dụng biểu mẫu C# nhỏ gọi một dịch vụ web. Mọi thứ đều hoạt động tốt nhưng tôi cần giữ trạng thái và để làm điều đó tôi cần sử dụng CookieContainer nếu tôi không nhầm.WCF Web Service Client sử dụng CookieContainer

Tôi đã tạo Tham chiếu dịch vụ bằng cách sử dụng trình đơn "Thêm dịch vụ tham chiếu" của dự án và mọi thứ hoạt động tốt. Nhưng tôi không biết làm thế nào để thêm một CookieManager trên máy khách được tạo ra.

tôi thấy một số ví dụ đã hiển thị mẫu thích:

serviceClient.CookieContainer=new CookieContainer() 

nhưng đây không phải là trường hợp. Khách hàng dịch vụ của tôi không có tài sản như vậy. Tôi đang kiện Visual Studio 2010 Beta bằng cách này.

Cảm ơn bạn trước!

Đây là ServiceReference dẫn (được tạo ra tự động):

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

// // Mã này được tạo bởi một công cụ. // Phiên bản thời gian chạy: 4.0.30128.1 // // Thay đổi đối với tệp này có thể gây ra hành vi không chính xác và sẽ bị mất nếu // mã được tạo lại. // // -------------------------------------------- ----------------------------------

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) { 
     [email protected] = @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) { 
     [email protected] = @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 [email protected]; 
    } 

    [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 [email protected]; 
    } 
} 

}

+0

Để cho khách hàng dịch vụ của tôi tạo ra để có tài sản CookieContainer tôi đã phải tạo ra một WebReference (NET 2.0 Nhìn vào tiêu đề tương thích trên dưới dạng thuật sĩ) nhưng tôi cần phải làm điều đó với mặc định VS2010 đường. bất kỳ ý tưởng? – DaTroop

Trả lời

-3

này tham khảo dịch vụ được tạo ra sử dụng WCF và WCF là độc lập của môi trường thực tế của thông tin liên lạc có nghĩa là các dịch vụ này có thể được sử dụng hoặc sử dụng webservice hoặc Remoting hoặc MSMQ. Và remoting và MSMQ không có khái niệm về Cookie.

Tạo tài liệu tham khảo dịch vụ web của bạn tốt hơn bằng WSDL.exe và thêm lớp kết quả vào dự án của bạn. Tôi đã làm điều tương tự.

+0

Tôi đã làm điều đó! Nhưng cảm ơn vì lời giải thích! – DaTroop

+0

-1: Đề xuất không hợp lệ. Bạn đang đề nghị anh ta quay trở lại quá khứ để tránh học hỏi hiện tại. –

0

Được trích dẫn: Nếu một phương thức dịch vụ Web XML sử dụng trạng thái phiên, thì cookie sẽ được trả về trong tiêu đề phản hồi cho máy khách dịch vụ Web XML xác định duy nhất phiên cho ứng dụng dịch vụ Web XML đó. Để một dịch vụ Web XML duy trì trạng thái phiên cho một máy khách, máy khách phải lưu trữ cookie. thông tin

thêm: http://msdn.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.cookiecontainer.aspx

+0

Bạn đang xem thông tin về các dịch vụ web ASMX cũ. Không sử dụng chúng trừ khi bạn phải. –

12

Tại sao hoàn nguyên về công nghệ cũ, khi bạn chỉ có thể bật cookie trên liên kết?

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

+1

Một và chỉ có chức năng sulution tôi đã tìm thấy và nó hoạt động tuyệt vời. – David

+0

Tin nhắn của liên kết trong ngắn (nó hoạt động): binding.AllowCookies = true; Sử dụng (var webServiceProxy = new ReportExecutionServiceSoapClient (binding, endpoint)) {} –

+0

Như một lời cảnh báo .. giải pháp quản lý tập trung trong liên kết có một cái nhìn rất đơn giản về cookie và không đáp ứng bất kỳ 3 RFC cookie nào khác nhau (2109, 2965, 6265) vì vậy điều quan trọng là phải hiểu các yêu cầu của máy chủ mà bạn đang kết nối. Ví dụ: tôi đã thấy một máy chủ gửi nhiều cookie được phân cách bằng dấu phẩy theo 2109 nhưng chỉ bao gồm các máy chủ mà bạn muốn bạn cập nhật. –

Các vấn đề liên quan