2011-06-24 32 views
6
[ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")] 
public interface IMyService 
{ 
    [OperationContract(Name = "MyOperation") 
    OperationResponse MyOperation(OperationRequest request); 
} 

Trong trường hợp này, điểm của ActionReplyAction là gì?WCF OperationContract - Điểm hành động và phản hồi là gì?


Edit: Tôi nên làm rõ câu hỏi của tôi ...

Làm thế nào wsdl của tôi sẽ khác nếu tôi không chỉ định các bộ phận này? Nó sẽ không chỉ sử dụng một số sự kết hợp của không gian tên, tên dịch vụ và tên opeartion anyways?

Trả lời

7

Bạn chỉ cần các thuộc tính Hành động/Trả lời nếu bạn muốn tùy chỉnh các giá trị đó trong thư (và chúng được phản ánh trong WSDL). Nếu bạn không có chúng, mặc định là <serviceContractNamespace> + <serviceContractName> + <operationName> cho Hành động và <serviceContractNamespace> + <serviceContractName> + <operationName> + "Response" cho Trả lời hành động.

Đoạn mã dưới đây in ra các thuộc tính Hành động/Trả lời của tất cả các thao tác trong dịch vụ.

public class StackOverflow_6470463 
{ 
    [ServiceContract(Namespace = "http://schemas.mycompany.com/", Name = "MyService")] 
    public interface IMyService 
    { 
     [OperationContract(Name = "MyOperation")] 
     string MyOperation(string request); 
    } 
    public class Service : IMyService 
    { 
     public string MyOperation(string request) { return request; } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(IMyService), new BasicHttpBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     foreach (ServiceEndpoint endpoint in host.Description.Endpoints) 
     { 
      Console.WriteLine("Endpoint: {0}", endpoint.Name); 
      foreach (var operation in endpoint.Contract.Operations) 
      { 
       Console.WriteLine(" Operation: {0}", operation.Name); 
       Console.WriteLine(" Action: {0}", operation.Messages[0].Action); 
       if (operation.Messages.Count > 1) 
       { 
        Console.WriteLine(" ReplyAction: {0}", operation.Messages[1].Action); 
       } 
      } 
     } 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

Vì vậy, khá nhiều nếu tôi đã làm 'ReplyAction = "http://schemas.mycompany.com/MyService/MyOperation" 'đó sẽ là giống hệt nhau để nó tự xây dựng nó? – michael

+1

Không, hành động trả lời sẽ là '" http://schemas.mycompany.com/MyService/MyOperationResponse "'. Tôi sẽ cập nhật câu trả lời bằng cách in ra các đặc tính hành động/trả lời của hoạt động. – carlosfigueira

+0

Xin lỗi, ý tôi là 'Action' và' ReplyAction' sẽ giống nhau nhưng với "Response" được thêm vào nó. – michael

1

Đôi khi WSDL được tạo không phù hợp với bạn. Một điều thú vị bạn cũng có thể làm là đặt Action = "*" để tạo trình xử lý tin nhắn không được nhận dạng.

http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.action.aspx

+1

Tại sao mọi người lại muốn tạo "trình xử lý tin nhắn không được công nhận"? – michael

+3

Bạn chắc chắn stumped anh ta, michael! Gần 2 năm và anh ấy vẫn nghĩ về nó! : P – seekerOfKnowledge

+6

@michael Tôi tin rằng đó không phải là "trình xử lý _message không được công nhận", nhưng "trình xử lý message_ _unrecognized". –

0

Action định nghĩa đầu vào của bạn uri cho sự vận hành xà phòng cho phương pháp dịch vụ của bạn.

Trả lờiHành động xác định số xuất uri cho phương thức dịch vụ của bạn.

Về cơ bản, chúng được sử dụng để tùy chỉnh uri cho cả hai.

[ServiceContract] 
public partial interface IServiceContract 
{ 
    [OperationContract(
      Action = "http://mynamspace/v1/IServiceContract/Input/ServiceMethod", 
      ReplyAction = "http://mynamspace/v1/IServiceContract/Output/ServiceMethod")] 
    SomeResponseType ServiceMethod(SomeRequestType x); 

Trong wsdl của bạn, bạn sẽ thấy

<wsdl:portType name="IServiceContract"> 
    <wsdl:operation name="ServiceMethod"> 
    <wsdl:input wsaw:Action="http://mynamspace/v1/IServiceContract/Input/ServiceMethod" name="SomeRequestType" message="tns:SomeRequestType " /> 
    <wsdl:output wsaw:Action="http://mynamspace/v1/IServiceContract/Output/ServiceMethod" name="SomeResponseType" message="tns:SomeResponseType " /> 
Các vấn đề liên quan