Get Familiar With Windows Communication Application Development

These questions are based on 70-503 TS: Microsoft .NET Framework 3.5 – Windows Communication Foundation Application Development (C#)

A Self Test Software Practice Test

Objective: Create services.

Sub-objective: Define service contracts.

  

Single answer, multiple-choice

 

You are using version 3.5 of the Microsoft .NET Framework to develop a Windows Communication Foundation (WCF) service. You have defined the following class for employees to handle purchase orders:

public class OrderService {
public void SubmitOrder ( int OrderID ) {
//Implementation omitted
}
public void ApproveOrder ( int OrderID, bool Approved ) {
//Implementation omitted
}
public string RetrieveOrder ( int OrderID ) {
//Implementation omitted
}
}

You want to expose the OrderService class as a WCF service. The OrderService class must support multiple inheritance and built-in contract versioning and backward compatibility features. What should you do?

 

A.     Apply the ServiceContract attribute to the OrderService class, and apply the OperationContract attribute on each method.

B.     Have the OrderService class inherit from the ServiceContract class, and add the static keyword to each method declaration.

C.     Have the OrderService class implement the IServiceContract interface, and add the static keyword to each method declaration.

D.     Create a new interface, apply the ServiceContract attribute to the interface and the OperationContract attribute on each of its methods, and have the OrderService class implement the interface.

 

Answer:

D.  Create a new interface, apply the ServiceContract attribute to the interface and the OperationContract attribute on each of its methods, and have the OrderService class implement the interface.

 

Tutorial:

To expose the OrderService class as a WCF service, you should use the ServiceContract and OperationContract attributes in a new interface and have the OrderService class implement that interface. The following code demonstrates this technique:

public class OrderService : IOrderService {
public void SubmitOrder ( int OrderID ) {
//Implementation omitted
}
public void ApproveOrder ( int OrderID, bool Approved ) {
//Implementation omitted
}
public string RetrieveOrder ( int OrderID ) {
//Implementation omitted
}
}

[ServiceContract]
public interface IOrderService {
     [OperationContract]
     void SubmitOrder ( int OrderID );
     [OperationContract]
     void ApproveOrder ( int OrderID, bool Approved );
     [OperationContract]
     string RetrieveOrder ( int OrderID );
}

In this scenario, you want to ensure the OrderService class supports multiple inheritance, built-in contract versioning and backward compatibility features. Therefore, you should create an interface and then have the OrderService class implement that interface.

You can create a service using any of the following methods:

 

  • Define the contract as a public interface and then create a class that implements the interface. You need to apply the ServiceContract attribute on the interface and the OperationContract attribute on the methods in the interface.
  • Create a class and apply the ServiceContract attribute on the class and the OperationContract attribute on the methods that should be made available to WCF clients.

There are many benefits of using an interface to implement the contract:

 

  • A service contract interface can extend multiple service contract or other interfaces.
  • Multiple service contracts can be fulfilled by a single class by implementing multiple service contract interfaces.
  • The implementation of a service contract can be changed without impacting the service contract.
  • Versioning of service can be done by implementing an old interface and a new interface. The old clients will connect to the original version; whereas the newer clients can connect to the newer version. Both contract versions could be implemented by same service class.

Using an interface to create a service contract is the preferred technique in most cases. However, you can mark the class directly with the ServiceContract attribute to create a service contract when you want to create it quickly and keep it simple. But the major disadvantage of this technique is you can only use a class to implement one service contract at a time.

A service is a program that receives messages and responds to these messages by executing code to perform predefined actions. The actions performed by a service are based on the contents of the message. A WCF service is a unit of functionality available to provide operations to clients. You create a WCF service by designing a class that implements WCF service contract. You should use the OperationContract, ServiceContract and DataContract attributes to create a service contract. A group of operations make up a service contract.

You can create a service operation by creating a public method and marking it with the OperationContract attribute. You can specify the unique action performed by the operation using the Action property of the OperationContract attribute. You can then group the operations to create a service contract.

The ServiceContract attribute is used to mark a service contract. You can specify the name of the service element in the configuration file to be used by using the ConfigurationName property of the ServiceContract attribute. You can also specify the name and namespace of the service contract using the Name and Namespace properties of the ServiceContract attribute respectively.

The DataContract attribute is used to serialize data that will be exchanged by service operations. You should explicitly apply the DataContract attribute on a type if it is not an intrinsic .NET type. By default, when a DataContract attribute is applied on a class, the class name is used as the local name of the contract. You can use the Name property of the DataContract attribute to override the default local name of the contract.

 

Also, when the DataContract attribute is applied on a class, the default namespace URI of the contract is the namespace of the class prefixed with “http : //schemas.datacontract.org/2004/07/”. You can override the default namespace URI by using the Namespace property of the DataContract attribute.

You should not apply the ServiceContract attribute to the OrderService class and apply the OperationContract attribute on each method because, when the ServiceContract attribute is applied directly on a class, it does not support multiple inheritance and built-in contract versioning. You should only apply the ServiceContract attribute directly to a class when you do not require multiple inheritances and want to simplify the service contract design.

You should not have the OrderService class inherit from the ServiceContract class and add the static keyword to each method declaration because the ServiceContract class is used for SQL Management Objects and is not used to expose a class as a WCF service. Also, the methods should not be class members but should be instance members if they are to be exposed as WCF operations.

You should not have the OrderService class implement the IServiceContract interface and add the static keyword to each method declaration because the IServiceContract interface is not a built-in interface in the default .NET Framework Library. You need to explicitly create an interface with the ServiceContract attribute applied and have the OrderService class implement that interface.

 

References:

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > WCF Feature Details > Endpoints: Addresses, Bindings and Contracts > Contracts > Designing Service Contracts

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > WCF Feature Details > Endpoints: Addresses, Bindings and Contracts > Contracts > Implementing Service Contracts

MSDN2 Library > .NET Framework 3.5 > .NET Framework Class Library > System.ServiceModel Namespace > ServiceContractAttribute Class

MSDN2 Library > .NET Framework 3.5 > .NET Framework Class Library > System.ServiceModel Namespace > OperationContractAttribute Class

 

 

Objective: Expose and deploy services.

Sub-objective: Create and configure service endpoints.

 

Single answer, multiple-choice

 

You are using version 3.5 of the Microsoft .NET Framework to develop a Windows Communication Foundation (WCF) service hosted in IIS. The WCF service will be consumed by an ASP.NET AJAX (Asynchronous JavaScript and XML) application.

You have designed the service contract as follows:

[ServiceContract]
public class AjaxWcfService {
[OperationContract] string OperationA ( string input ) {
//Implementation omitted
}
[OperationContract] string OperationB ( string input ) {
//Implementation omitted
}
}

The web.config file includes the following service registration:

<service name=”AjaxWcfService”>
<endpoint address=””
behaviorConfiguration=”ScriptBehavior”
binding=”webHttpBinding”
contract=”AjaxWcfService” />
</service>

Which configuration should you add to the web.config file to ensure the AjaxWcfService can be accessed using client-side JavaScript?

 

A.     <behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>

B.     <behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<enableAjaxAcces />
</behavior>
</endpointBehaviors>
</behaviors>

C.     <behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>

D.     <behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<clientVia viaUri=”javascript:” />
</behavior>
</endpointBehaviors>
</behaviors>

 

Answer:

C.

<behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>

 

Tutorial:

To ensure the AjaxWcfService can be accessed using client-side JavaScript, you should add the following configuration settings to the web.config file:

<behaviors>
<endpointBehaviors>
<behavior name=”ScriptBehavior”>
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>

This configuration specifies the endpoint behavior as enableWebScript to ensure the WCF service is accessible from ASP.NET AJAX applications. The endpoint behavior that you specify works successfully only with the corresponding endpoint. The enableWebScript endpoint behavior can only be used when the binding is a standard webHttpBinding or custom webMessageEncoding. In this scenario, the binding property of the endpoint is set to webHttpBinding.

Configuration information for a WCF service can be specified using code or configuration files, such as web.config for IIS-hosted services or app.config for other application hosts. WCF services are configured for communication using the XML elements service, endpoint and binding. The service element specifies the configuration settings for a WCF service and its associated endpoints. The WCF service endpoints are used to expose functionalities of a WCF service to client applications. An endpoint specifies the location of the service, the supported communication protocol and, optionally, metadata endpoint(s).

A WCF endpoint in a configuration file is specified using the endpoint element. The endpoint element attributes include address, binding and contract. The address attribute specifies the unique location of the WCF service to client applications. If the address attribute is not set, the service base address is used as the WCF service address. The binding attribute determines the transport protocol, security mechanism and encoding that will be used during communication with WCF service. The contract attribute specifies the service contract used for the WCF service.

The behaviors element is a parent element that contains one or more child elements, such as endpointBehaviors or serviceBehaviors. The endpointBehaviors element contains run-time configuration details associated with an endpoint, while the serviceBehaviors element contains the run-time configuration details for the service. Behavior configuration is specified using the behavior element in both elements. The attribute behaviorConfiguration specifies the name of the behavior to be used. The bindingConfiguration specifies the name of the binding configuration to be used.

The binding element specifies either a system or custom binding configuration to be used during communication with the WCF service. If you are adding more settings to a binding configuration, then the name property of the binding element should match the bindingConfiguration attribute of the endpoint element.

You should not use the configuration that specifies the endpoint behavior as webHttp because this will not enable access to the WCF endpoint using client-side JavaScript. The WebHttpBehavior type is used to enable the Web programming model in conjunction with the WebGet and WebInvoke attributes.

You should not use the configuration that specifies the endpoint behavior as enableAjaxAcces because this is not a valid element for endpoint behavior. If used, this endpoint behavior will generate syntax errors.

You should not use the configuration that specifies the endpoint behavior as clientVia because this element is used to specify routing using the viaUri property for WCF messages that use federation services.

 

References:

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > Basic WCF Programming > Configuring Services > Configuring Services Using Configuration Files

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > Basic WCF Programming > Configuring Services > Bindings > Configuring Bindings for Windows Communication Foundation Services

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > WCF Feature Details > AJAX Integration and JSON Support > Creating WCF Services for ASP.NET AJAX > How to: Use Configuration to Add an ASP.NET AJAX Endpoint

 

 

Objective: Instrument and administer services.

Sub-objective: Implement end-to-end service tracing.

  

Single answer, multiple-choice

 

You are configuring end-to-end tracing for a Windows Communication Foundation (WCF) service developed using the Microsoft .NET 3.5 Framework. You have added the following listener declaration to system.diagnostics section of the configuration file:

<sharedListeners>
<add name=”xmlListener”
initializeData=”C:\\Logs\\web_tracelog.svclog”
type=”System.Diagnostics.XmlWriterTraceListener” />
</sharedListeners>

You want to trace the service activities to debug and optimize its operations. The trace should use the specified listener. Which setting should you add to the configuration file?

 

A.     <sources>
<source name=”System.Runtime.Serialization”
switchValue=”Verbose,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

B.     <sources>
<source name=”System.Runtime.Serialization”
switchValue=”Critical,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

C.     <sources>
<source name=”System.ServiceModel”
switchValue=”Critical,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

D.     <sources>
<source name=”System.ServiceModel”
switchValue=”Verbose,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

 

Answer:

D.

<sources>
<source name=”System.ServiceModel”
switchValue=”Verbose,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

 

Tutorial:

To trace the service activities to debug and optimize its operations using the specified listener, you should add the following setting to the configuration file:

<sources>
<source name=”System.ServiceModel”
switchValue=”Verbose,ActivityTracing”>
<listeners>
<add name=”xmlListener” />
</listeners>
</source>
</sources>

The name attribute of the of the source element is used to specify the trace source that is to be used for trace logging. The trace source type System.ServiceModel is used to log all aspects of WCF processing, such as message processing and reading a configuration file. The switchValue attribute of the source element is used to specify the tracing level of the trace source. The trace level is set to Verbose to specify that the tracing should be done for all the events, including informational events. This level of tracing generally is used for debugging purposes or for optimizing the performance of an application. The switchValue attribute can be set to ActivityTracing to specify that tracing should be done for all flow events, including tracing for activity boundaries and endpoint transfers.

The Listeners section of the source element is used to specify the type and name of the trace listener. You can use the add attribute of the listeners element to specify the name of the listener.

Tracing can be used to capture and view WCF communication and infrastructure events. You can use tracing for events such as operation calls, code exceptions and transaction stages. You can use the SvcTraceViewer tool to view the trace output. The source trace can be configured either in the configuration file or in the code. WCF provides a trace source for each of the WCF assemblies. The trace sources determine the event types as follows:

 

  • System.ServiceModel: Logs all transport and security events in WCF processing.
  • System.ServiceModel.MessageLogging: Logs all messages send between WCF hosts.
  • System.ServiceModel.Activation: Logs activation and creation events associated with a WCF service.
  • System.IO.Log: Logs all events in the event log.
  • System.Runtime.Serialization: Logs when objects are serialized or de-serialized.

 

The switch value indicates the trace level and whether activity tracing is enabled. If activity tracing is enabled, then flow events between processing activities and components will be traced, as well. The trace levels are defined as follows:

 

  • Off: Disables tracing.
  • Critical: Only the non-application negative events are traced. This includes events such as unexpected processing or error conditions.
  • Error: All negative events are traced, including application-specific errors.
  • Warning: All negative events and run time.
  • Information: All negative and positive events are traced.
  • Verbose: All negative and positive events are traced, including application-specific informational events.
  • All: All events are traced.

 

You can use ActivityTracing to trace the activities of an application at run time. If you want to propagate the activity to the other endpoints involved in the message exchange, then you should set the propagateActivity attribute to true. When propagateActivity is set to true, the traces generated by the two endpoints can be compared to see how the traces at one endpoint flowed to the traces at another endpoint.

 

The propagateActivity attribute only can be set for the trace sources provided by WCF and not for the user-defined trace sources. The trace messages are collected, stored and routed by listeners. You can redirect the traces generated by the application or the service to a log, a window or a text file by using a listener. To read the trace log using the SvcTraceViewer tool, you should use the XmlWriterTraceListener class.

You should not set the switchValue attribute to the value Critical, ActivityTracing because trace-level Critical does not trace warning or positive events, required for optimization.

You should not set the name attribute of the of the source element to System.Runtime.Serialization because you need to trace all WCF processing events. You cannot set switchValue attribute to ActivityTracing with the trace source System.Runtime.Serialization.

 

References:

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > Administration and Diagnostics > Tracing > Configuring Tracing

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > Administration and Diagnostics > Tracing > End-to-End Tracing > Activity

 

 

Objective: Consume services.

Sub-objective: Create a service proxy.

 

Single answer, multiple-choice

 

You are developing a Windows Communication Foundation (WCF) client using the Microsoft .NET 3.5 Framework. The WCF service provides updated news articles and columns from newspapers around the world.

The WCF service is hosted in a local IIS server instance with the following settings:

 

  • Server: WcfWebServer
  • VirtualDirectory: Services
  • Service File: NewsService.svc

 

You want to generate a WCF client proxy with the least amount of developer effort. Which command-line statement should you run?

 

A.     svcutil /l:cs /t:metadata /tcv:Version35 http://WcfWebServer/Services/NewsService.svc?wsdl

B.     svcutil /l:cs /tcv:Version35 http://WcfWebServer/Services/NewsService.svc

C.     svcconfigeditor /l:cs /t:metadata /tcv:Version35 http://WcfWebServer/Services/NewsService.svc?wsdl

D.     svcconfigeditor /l:cs /tcv:Version35 http://WcfWebServer/Services/NewsService.svc

 

Answer:

B. svcutil /l:cs /tcv:Version35 http://WcfWebServer/Services/NewsService.svc

 

Tutorial:

To generate a WCF client proxy with the least amount of developer effort, you should use the ServiceModel Metadata Utility (Svcutil.exe) Tool as follows:

svcutil /l:cs /tcv:Version35 http://WcfWebServer/Services/NewsService.svc

This command-line statement uses the /language or /l switch to specify that WCF client code should be generated in C#. To ensure the WCF client is using the Microsoft .NET Framework version 3.5, the /tcv parameter is set to Version35. The main argument is set to the service file location http://WcfWebServer/Services/NewsService.svc. When this command-line statement is executed, the client proxy will be generated in C# using the Microsoft .NET 3.5 Framework.

The svcutil.exe utility can be used for various purposes, such as generating WCF client code, exporting metadata from client code, validating WCF service compiled code, downloading metadata from a WCF services and generating serialization code. The /target or /t switch specifies the output to be generated, while the /directory or /d switch specifies the output directory. The /out or /o switch specifies the file name of the output file. The svcutil.exe utility expects the main argument as an endpoint address, service file URL or metadata document path.

When generating a WCF client proxy, you may also use the following switches:

 

  • /async: Generates asynchronous signatures for service operations.
  • /config: Specifies the filename for configuration settings.

 

When exporting metadata from a WCF service, you may also use the following switches:

 

  • /t:metadata: Specifies that WCF metadata will be generated.
  • /serviceName: Specifies the service configuration name (used in probing).
  • /dataContractOnly: Indicates that only data contracts will be processed, not service contracts.

 

To validate a WCF service code, you should specify the /validate switch. To generate serialization code for a WCF service, you should specify the /t:xmlSerializer switch.

You should not use the command-line statement that sets the /t option to metadata because this will export metadata, not a WCF client proxy.

You should not use the command-line statements that specify the svcconfigeditor.exe tool because this tool is used to generate the XML for a WCF server or client configuration file.

 

References:

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > WCF Feature Details > Metadata > Using Metadata > Generating a WCF Client from Service Metadata

MSDN2 Library > .NET Framework 3.5 > Tools (.NET Framework Technologies) > Windows Communication Foundation Tools > ServiceModel Metadata Utility Tool (Svcutil.exe)

MSDN2 Library > .NET Framework 3.5 > Tools (.NET Framework Technologies) > Windows Communication Foundation Tools > Configuration Editor Tool (SvcConfigEditor.exe)

 

 

Objective: Secure services.

Sub-objective: Implement transport-level security.

  

Multiple answer, multiple-choice

 

You need to develop a Windows Communication Foundation (WCF) client using the Microsoft .NET 3.5 Framework. The client needs to invoke methods in a Web Services Enhancements (WSE) 3.0-enabled Web service. The WSE 3.0 service uses turnkey security assertions to enforce Web method authorization.

You want to create a WCF client that supports all of the features used by the WSE 3.0 service. You want to use the least amount of developer effort. What should you do? (Choose two. Each answer is part of the complete solution.)

 

A.     Run the Svcutil.exe tool to generate the client proxy class.

B.     Run the SvcConfigEditor.exe tool to generate the client proxy class.

C.     Run the Wsewsdl.exe tool to generate the client proxy class.

D.     Specify a WseHttpBinding binding as the client endpoint.

E.      Specify a WSDualHttpBinding binding as the client endpoint.

F.      Specify a custom binding that inherits from the Binding class as the client endpoint.

 

Answer:

A. Run the Svcutil.exe tool to generate the client proxy class.

F. Specify a custom binding that inherits from the Binding class as the client endpoint.

 

Tutorial:

To provide interoperability between WSE 3.0 Web services and WCF clients, you need to perform the following steps:

 

  1. Run the Svcutil.exe to generate the WCF client proxy class just as you would for a WCF service. Because the MEX protocol is unsupported, the Svcutil.exe tool will not retrieve any security assertions.
  2. Create a custom binding class derived from the Binding class. This class should map WSE 3.0 features to WCF properties.
  3. Specify the custom binding for the client endpoint.

The following code demonstrates how to derive from the Binding class and support WSE 3.0 security assertions:

public class WseHttpBinding : Binding {
private WseSecurityAssertion _securityAssertion;

public WseSecurityAssertion SecurityAssertion {
get { return _securityAssertion; }
set { _securityAssertion = value; }
}

public override BindingElementCollection CreateBindingElements () {
//Determine the BindingElementCollection based on the SecurityAssertion
}
}
public enum WseSecurityAssertion {
UsernameOverTransport = 0,
MutualCertificate10 = 1,
UsernameForCertificate = 2,s
AnonymousForCertificate = 3,
MutualCertificate11 = 4,
Kerberos = 5
}

The code derives a class named WseHttpBinding from the Binding class and creates a property named sercurityAssertion to support WSE turnkey assertions. The code then creates a BindingElementCollection instance by overriding the CreateBindingElements method. You should also add code in the client application code to set the binding properties.

When WCF clients are configured to use the August 2004 version of the WS-Addressing specification, they are compatible with WSE services. You can use the Svcutil.exe tool to generate a client proxy class. Since WSE services do not support the metadata exchange (MEX) protocol, the client proxy will not have security settings applied to it, so you need to create a custom binding class to provide WSE turnkey security settings.

You should not run the SvcConfigEditor.exe tool to generate the client proxy class because the SvcConfigEditor.exe tool is used to create the WCF configuration file using XML. You should not run the Wsewsdl.exe tool to generate the client proxy class because the Wsewsdl.exe tool is used to generate a WSE 3.0 proxy from an ASMX Web service.

You should not use WSDualHttpBinding class because WSDualHttpBinding binding is not compatible with WSE 3.0 Web services. None of the existing binding classes supports WSE turnkey security settings. You should not use the WseHttpBinding class because it is not a valid binding class. You must create a custom binding class to provide compatibility with the WSE 3.0 services.

 

References:

MSDN2 Library > .NET Framework 3.5 > Windows Communication Foundation > WCF Feature Details > Interoperability and Integration > Interoperability with Web Services Enhancements 3.0 > How to: Configure a WCF Client to interoperate with WSE 3.0 Services

MSDN2 Library > .NET Framework 3.5 > Tools (.NET Framework Technologies) > Windows Communication Foundation Tools > ServiceModel Metadata Utility Tool (Svcutil.exe)

Like what you see? Share it.Share on Google+Share on LinkedInShare on FacebookShare on RedditTweet about this on TwitterEmail this to someone
cmadmin

ABOUT THE AUTHOR

Posted in Certification|

Comment:

Leave a comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>