2013-08-23 32 views
12

Khi thêm WebService Tham chiếu đến một dịch vụ ASMX trên một dự án .NET 2.0 ví dụ,Tôi sẽ đặt CookieContainer ở đâu trên Tham chiếu Dịch vụ?

var objService = new NameSpace.groupservices(); 

có tồn tại,

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

Khi thêm ServiceReference đến một dịch vụ ASMX trên một dự án .NET 4.0 cho Ví dụ,

var objService = new NameSpace.groupservicesSoapClient(); 

không có bất cứ tài sản CookieContainer cho objService

Một câu hỏi tương tự đã được hỏi here không có giải pháp tích cực.

Ai đó có thể vui lòng hướng dẫn nơi tìm tài sản?

+1

@marc_s: ngay cả đối với một dịch vụ ASMX, ông nên sử dụng "Add Service Reference". –

Trả lời

-1

Tìm thấy giải pháp ở đây:

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

Hóa ra tôi cần một tài liệu tham khảo web thay vì một tài liệu tham khảo phục vụ

+0

Uh, không, bạn không biết. Tại sao bạn nghĩ rằng bạn làm gì? –

+0

Đồng ý, Tham khảo web chỉ là phiên bản cũ. Sử dụng câu trả lời của @Markus, ở trên, thay vì –

8

Ngược lại với ASMX Dịch vụ Web được gắn với giao thông HTTP, WCF cho phép các giao thức truyền tải khác nhau được sử dụng. Vì vậy, không phải tất cả các tùy chọn giao thức cụ thể (chẳng hạn như Cookie cho HTTP vận chuyển) có sẵn trong một tài liệu tham khảo dịch vụ WCF.

Tuy nhiên, bạn có thể thêm trình kiểm tra thư kiểm tra thư được gửi giữa máy khách và máy chủ. Điều này article mô tả cách gửi cookie đến máy chủ.

Tôi đã mở rộng mẫu để sử dụng CookieContainer. Ngoài ra, mã sau đây cho thấy cách đánh giá tiêu đề Set-Cookie do máy chủ gửi để thêm cookie mới vào vùng chứa. Xin lưu ý rằng mẫu cho thấy một phác thảo cơ bản, nhưng có thể cần mở rộng hoặc xác nhận thêm một số. Tuy nhiên, trong một kịch bản đơn giản nó hoạt động.

Đoạn mã sau đây cho thấy một phương pháp thử nghiệm của dịch vụ WCF được lưu trữ trên IIS và được tích hợp trong khung ASP.NET. Về cơ bản nó vang cookie gửi đến máy chủ trong một chuỗi và thêm hai cái mới:

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; 
} 

Các chương trình thử nghiệm sau tạo một CookieContainer cho cookie, thêm một cookie bản demo và đăng ký một hành vi mới cho thiết bị đầu cuối của dịch vụ:

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(); 
    } 
} 

các hành vi phục vụ mục đích của việc thêm một thanh tra viên thông báo tùy chỉnh và bàn giao 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) { } 
} 

thông điệp trong spector đều thêm cookie khi một yêu cầu được gửi đến máy chủ theo phương thức BeforeSendRequest và truy xuất các cookie sẽ được cập nhật theo phương thức AfterReceiveReply. Lưu ý rằng correlationState trả về bởi BeforeSendRequest được sử dụng để lấy Uri trong 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); 
    } 
} 
8

Mở file app.config của bạn và thêm allowCookies = "true" đến các ràng buộc.

Something như thế này:

<binding allowCookies="true" /> 
+0

Cho rằng câu trả lời hàng đầu, giành giải thưởng ở đây quá phức tạp, có vẻ như đây không phải là câu trả lời, nhưng ít nhất với API của eTapestry, yêu cầu cookie hoạt động, đây là tất cả phải mất . –

+0

Wow ... câu trả lời cũ nhưng hoàn hảo. Đơn giản chiến thắng trên sự phức tạp điên rồ. Tôi đang làm việc với một API SOAP (InsideSales) yêu cầu thông tin đăng nhập để đặt cookie phiên cho tất cả các cuộc gọi trong tương lai. Đây là tất cả những gì tôi cần. –