In WCF, there are different forms of the service contract interface that can be used to define a WCF service. The three main forms are:
[ServiceContract]
attribute, and the methods are decorated with the [OperationContract]
attribute. This is the most common form of the service contract interface and is used to define the contract of the service and its operations.[ServiceContract] public interface IMyService { [OperationContract] void MyOperation(); }
public interface IMyService { void MyOperation(); } // Define the service contract using code ServiceContractAttribute serviceContractAttribute = new ServiceContractAttribute(); OperationContractAttribute operationContractAttribute = new OperationContractAttribute(); Type serviceContractType = typeof(IMyService); ServiceContractDescription serviceContractDescription = new ServiceContractDescription(serviceContractType, serviceContractAttribute); MethodInfo methodInfo = serviceContractType.GetMethod("MyOperation"); OperationDescription operationDescription = new OperationDescription(methodInfo.Name, serviceContractDescription); operationDescription.Messages.Add(new MessageDescription(operationContractAttribute.ReplyAction, MessageDirection.Output)); operationDescription.Messages.Add(new MessageDescription(operationContractAttribute.Action, MessageDirection.Input)); serviceContractDescription.Operations.Add(operationDescription);
public interface IMyService { void MyOperation(); } // Define the service contract using a fluent syntax ServiceContractDescription serviceContractDescription = new ServiceContractDescription(typeof(IMyService)); serviceContractDescription .AddOperation(new OperationDescription("MyOperation", serviceContractDescription) .AddMessage(new MessageDescription(MessageHeaders.ReplyTo, MessageDirection.Output)) .AddMessage(new MessageDescription(MessageHeaders.To, MessageDirection.Input)));
All three forms of the service contract interface are valid and can be used to define a WCF service. The attribute-based form is the most common and provides a simple and straightforward way to define the service contract and its operations. The code-based and fluent forms provide more flexibility and control, but are more complex and require more code to set up.
WCF Service Contract Interface Example
[ServiceContract] public interface IMyService { [OperationContract] string GetData(int value); }
WCF Service Contract with Data Contract Example
[DataContract] public class MyData { [DataMember] public int Value { get; set; } } [ServiceContract] public interface IMyService { [OperationContract] string GetData(MyData data); }
WCF Service Contract with Fault Contract Example
[FaultContract(typeof(MyFault))] [ServiceContract] public interface IMyService { [OperationContract] string GetData(int value); } [DataContract] public class MyFault { [DataMember] public string ErrorMessage { get; set; } }
WCF Service Contract with Callback Contract Example
[ServiceContract(CallbackContract = typeof(IMyCallback))] public interface IMyService { [OperationContract] void Subscribe(); [OperationContract] void Unsubscribe(); } public interface IMyCallback { [OperationContract] void OnDataReceived(string data); }
WCF Service Contract with Service Known Type Example
ServiceKnownType
attribute in the WCF service contract interface.[ServiceContract] [ServiceKnownType(typeof(MyDerivedType))] public interface IMyService { [OperationContract] string GetData(MyBaseType data); } [DataContract] public class MyBaseType { } [DataContract] public class MyDerivedType : MyBaseType { }
WCF Service Contract with Message Contract Example
[MessageContract] public class MyMessage { [MessageHeader] public string Header { get; set; } [MessageBodyMember] public string Body { get; set; } } [ServiceContract] public interface IMyService { [OperationContract] string ProcessMessage(MyMessage message); }
WCF Service Contract with Multiple Operations Example
[ServiceContract] public interface IMyService { [OperationContract] string GetData(int value); [OperationContract] void UpdateData(string data); }
WCF Service Contract with Message Header Example
[ServiceContract] public interface IMyService { [OperationContract] string GetData(); [OperationContract] string GetDataWithHeader(int value); }
WCF Service Contract with One-Way Operation Example
[ServiceContract] public interface IMyService { [OperationContract(IsOneWay = true)] void ProcessData(string data); }
WCF Service Contract with Asynchronous Operation Example
[ServiceContract] public interface IMyService { [OperationContract] Task<string> GetDataAsync(int value); }
react-big-calendar custom-pages https time-limiting development-environment brokeredmessage uikeyboard swiftui aar listbox