-1

I am debugging the code of a server which receives requests and sends response using WCF.

I have never used WCF before. I found that when a client sends a request, a method in the server will be executed. I don't see anything particular about the declaration of the method.

I wonder how to specify a method of a server to run when the server receives a request? Is the declaration of such a method specified explicitly (with some modifier), or implicitly (via a special method name)?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Tim
  • 1
  • 141
  • 372
  • 590
  • Can you clarify your question? Are you asking you want to know what the actual request coming in is? – William Xifaras Mar 01 '17 at 18:46
  • I am asking how a server knows which method to be called to handle a newly received request. – Tim Mar 01 '17 at 18:49
  • When you say server, do you mean the WCF service running on the server? And are you interested in examining the request that comes in? – William Xifaras Mar 01 '17 at 18:52
  • It depends on the binding. A REST binding uses URL patterns, a SOAP binding uses the SOAP action to map an incoming request to an operation. What did your research show, and what is your actual question? What are you trying to do? See also http://stackoverflow.com/questions/1686411/mapping-a-wcf-request-message-to-the-underlying-operation, http://stackoverflow.com/questions/6446480/how-does-wcf-webapi-map-a-request-uri-to-the-appropriate-service-type-operatio, and so on. – CodeCaster Mar 01 '17 at 18:55
  • @CodeCaster Thanks. How can I tell if it is REST or SOAP binding? – Tim Mar 01 '17 at 19:09
  • Bindings are applied through code or configuration. – CodeCaster Mar 01 '17 at 19:10

1 Answers1

1

Methods that are callable require the OperationContract attribute.

https://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute(v=vs.110).aspx

You have the attribute either in an interface that your service implements

 [ServiceContract(...)]
 public interface IFoo
 {
      [OperationContract(...)]
      void Bar();
 }

 public class Foo : IFoo 
 {
      public void Bar()
      {
           ...
      }
 }

or you can omit the interface and have the attribute directly in the implementation

 [ServiceContract(...)]
 public class Foo : IFoo 
 {
      [OperationContract(...)]
      public void Bar()
      {
           ...
      }
 }

Note that the attribute has the Action property that further specifies how the action is called by the client

https://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontractattribute.action(v=vs.110).aspx

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
  • That is one part of the story. The question is how WCF decides that a particular request should invoke a certain operation. – CodeCaster Mar 01 '17 at 19:07
  • Thanks. The class implements an interface. In the declaration of the method in the interface, there is a `[OperationContract]` at the beginning of its declaration. Does it specify the method to be called upon a received request? Does it mean differently from `[ServiceContract(...)]`? – Tim Mar 01 '17 at 19:08
  • @Tim why don't you start by reading a WCF tutorial? [MSDN has a great deal of information](https://msdn.microsoft.com/en-us/library/ms734712(v=vs.110).aspx). – CodeCaster Mar 01 '17 at 19:09
  • 1
    @Tim: have you even implemented or used any web service which would use SOAP or REST binding? In any of the two, the client sends a web request that somehow (depending on which one is used) bind the request arguments to specific methods, in SOAP it's usually based on the `Action`, in REST on a specific uri route. – Wiktor Zychla Mar 01 '17 at 19:27
  • Thanks. No, I haven't. I have never learned SOAP or REST before. – Tim Mar 01 '17 at 19:50