Preliminary Study of WCF-15: WCF operation agreement, Preliminary Study of wcf-15 Agreement

Source: Internet
Author: User

Preliminary Study of WCF-15: WCF operation agreement, Preliminary Study of wcf-15 Agreement

Preface:

  • In the previous article, when we define a service agreement, OperationContract is added to its operation methods. This feature belongs to the OperationContractAttribute class and applies OperationContract to methods, to indicate that this method is used to perform service operations as part of the service agreement (specified by the ServiceContractAttribute attribute. OperationContractAttribute attribute declaration method is an operation in the service agreement. Only methods with the OperationContractAttribute attribute can be made public as service operations. Service Agreements without Methods marked with OperationContractAttribute do not disclose any operations. Public operation methods are called Operation Agreements.

Operation Agreement attributes:

OperationContract:

  • Action: Get or set the WS-Addressing operation of the request message. WCF schedules request messages to methods based on their operations.

 

  • AsyncPattern: attribute indicates that the Begin/End method can be used to implement or asynchronously call this operation.

 

  • IsOneWay: gets or sets a value that indicates whether the operation returns a reply message.

 

  • IsInitiating: gets or sets a value that indicates whether the method can start a session (if any) on the server.

 

  • IsTerminating: gets or sets a value that indicates whether the server closes the session after a service operation sends a reply message (if any.

 

  • ProtectionLevel gets or sets a value that specifies whether the operated message must be encrypted and/or signed.

  

  • ReplyAction: gets or sets the value of the SOAP operation used to reply to a message for this operation.

WCFOperation Agreement example:

  • The solution is as follows:

  

  • Engineering Structure Description:

Set the operation contract Action (WS-Addressing operation for the request message) and ReplyAction (set the value of the SOAP operation for the Operation to reply to the message)

The ISampleMethod. cs code is as follows:

Using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. ServiceModel; namespace Service {[ServiceContract (Name = "SampleMethodContract", Namespace =" http://wangweimutou.SampleMethodContract ", SessionMode = SessionMode. required)] public interface ISampleMethod {// <summary> // The default value of IsInitiating is true, the default IsTerminating value is false /// </summary> /// <param name = "msg"> </param> [OperationContract (Name = "OCMethodOne", AsyncPattern = false, isInitiating = true, IsTerminating = false, Action =" http://wangweimutou.SampleMethodContract /RequestMethodOne ", ReplyAction =" http://wangweimutou.SampleMethodContract /ResponseMethodOne ")] string MethodOne (string msg); [OperationContract (Name =" OCMethodTwo ", AsyncPattern = false, IsInitiating = true, IsTerminating = false, Action =" http://wangweimutou.SampleMethodContract /RequestMethodTwo ", ReplyAction =" http://wangweimutou.SampleMethodContract /ResponseMethodTwo ")] string MethodTwo (string msg); [OperationContract (Name =" OCMethodThree ", AsyncPattern = true, IsInitiating = true, IsTerminating = false, Action =" http://wangweimutou.SampleMethodContract /RequestMethodThree ", ReplyAction =" http://wangweimutou.SampleMethodContract /ResponseMethodThree ")] IAsyncResult BeginMethodThree (string msg, AsyncCallback callback, object asyncState); string EndMethodThree (IAsyncResult result );}}

The sample method. cs code is as follows:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.ServiceModel;namespace Service{      public class SampleMethod :ISampleMethod    {        public string MethodOne(string msg)        {            return "You called MethodOne return message is: " + msg;        }        public string MethodTwo(string msg)        {            return "You called MethodTwo return message is: " + msg;        }        public IAsyncResult BeginMethodThree(string msg, AsyncCallback callback, object asyncState)        {            return new CompletedAsyncResult<string>(msg);        }        public string EndMethodThree(IAsyncResult r)        {            CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;            return "You called MethodThree return message is: " + result.Data;        }    }    class CompletedAsyncResult<T> : IAsyncResult    {        T data;        public CompletedAsyncResult(T data)        { this.data = data; }        public T Data        { get { return data; } }        #region IAsyncResult Members        public object AsyncState        { get { return (object)data; } }        public WaitHandle AsyncWaitHandle        { get { throw new Exception("The method or operation is not implemented."); } }        public bool CompletedSynchronously        { get { return true; } }        public bool IsCompleted        { get { return true; } }        #endregion    }}

2. Host: console application and service bearer program. Add a reference to the Service Assembly to implement the following code. The Code of Program. cs is as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using Service; using System. serviceModel; namespace Host {class Program {static void Main (string [] args) {using (ServiceHost host = new ServiceHost (typeof (SampleMethod) {host. opened + = delegate {Console. writeLine ("the service has been started. Press any key to terminate! ") ;}; Host. Open (); Console. Read ();}}}}View Code

The App. config code is as follows:

<? Xml version = "1.0"?> <Configuration> <system. serviceModel> <services> <service name = "Service. sampleMethod "behaviorConfiguration =" mexBehavior "> 3. Client: console application. Client application, start the Host service bearer program, add a reference to the service address http: // localhost: 1234/SampleMethod/, and set the namespace to ServiceRef,

Select the generate Asynchronous Operation check box to generate an asynchronous client proxy class (referWCFPreliminary Exploration-11:WCFThe client calls the Service asynchronously.)Then we can make synchronous and asynchronous calls to the program. The Program. cs code is as follows:

    

Run the program and the result is as follows:

  

From the results, we can see that the program stops calling MethodTwo and MethodThree. This is because the IsTerminating of MethodOne is set to true, so the client agent finishes calling MethodOne.

Then the server session is disabled. Next, we will comment out the code that calls MethodOne, compile the Client, and run it again. The result is as follows:

  

Because we set IsInitiating of MethodTwo to false, the server session is not started, so the service call fails. Next, we will perform three operations: MethodOne, MethodTwo, and MethodThree.

The IsInitiating and IsTerminating of the contract are set to true and false respectively, that is, they are set to the default value. After re-compiling the program, run the client and we can see the following results:

  

Next, let's take a look at the property values of the operation contract settings. From the client code above, we can see that the methods and names of the service contract and operation contract have been changed to the set value, for example, MethodOne is changed to OCMethodOne.

Open the client test program and add a reference to the service address. Then we can see the request and response of the message. The result of the MethodOne call is as follows:

  

Summary:

  • In the above example, we modified some properties of the operation contract, and also verified these attributes from the running results and exchanged messages. The implementation of asynchronous services is also completed. The message protection level attributes will be parsed in future blog posts.
  • For IsOneWay of OperationContract, you can view the following blog:

A Preliminary Study on WCF-3: one-way mode of the WCF message exchange mode

A Preliminary Study on WCF-5: Duplex Communication in the WCF message exchange mode)

WCF preliminary study-13: the WCF client creates a callback object for the duplex Service

  • For more information about asynchronous client service calling, see the following blog:

   A Preliminary Study on WCF-11: the WCF client calls the Service asynchronously.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.